APIVoid logo

HTML to PDF API Reference

Convert custom HTML code, such as an invoice or a newsletter template, into a PDF.

Service details and pricing: HTML to PDF API

POSThttps://api.apivoid.com/v2/html-to-pdf
20 credits per successful requestCan take up to 150 secondsPOST · JSON

Request example

Query the endpoint via an HTTPS POST request (replace YOUR_API_KEY_HERE with your API key):

curl
curl -X POST "https://api.apivoid.com/v2/html-to-pdf" \
     -H "Content-Type: application/json" \
     -H "X-API-Key: YOUR_API_KEY_HERE" \
     -d '{"html_base64": "PGh0bWw+PGJvZHk+PGgxPlRlc3RpbmcgSFRNTCB0byBQREYgQVBJPC9oMT48L2JvZHk+PC9odG1sPg=="}'

The same request in PHP:

php
$html = '<html><body><h1>Testing</h1><p>Example text...</p></body></html>';
$htmlBase64 = base64_encode($html);

$apiKey = 'YOUR_API_KEY_HERE';

$curl = curl_init('https://api.apivoid.com/v2/html-to-pdf');
curl_setopt($curl, CURLOPT_HTTPHEADER, ['Content-Type: application/json', 'X-API-Key: ' . $apiKey]);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode(['html_base64' => $htmlBase64]));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($curl);
$httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);

if ($httpCode === 200) {
    $responseData = json_decode($response, true);
    print_r($responseData);

    // Save the "base64_file" data as PDF file
    if (isset($responseData['rendered_file']['base64_file'])) {
        $saveAs = __DIR__ . '/document.pdf';

        file_put_contents($saveAs, base64_decode($responseData['rendered_file']['base64_file']));

        if (file_exists($saveAs)) {
            echo '<p>File document.pdf saved successfully!</p>';
        } else {
            echo '<p>Failed to create document.pdf file.</p>';
        }
    }
} else {
    print_r('An error occurred: '.$response);
}

Request parameters

Required

html_base64stringRequired

HTML data encoded in base64.

Optional

pdf_papersize_widthintegerDefault: 0

Change PDF paper width in pixels (max 5000).

pdf_papersize_heightintegerDefault: 0

Change PDF paper height in pixels (max 10000).

pdf_formatstringDefault: A4

Change PDF format, can be Letter, Legal, Tabloid, Ledger, A0, A1, A2, A3, A4, A5, A6.

pdf_marginintegerDefault: 0

Change PDF margin.

pdf_show_backgroundbooleanDefault: true

Show the background of the web page.

pdf_landscapebooleanDefault: false

Change the PDF orientation to landscape.

pdf_page_rangesstring

Select page ranges, can be 1 or 1-3 (for example).

pdf_scalefloat

Scale the PDF, must be between 0.1 and 2.

pdf_one_pagebooleanDefault: false

Try to fit the web page into a single PDF page.

Response example

A successful request returns HTTP 200 with a JSON body:

json · 200
{
    "rendered_file": {
        "format": "PDF",
        "date_time_utc": "2024-11-29 19:05:01",
        "base64_file": "JVBERi0xLjcKJeLjz9MKMSAwIG9iago8PC9UeXBlL0NhdGFsb2cvUGFnZXMgMiAwIFI+PgplbmRvYmoKMiAwIG9iago8PC9U...",
        "file_size_readable": "14.73 KB",
        "file_size_bytes": 15080
    },
    "elapsed_ms": 5485
}

Response fields

The fields returned in the JSON response:

rendered_file → formatstring

Format of the rendered file, e.g. PDF.

rendered_file → date_time_utcstring

Date and time (UTC) of when the file was rendered.

rendered_file → base64_filestring

The rendered PDF file encoded in base64.

rendered_file → file_size_readablestring

File size in human-readable format, e.g. 14.73 KB.

rendered_file → file_size_bytesinteger

File size in bytes.

elapsed_msinteger

Time taken to process the request, in milliseconds.