How to Convert HTML or Webpages to PDF with an API
1. What problem are we solving
Saving a webpage as a PDF is harder than simply printing to paper.
Web pages contain dynamic content, CSS styling and scripts that don't always render correctly when captured manually.
Developers often need a programmatic way to turn HTML or entire URLs into shareable, high-quality PDF documents.
In this guide, you'll learn how to solve this problem using an API.
2. When you need this solution
You may need to convert web content to PDF when:
- Generating printable reports from web dashboards
- Archiving webpages for compliance or record keeping
- Creating PDFs from user-generated HTML forms or invoices
- Embedding PDF exports into applications without building your own rendering engine
3. How the API solution works
A HTML-to-PDF API handles the rendering pipeline for you.
You pass either raw HTML or a URL, specify whether the output should be portrait or landscape, and the API converts the page to a PDF using a headless browser.
The service returns a link to the generated PDF along with status metadata.
This abstraction ensures consistent formatting across different pages and eliminates the need to maintain custom PDF generation code.
4. Step-by-step: Convert web content to PDF using an API
Step 1: Decide whether you will convert HTML content or a URL and prepare the content parameter accordingly
Step 2: Send a POST request to the HTML/URL to PDF API endpoint with your appkey, type, content and optional landscape flag
Step 3: Receive a JSON response containing a status object and a permanent link to the generated PDF
Step 4: Download or embed the PDF link into your application
5. API request example
cURL
curl -X POST "https://api.gugudata.io/v1/imagerecognition/html2pdf" \
-d "appkey=YOUR_APPKEY&type=URL&content=https://example.com&landscape=0"
Example response:
{
"DataStatus": {
"StatusCode": 100,
"StatusDescription": "请求成功。",
"ResponseDateTime": "2026-01-27T12:00:00Z",
"DataTotalCount": 1
},
"Data": "https://cdn.gugudata.io/pdf/abcd1234.pdf"
}
JavaScript (fetch)
const formData = new URLSearchParams();
formData.append('appkey', 'YOUR_APPKEY');
formData.append('type', 'HTML');
formData.append('content', '<html><body><h1>Hello PDF</h1></body></html>');
formData.append('landscape', '0');
fetch('https://api.gugudata.io/v1/imagerecognition/html2pdf', {
method: 'POST',
body: formData
})
.then(response => response.json())
.then(data => {
console.log(data.Data); // URL to the generated PDF
})
.catch(error => console.error(error));
Python (requests)
import requests
url = "https://api.gugudata.io/v1/imagerecognition/html2pdf"
payload = {
"appkey": "YOUR_APPKEY",
"type": "URL",
"content": "https://example.com",
"landscape": "0"
}
response = requests.post(url, data=payload)
data = response.json()
print(data["Data"])
6. Why use an API instead of manual conversion
Manual PDF conversion requires complex rendering engines and constant tuning to handle varied web layouts.
An API abstracts these details and provides reliable results without requiring you to run headless browsers yourself.
This saves development time and ensures consistent PDF output across different types of web content.
7. Frequently Asked Questions
Can this API convert both raw HTML and live web pages?
Yes. Use the type parameter to specify HTML for raw content or URL for a live webpage, and set the content accordingly.
Does the API preserve CSS styles and images?
The service renders the page using a headless browser, so CSS and images that load normally will be reflected in the PDF.
Is the generated PDF link permanent?
The API response includes a permanent link where the generated PDF is stored, allowing you to download or share it later.
Can I control orientation?
You can set landscape to 1 for landscape orientation or leave it at 0 for portrait.
8. Next steps
If you need to generate PDFs from HTML or URLs without building your own rendering infrastructure, try this HTML/URL to PDF API.
With a single POST request you can convert web content into a shareable PDF and integrate it into your workflow quickly.
Top comments (0)