Link Search Menu Expand Document

PDF from HTML (HTML to PDF)

Related Knowledgebase-Explore Samples

Description: Convert HTML code snippet into full featured PDF. GET or POST request.

Status Errors

CodeDescription
200The request has succeeded
400bad input parameters
401unauthorized
403not enough credits
405Timeout error. To process large documents or files please use asynchronous mode ( set async parameter to true) and then check the status using /job/check endpoint. If a file contains many pages then specify a page range using pages parameter. The number of pages of the document can be obtained using the endpoint /pdf/info

Example

Sample Request:

! Don’t forget to set x-api-key url param or http header param (preferred) to API key, get yours here

POST
{
	"name" : "result.pdf",
	"html" : "true",
	"url" : "https://bytescout-com.s3.amazonaws.com/files/demo-files/cloud-api/pdf-to-html/sample.html"
}

Available Methods

[POST] /pdf/convert/from/html (raw html input)

Generate PDF from HTML input. You can also set the following params:

  • html required. Input HTML code to be converted. If you need to convert link or url to PDF then use /pdf/convert/from/url endpoint instead.
  • async optional. Set to true to run as async job in background (recommended for heavy documents).
  • name optional. File name for generated output. Must be a String.
  • expiration (optional). Output link expiration in minutes. Default is 60 (i.e. 60 minutes or 1 hour). After this delay generated output file(s) (if any) will be auto-removed from PDF.co temporary files storage. Max allowed expiration period depends on your current subscription plan. To store permanent input files (e.g. re-usable images, pdf, documents), please use PDF.co built-in Files Storage instead.
  • margins optional. set to css style margins like 10px, 5mm, 5in for all sides or 5px 5px 5px 5px (the order of margins is top, right, bottom, left).
  • paperSize optional. Letter is set by default. Can be Letter, Legal, Tabloid, Ledger, A0, A1, A2, A3, A4, A5, A6 or a custom size. Custom size can be set in px (pixels), mm or in (inches) with width and height separated by space like this: 200 300, 200px 300px, 200mm 300mm, 20cm 30cm or 6in 8in.
  • orientation optional. set to Portrait or Landscape. Portrait by default.
  • printBackground optional. true by default. Set to false to disable printing of background.
  • DoNotWaitFullLoad optional. false by default. Set to true to skip waiting for full load (like full video load etc that may affect the total conversion time).
  • header optional. Set to HTML for header to be applied on every page at the header.
  • footer optional. Set to HTML for footer to be applied on every page at the bottom.

The header and footer params should contain valid HTML markup with the following classes used to inject printing values into them:

date: formatted print date title: document title url: document location pageNumber: current page number totalPages: total pages in the document

For example, the following markup will generate Page N of NN page numbering:

<span style='font-size:10px'>Page <span class='pageNumber'></span> of <span class='totalPages'></span>.</span>

Sample JSON for PDF generation with more advanced header and footer. Note that the top and bottom page margins are important because the page content may overlap the footer or header.

{
 "html": ""<!DOCTYPE html><html><head><meta charSet=\"utf-8\"/><style type=\"text/css\">body { font-family: \"Arial\" }</style></head><body><h1>Hello</h1></body></html>",
  "async": false,
  "encrypt": false,
  "name": "result.pdf",
  "margins": "40px 5px 40px 5px",
  "paperSize": "Letter",
  "orientation": "Portrait",
  "printBackground": true,
  "header": "<div style='width:100%'><span style='font-size:10px;margin-left:20px;width:50%;float:left'>LEFT SUBHEADER</span><span style='font-size:8px;width:30%;float:right'>RIGHT SUBHEADER</span></div>",
  "footer": "<div style='width:100%;text-align:right'><span style='font-size:10px;margin-right:20px'>Page <span class='pageNumber'></span> of <span class='totalPages'></span>.</span></div>"
}
  • profiles optional. Must be a String. Use this param to set additional configuration for fine tuning and extra options. Explore PDF.co knowledgebase for profile examples.

  • Method: POST
  • URL: /v1/pdf/convert/from/html

Query parameters

No query parameters accepted.

Body payload

{
    "html": "<h1>Hello World!</h1><a href='https://pdf.co'>Go to PDF.co</a>",
    "name": "result.pdf",
    "margins": "5px 5px 5px 5px",
    "paperSize": "Letter",
    "orientation": "Portrait",
    "printBackground": true,
    "header": "",
    "footer": "",
    "async": false,
    "encrypt": false
}

Example responses

/pdf/convert/from/html
{
    "url": "https://pdf-temp-files.s3.amazonaws.com/97dc323f32794eae8fa6602f5bd981c1/result.pdf",
    "pageCount": 1,
    "error": false,
    "status": 200,
    "name": "result.pdf",
    "remainingCredits": 60646
}

Code Snippet

