Skip to main content

POST /v1/pdf/convert/from/html

This API converts a RAW HTML code into a PDF document and process any JavaScript which the webpage triggers when it loads. For example if the the webpage triggers a JavaScript popup window then that will be included in the conversion process. There is no option to disable JavaScript on the supplied RAW HTML code.

Attributes

Attributes are case-sensitive and should be inside JSON for POST request. for example: { "url": "https://example.com/file1.pdf" }
Remember to ensure that request sizes are less than 4 mb in file size. For more information, see Request Size Limits.
AttributeTypeRequiredDefaultDescription
htmlstringYes-Input HTML code to be converted. To convert the link to a PDF use the /pdf/convert/from/url endpoint instead.
marginsstringNo-Set custom margins, overriding CSS default margins. Specify the margins in the format {top} {right} {bottom} {left}. You can usepx,mm,cmorinunits. Also, you can set margins for all sides at once using a single value.
paperSizestringNoA4Specifies the paper size. Accepts standard sizes like ‘Letter’, ‘Legal’, ‘Tabloid’, ‘Ledger’, ‘A0’–‘A6’. You can also set a custom size by providing width and height separated by a space, with optional units: px (pixels), mm (millimeters), cm (centimeters), or in (inches). Examples: ‘200 300’, ‘200px 300px’, ‘200mm 300mm’, ‘20cm 30cm’, ‘6in 8in’.
orientationstringNoPortraitSets the document orientation. Options: Portrait for vertical layout, and Landscape for horizontal layout.
printBackgroundbooleanNotrueSet to false to disable background colors and images are included when generating PDFs from HTML/URL
mediaTypestringNoprintControls how content is rendered when converting to PDF. Options: print (uses print styles), screen (uses screen styles), none (no media type applied).
DoNotWaitFullLoadbooleanNofalseControls how thoroughly the converter waits for a page to load before converting HTML to PDF --- false waits for full page load, while true speeds up conversion by waiting only for minimal loading.
headerstringNo-Set this to can add user definable HTML for the header to be applied on every page header. The format is html.
footerstringNo-Set this to can add user definable HTML for the footer to be applied on every page bottom. The format is html.
asyncbooleanNofalseSet async to true for long processes to run in the background, API will then return a jobId which you can use with the Background Job Check endpoint. Also see Webhooks & Callbacks
callbackstringNo-The callback URL (or Webhook) used to receive the POST data. see Webhooks & Callbacks. This is only applicable when async is set to true.
namestringNo-File name for the generated output, the input must be in string format.
expirationintegerNo60Set the expiration time for the output link in minutes. After this specified duration, any generated output file(s) will be automatically deleted from PDF.co Temporary Files Storage. The maximum duration for link expiration varies based on your current subscription plan. To store permanent input files (e.g. re-usable images, pdf templates, documents) consider using PDF.co Built-In Files Storage.
profilesobjectNo-See Profiles for more information.
    outputDataFormatstringNo-If you require your output as base64 format, set this to base64
    DataEncryptionAlgorithmstringNo-Controls the encryption algorithm used for data encryption. See User-Controlled Encryption for more information. The available algorithms are: AES128, AES192, AES256.
    DataEncryptionKeystringNo-Controls the encryption key used for data encryption. See User-Controlled Encryption for more information.
    DataEncryptionIVstringNo-Controls the encryption IV used for data encryption. See User-Controlled Encryption for more information.
    DataDecryptionAlgorithmstringNo-Controls the decryption algorithm used for data decryption. See User-Controlled Encryption for more information. The available algorithms are: AES128, AES192, AES256.
    DataDecryptionKeystringNo-Controls the decryption key used for data decryption. See User-Controlled Encryption for more information.
    DataDecryptionIVstringNo-Controls the decryption IV used for data decryption. See User-Controlled Encryption for more information.
The header and footer parameters can 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
img: tag is supported in both the header and footer parameter, provided that the src attribute is specified as a base64-encoded string. 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>
An example with an advanced header and footer. Note that the top and bottom page margins are important because 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,
    "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>"
}
If you use JSON as input then make sure to escape it first (with JSON.stringify(dataObject) in JS). Escaping is when every " is replaced with \". Example with " be escaped as \" then: "templateData": "{ \"paid\": true, \"invoice_id\": \"0002\", \"total\": \"$999.99\" }".

Query parameters

No query parameters accepted.

Responses

ParameterTypeDescription
urlstringDirect URL to the final PDF file stored in S3.
outputLinkValidTillstringTimestamp indicating when the output link will expire
pageCountintegerNumber of pages in the PDF document.
errorbooleanIndicates whether an error occurred (false means success)
statusstringStatus code of the request (200, 404, 500, etc.). For more information, see Response Codes.
namestringName of the output file
creditsintegerNumber of credits consumed by the request
remainingCreditsintegerNumber of credits remaining in the account
durationintegerTime taken for the operation in milliseconds

Example Payload

To see the request size limits, please refer to the Request Size Limits.
{
  "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": "",
  "mediaType": "print",
  "async": false
}

Example Response

To see the main response codes, please refer to the Response Codes page.
{
  "url": "https://pdf-temp-files.s3.amazonaws.com/97dc323f32794eae8fa6602f5bd981c1/result.pdf",
  "pageCount": 1,
  "error": false,
  "status": 200,
  "name": "result.pdf",
  "remainingCredits": 60646
}
Inconsistent URL Encoding in cURL Output: When using cURL to make API requests, the output JSON may show URL characters encoded as Unicode escape sequences. For example, the ampersand character (&) may appear as \u0026 in the cURL output. This is normal JSON encoding behavior and does not affect the validity of the URL. The URL will function correctly when used, as JSON parsers automatically decode these escape sequences. If you’re parsing the response programmatically, your JSON parser will handle this conversion automatically.

Code Samples

  • CURL
  • JavaScript/Node.js
  • Python
  • C#
  • Java
  • PHP
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": "",
"mediaType": "print",
"async": false
}'