DEV Community

vøiddo
vøiddo

Posted on Originally published at voidd0.github.io

HTML to PDF API: how to convert HTML into A4, Letter or Legal PDF files (Python, cURL, no code)

Turn an HTML document into a print-ready PDF with page size, margins, orientation and page-numbered header and footer.

This tutorial shows what the Actor v0iddo/html-to-pdf returns, how to run it on the Apify Store without code, and how to call it from Python and cURL.

What data you get

One row per result. Every row can be downloaded as JSON, CSV, Excel or HTML, or read through the API.

Field Label Type Example
index # number 0
file_url File URL https://api.apify.com/v2/key-value-stores/lVWhmtaYNF01wiXf1/records...
filename File name text document.pdf
mime Type text application/pdf
bytes Bytes number 15311
credits Credits number 3
ms Time (ms) number 514
file_key file_key text result-001-document.pdf
pages pages number 1
format format text A4
blocked_requests blocked_requests number 0

Real rows from a run of the Actor (fields that name people are left out):

[
  {
    "index": 0,
    "file_url": "https://api.apify.com/v2/key-value-stores/lVWhmtaYNF01wiXf1/records/result-001-document.pdf?signature=sze38py0WNZeSdrDe0u0",
    "file_key": "result-001-document.pdf",
    "filename": "document.pdf",
    "mime": "application/pdf",
    "bytes": 15311,
    "pages": 1,
    "format": "A4",
    "blocked_requests": 0,
    "credits": 3,
    "ms": 514
  }
]
Enter fullscreen mode Exit fullscreen mode

No code: run it on the Apify Store

  1. Open the HTML to PDF API page on Apify.
  2. Fill in the input form. An example input is already filled in.
  3. Press Start, wait for the run to finish, and download the results as JSON, CSV or Excel from the Output tab.

The input that is pre-filled on the Actor page:

{
  "html": "<h1>Invoice #1042</h1><p>Total due: 120.00 USD, payable within 14 days.</p>"
}
Enter fullscreen mode Exit fullscreen mode

Ready-made examples

Saved inputs on Apify: run one as it is, or copy it and change the values.

Python (apify-client)

Get your API token in Apify Console under Settings, then Integrations. The Actor id is v0iddo/html-to-pdf.

from apify_client import ApifyClient

client = ApifyClient("YOUR_APIFY_TOKEN")

run_input = {'html': '<h1>Invoice #1042</h1><p>Total due: 120.00 USD, payable within 14 '
         'days.</p>'}

run = client.actor("v0iddo/html-to-pdf").call(run_input=run_input)
for item in client.dataset(run["defaultDatasetId"]).iterate_items():
    print(item)
Enter fullscreen mode Exit fullscreen mode

cURL

curl -X POST "https://api.apify.com/v2/acts/v0iddo~html-to-pdf/run-sync-get-dataset-items" \
  -H "Authorization: Bearer YOUR_APIFY_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"html": "<h1>Invoice #1042</h1><p>Total due: 120.00 USD, payable within 14 days.</p>"}'
Enter fullscreen mode Exit fullscreen mode

Price

$3.40 per 1,000 results. You pay per result row. There is no separate charge per run. Higher Apify plans pay less per result, down to $2.38 per 1,000.

FAQ

What data does the HTML to PDF API return?

Each result is one row in an Apify dataset. Fields include index, file_url, filename, mime, bytes, credits, ms, file_key, and more. The table above shows every field with an example value.

Can I use it without writing code?

Yes. Open the Actor page on Apify, fill in the input form (an example is already filled in), press Start, and download the results as JSON, CSV, Excel or HTML. You can also schedule runs there.

How much does it cost?

$3.40 per 1,000 results. You pay per result row. There is no separate charge per run. Higher Apify plans pay less per result, down to $2.38 per 1,000. A new Apify account gets $5 of platform credit every month.

Links

Top comments (0)