CURL
curl --location --request POST 'https://api.pdf.co/v1/pdf/convert/from/html' \
--header 'x-api-key: ' \
--header 'Content-Type: application/json' \
--data-raw '{
    "html": "<h1>Hello World!</h1><a href='\''https://pdf.co'\''>Go to PDF.co</a>",
    "name": "result.pdf",
    "margins": "5px 5px 5px 5px",
    "paperSize": "Letter",
    "orientation": "Portrait",
    "printBackground": true,
    "header": "",
    "footer": "",
    "async": false,
    "encrypt": false
}'

[POST] /pdf/convert/from/html (with custom profiles and custom js to disable links)

Generate PDF from HTML input. You can also set the following params:

  • html required. Input HTML code to be converted. If you need to convert link or url to PDF then use /pdf/convert/from/url endpoint instead.
  • async optional. Set to true to run as async job in background (recommended for heavy documents).
  • name optional. File name for generated output. Must be a String.
  • expiration (optional). Output link expiration in minutes. Default is 60 (i.e. 60 minutes or 1 hour). After this delay generated output file(s) (if any) will be auto-removed from PDF.co temporary files storage. Max allowed expiration period depends on your current subscription plan. To store permanent input files (e.g. re-usable images, pdf, documents), please use PDF.co built-in Files Storage instead.
  • margins optional. set to css style margins like 10px, 5mm, 5in for all sides or 5px 5px 5px 5px (the order of margins is top, right, bottom, left).
  • paperSize optional. Letter is set by default. Can be Letter, Legal, Tabloid, Ledger, A0, A1, A2, A3, A4, A5, A6 or a custom size. Custom size can be set in px (pixels), mm or in (inches) with width and height separated by space like this: 200 300, 200px 300px, 200mm 300mm, 20cm 30cm or 6in 8in.
  • orientation optional. set to Portrait or Landscape. Portrait by default.
  • printBackground optional. true by default. Set to false to disable printing of background.
  • header optional. Set to HTML for header to be applied on every page at the header.
  • footer optional. Set to HTML for footer to be applied on every page at the bottom.

The header and footer params should contain valid HTML markup with the following classes used to inject printing values into them:

date: formatted print date title: document title url: document location pageNumber: current page number totalPages: total pages in the document

For example, the following markup will generate Page N of NN page numbering:

<span style='font-size:10px'>Page <span class='pageNumber'></span> of <span class='totalPages'></span>.</span>

Sample for HTML PDF generation with more advanced header and footer. Note that the top and bottom page margins are important because the page content may overlap the footer or header.

{
 "html": ""<!DOCTYPE html><html><head><meta charSet=\"utf-8\"/><style type=\"text/css\">body { font-family: \"Arial\" }</style></head><body><h1>Hello</h1></body></html>",
  "async": false,
  "encrypt": false,
  "name": "result.pdf",
  "margins": "40px 5px 40px 5px",
  "paperSize": "Letter",
  "orientation": "Portrait",
  "printBackground": true,
  "header": "<div style='width:100%'><span style='font-size:10px;margin-left:20px;width:50%;float:left'>LEFT SUBHEADER</span><span style='font-size:8px;width:30%;float:right'>RIGHT SUBHEADER</span></div>",
  "footer": "<div style='width:100%;text-align:right'><span style='font-size:10px;margin-right:20px'>Page <span class='pageNumber'></span> of <span class='totalPages'></span>.</span></div>"
}
  • profiles optional. Must be a String. Use this param to set additional configuration for fine tuning and extra options. Explore PDF.co knowledgebase for profile examples.

  • Method: POST
  • URL: /v1/pdf/convert/from/html

Query parameters

No query parameters accepted.

Body payload

{
    "html": "<h1>Hello World!</h1>",
    "name": "result.pdf",
    "margins": "5px 5px 5px 5px",
    "printBackground": true,
    "header": "",
    "footer": "",
    "async": false,
    "encrypt": false,
    "profiles": "{\"orientation\": \"landscape\", \"paperSize\": \"letter\" }"
}

Example responses

/pdf/convert/from/html
{
    "url": "https://pdf-temp-files.s3.amazonaws.com/97dc323f32794eae8fa6602f5bd981c1/result.pdf",
    "pageCount": 1,
    "error": false,
    "status": 200,
    "name": "result.pdf",
    "remainingCredits": 60646
}

Code Snippet

CURL
curl --location --request POST 'https://api.pdf.co/v1/pdf/convert/from/html' \
--header 'x-api-key: ' \
--header 'Content-Type: application/json' \
--data-raw '{
    "html": "<h1>Hello World!</h1>",
    "name": "result.pdf",
    "margins": "5px 5px 5px 5px",
    "printBackground": true,
    "header": "",
    "footer": "",
    "async": false,
    "encrypt": false,
    "profiles": "{\"orientation\": \"landscape\", \"paperSize\": \"letter\" }"
}'

Knowledgebase

Samples