PDF from HTML#

Available Methods#

/pdf/convert/from/html#

Create PDF from HTML.

  • Method: POST

  • Endpoint: /v1/pdf/convert/from/html

Attributes#

Note

Attributes are case-sensitive and should be inside JSON for POST request.

Attribute

Description

Required

html

Input HTML code to be converted. To convert the link to a PDF use the /pdf/convert/from/url endpoint instead.

yes unless you are using a templateId instead.

templateId

Set to the ID of your HTML template. You can find and copy the ID from HTML to PDF Templates.

no unless you are are not using html.

templateData

Set it to a string with input JSON data (recommended) or CSV data.

no unless you are using templateId.

margins

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).

no

paperSize

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.

no

orientation

Set to Portrait or Landscape. Portrait by default.

no

printBackground

true by default. Set to false to disable printing of background.

no

mediaType

Uses print by default. Set to screen to convert HTML as it appears in a browser or print to convert as it appears for printing or none to set none as mediaType for CSS styles.

no

DoNotWaitFullLoad

false by default. Set to true to skip waiting for full load (like full video load etc. that may affect the total conversion time).

no

header

User definable HTML for the header to be applied on every page header.

no

footer

User definable HTML for the footer to be applied on every page bottom.

no

async

Set 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 to check the status of the process and retrieve the output while you can proceed with other tasks.

no

name

File name for the generated output, the input must be in string format.

no

expiration

Set the expiration time for the output link in minutes (default is 60 i.e 60 minutes or 1 hour), 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.

no

profiles

Use this parameter to set additional configurations for fine-tuning and extra options. Explore the Profiles section for more.

no

HTML Templates#

Use the dashboard to manage your HTML to PDF Templates.

Templates use {{Mustache}} and Handlebars templating syntax. You just need to insert macros surrounded by double brackets like {{ and }}.

Some Examples of macro inside html template:

  • {{variable1}} will be replaced with test if you set templateData to { "variable1": "test"}

  • {{object1.variable1}} will be replaced with test if you set templateData to { "object1": { "variable1": "test"} }

  • Simple conditions are also supported. For example: {{#if paid}} invoice was paid {{/if}} will show invoice was paid when templateData is set to { "paid": true }.

Sample JSON input#

"templateData": "{ 'paid': true, 'invoice_id': '0002', 'total': '$999.99' }"

Note

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\" }".

Sample CSV input#

"templateData": "paid,invoice_id,total
true,0002,$999.99"

Query parameters#

No query parameters accepted.

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": "",
    "mediaType": "print",
    "async": false
}

Response 2#

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

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": "",
    "mediaType": "print",
    "async": false
}'


Code samples#

Footnotes

1

Supports links from Google Drive, Dropbox, and PDF.co Built-In Files Storage. To upload files via the API check out the File Upload section. Note: If you experience intermittent Too Many Requests or Access Denied errors, please try to add cache: to enable built-in URL caching. (e.g cache:https://example.com/file1.pdf) For data security, you have the option to encrypt output files and decrypt input files. Learn more about user-controlled data encryption.

2

Main response codes as follows:

Code

Description

200

Success

400

Bad request. Typically happens because of bad input parameters, or because the input URLs can’t be reached, possibly due to access restrictions like needing a login or password.

401

Unauthorized

402

Not enough credits

445

Timeout error. To process large documents or files please use asynchronous mode (set the async parameter to true) and then check status using the /job/check endpoint. If a file contains many pages then specify a page range using the pages parameter. The number of pages of the document can be obtained using the /pdf/info endpoint.