Link Search Menu Expand Document

PDF from Template (Mustache style)

Description: Creates PDF from HTML template. 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 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

PDF.co supports style HTML templates. For example, you can set `templateData` property to JSON like this `{ invoice: { company: \"ACME Company\"}}` and inside template will be replaced with ACME Company.

For more information about Handlebars & Mustache templates please visit https://handlebarsjs.com/.

Available Methods

[POST] /pdf/convert/from/html (using saved template)

Streamline PDF generation using HTML templates. API loads data from templateData parameter and uses this data for generating from template.

  • Create, test and manage your HTML templates here

Templates support style. This means you just need to insert macros surrounded by double brackets.

Example of macro inside html template:

  • `` will be replaced with test if you set templateData to { "variable1": "test"}
  • `` will be replaced with test if you set templateData to { "object1": { "variable1": "test"} }
  • simple conditions are also supported. For example: ` invoice was paid ` will show invoice was paid when templateData is set to { "paid": true }

Parameters

  • templateId required. Set to the ID of your html template. You can find and copy ID at HTML templates section in the app.
  • templateData required. Set it to a string converted from input JSON data.

NOTE: If you need to set it as a static value then make sure to use the escaped string value (use JSON.stringify(dataObject) in JS) where every " is replaced with \" (most programming languages are doing it automatically though. For example, string from { "variable1": "test"} will look like this in the request:

  • 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).
  • orientation optional. set to Portrait or Landscape. Portrait by default.
  • 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.
  • 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.
  • async optional. Set to true to run as async job in background (recommended for heavy documents).
  • profiles optional. Must be a String. You can set additional and extra options using this parameter that allows you to set custom configuration. See profiles samples for examples.

Description

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

Query parameters

No query parameters accepted.

Body payload

{
    "templateId": 1,
    "name": "newDocument.pdf",
    "margins": "40px 20px 20px 20px",
    "paperSize": "Letter",
    "orientation": "Portrait",
    "printBackground": true,
    "header": "",
    "footer": "",
    "async": false,
    "encrypt": false,
    "templateData": "{\"invoice\":{\"id\":\"12345\",\"date\":\"August 29, 2041\",\"dateDue\":\"September 29, 2041\"},\"issuer\":{\"name\":\"Sarah Connor\",\"company\":\"T-800 Research Lab\",\"address\":\"435 South La Fayette Park Place, Los Angeles, CA 90057\",\"website\":\"www.example.com\",\"email\":\"info@example.com\"},\"client\":{\"name\":\"Cyberdyne Systems\",\"company\":\"Cyberdyne Systems\",\"address\":\"18144 El Camino Real, Sunnyvale, California\",\"email\":\"sales@example.com\"},\"items\":[{\"name\":\"T-800 Prototype Research\",\"price\":\"$1000.00\"},{\"name\":\"T-800 Cloud Sync Setup\",\"price\":\"$300.00\"},{\"name\":\"Skynet Domain Setup\",\"price\":\"$50.00\"},{\"name\":\"T-800 OS License\",\"price\":\"$350.00\"},{\"name\":\"T-800 Vision Testing\",\"price\":\"$150.00\"}],\"discount\":\"$100.00%\",\"tax\":\"$126.88 (7.5%)\",\"total\":\"$1,876.88\",\"paid\":true,\"note\":\"Thank you for your support of advanced robotics.\"}"
}

Example responses

/pdf/convert/from/html (using saved template)
{
    "url": "https://pdf-temp-files.s3.amazonaws.com/d696cc7a587749f3af6d5131ea640937/newDocument.pdf",
    "pageCount": 1,
    "error": false,
    "status": 200,
    "name": "newDocument.pdf",
    "remainingCredits": 99993606,
    "credits": 9
}

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 '{
    "templateId": 1,
    "name": "newDocument.pdf",
    "margins": "40px 20px 20px 20px",
    "paperSize": "Letter",
    "orientation": "Portrait",
    "printBackground": true,
    "header": "",
    "footer": "",
    "async": false,
    "encrypt": false,
    "templateData": "{\"invoice\":{\"id\":\"12345\",\"date\":\"August 29, 2041\",\"dateDue\":\"September 29, 2041\"},\"issuer\":{\"name\":\"Sarah Connor\",\"company\":\"T-800 Research Lab\",\"address\":\"435 South La Fayette Park Place, Los Angeles, CA 90057\",\"website\":\"www.example.com\",\"email\":\"info@example.com\"},\"client\":{\"name\":\"Cyberdyne Systems\",\"company\":\"Cyberdyne Systems\",\"address\":\"18144 El Camino Real, Sunnyvale, California\",\"email\":\"sales@example.com\"},\"items\":[{\"name\":\"T-800 Prototype Research\",\"price\":\"$1000.00\"},{\"name\":\"T-800 Cloud Sync Setup\",\"price\":\"$300.00\"},{\"name\":\"Skynet Domain Setup\",\"price\":\"$50.00\"},{\"name\":\"T-800 OS License\",\"price\":\"$350.00\"},{\"name\":\"T-800 Vision Testing\",\"price\":\"$150.00\"}],\"discount\":\"$100.00%\",\"tax\":\"$126.88 (7.5%)\",\"total\":\"$1,876.88\",\"paid\":true,\"note\":\"Thank you for your support of advanced robotics.\"}"
}'