HTML to PDF

Generate high quality PDF documents from HTML files.

Endpoint

POST/v1/html-to-pdf

For a valid request to generate a PDF document you only need an API key and an HTML document which should be converted.

curl -X POST https://api.apistax.io/v1/html-to-pdf \
    -o 'document.pdf' \
    -H 'Authorization: Bearer API_KEY_HERE' \
    -H 'Content-Type: application/json' \
    -d '{ "content": "<html><body><h1>PDF</h1></body></html>"}'

Parameters

content: String

The HTML document which should be converted to PDF.

header: String

An HTML document that is printed at the top of each page of the resulting PDF file. It must be a complete and valid HTML document. For more information please have a look at the dedicated Header & Footer section.

footer: String

An HTML document that is printed at the bottom of each page of the resulting PDF file. It must be a complete and valid HTML document. For more information please have a look at the dedicated Header & Footer section.

width: Float = 21

The width of the PDF document in centimeters.

height: Float = 29.7

The height of the PDF document in centimeters.

marginTop: Float = 1

The margin in centimeters from the top. If a header is used, the margin should be large enough so that it is not covered by the content. More about this in the Header & Footer section.

marginBottom: Float = 1

The margin in centimeters from the bottom. If a footer is used, the margin should be large enough so that it is not covered by the content. More about this in the Header & Footer section.

marginStart: Float = 1

The margin in centimeters from the left.

marginEnd: Float = 1

The margin in centimeter from the right.

landscape: Boolean = false

Specifies whether to print the page in landscape orientation.

printBackground: Boolean = false

Specifies whether to print the background of the document. If the value is false all background CSS properties are ignored.

If all parameters are used, a possible request body may look like the following:

{
    "content": "<html><body><h1>DIN A3 PDF</h1></body></html>",
    "header": "<html><body><p>This is a header</p></body></html>",
    "footer": "<html><body><p>Page <span class=\"pageNumber\"></span></body></html>",
    "width": 29.7,
    "height": 42,
    "marginTop": 3.5,
    "marginBottom": 3.5,
    "marginStart": 2,
    "marginEnd": 2,
    "landscape": true,
    "printBackground": false
}

Header and footer are complete HTML documents, independent of the content. For example, CSS styles or fonts are not available from the content.

In difference to content there is the possibility to inject special print data into HTML elements:

  • date A formatted date from the moment of printing
  • title The page title
  • pageNumber The current page number
  • totalPages The total number of pages

For example, a footer document could look like following:

<html>
<head>
    <style>
        p {
            font-size: 12pt;
        }

        span {
            font-weight: bold;
        }
    </style>
</head>
<body>
<p>
    <span class="pageNumber"></span> of <span class="totalPages"></span>
</p>
</body>
</html>

Caution: If the header or footer is not visible, please consider the parameters marginTop and marginBottom if they leave enough space and the header or footer is not covered by the content.

Images & Fonts

If you want to use your own fonts or include images, you can include them as Base64 encoded resources.

An example of how a font can be included as Base64:

<html>
<head>
    <style>
        @font-face {
            font-family: 'Example';
            src: url(data:application/x-font-woff;chartset=utf-8;base64,NSUhEUgA...) format('woff');
            font-weight: normal;
            font-style: normal;
        }

        p {
            font-family: 'Example';
        }
    </style>
</head>
<body>
<p>Text displayed in Example font</p>
</body>
</html>

An example of how an image can be included as Base64:

<html>
<body>
<img src="data:image/png;base64,iVBORw0KG..." alt="Base64 image"/>
</body>
</html>

Possible errors

Identifier Description
message.contentMustNotBeBlank Parameter content is required.

It should be noted that there are a number of common error messages, which can be found in the "Possible errors" section of the general documentation.

HTML to PDF costs
Generate PDF files using HTML content as request payload.
2
Credits spent
Credit cost per REST call
0,0080
EUR per call
Price depends on your subscription