# HTML to PDF Converter (`rainminer/html-to-pdf-converter`) Actor

Convert raw HTML or web page URLs into downloadable PDF files using a real browser. Render CSS, images, tables, invoices, reports, and dynamic layouts, then save the generated PDF to the Apify Key-Value Store with dataset metadata.

- **URL**: https://apify.com/rainminer/html-to-pdf-converter.md
- **Developed by:** [rainminer](https://apify.com/rainminer) (community)
- **Categories:** Automation, Developer tools, AI
- **Features:** Limited permissions
- **Stats:** 50 total users, 9 monthly users, 100.0% runs succeeded, 0 bookmarks
- **User rating**: No ratings yet

## Pricing

from $9.99 / 1,000 pdfs

This Actor is paid per event. You are not charged for the Apify platform usage, but only a fixed price for specific events.

Learn more: https://docs.apify.com/actors/running/actors-in-store.md#pay-per-event

## What's an Apify Actor?

An Actor is a serverless cloud program that runs on the Apify platform. It has two run modes.
In Batch mode, an Actor accepts a well-defined JSON input, performs an action which can take anything from a few seconds to a few hours,
and optionally produces a well-defined JSON output, datasets with results, or files in key-value store.
In Standby mode, an Actor provides a web server which can be used as a website, API, or an MCP server.

Apify vocabulary and the platform model are defined once, in the agent quickstart at https://apify.com/agents.md.

## How to integrate an Actor?

If asked about integration, you help developers integrate Actors into their projects.
You adapt to their stack and deliver integrations that are safe, well-documented, and production-ready.

Do not guess an integration path. Every one of them is in the agent quickstart at https://apify.com/agents.md: the Apify MCP server, Agent Skills with the Apify CLI, the JavaScript and Python clients, the REST API, and the account-free path for an agent with no human to sign in. It also carries the rule on stating cost before the first paid run.

For examples already wired to this Actor's own input schema, see the [API](#api) section below.

Each client library has reference documentation the quickstart does not restate: [JavaScript/TypeScript](https://docs.apify.com/api/client/js/docs.md) (`npm install apify-client`) and [Python](https://docs.apify.com/api/client/python/docs.md) (`pip install apify-client`).

# README

## HTML to PDF

Convert raw HTML or a public web page URL into a downloadable PDF with a temporary Playwright browser.

### What It Does

HTML to PDF renders your input in headless Chrome, generates a PDF with Playwright `page.pdf`, saves the file to the Apify Key-Value Store, and pushes one dataset item with the file name, storage key, PDF URL, byte size, source type, and page title when available.

Raw HTML is the primary input. If `html` is empty, the Actor loads `url` instead.

### Input

```json
{
  "html": "<!doctype html><html><head><title>Invoice</title></head><body><h1>Invoice</h1></body></html>",
  "fileName": "invoice.pdf",
  "format": "A4",
  "printBackground": true,
  "landscape": false,
  "margin": {
    "top": "0.4in",
    "right": "0.4in",
    "bottom": "0.4in",
    "left": "0.4in"
  },
  "viewportWidth": 1280,
  "viewportHeight": 720,
  "waitForMillis": 0
}
```

Use `url` instead of `html` when you want to render a live page:

```json
{
  "url": "https://example.com",
  "fileName": "example.pdf"
}
```

### Output

The PDF is stored in the default Key-Value Store under `fileName`, defaulting to `output.pdf`.

Example dataset item:

```json
{
  "fileName": "invoice.pdf",
  "pdfUrl": "https://api.apify.com/v2/key-value-stores/{storeId}/records/invoice.pdf",
  "key": "invoice.pdf",
  "bytes": 18421,
  "sourceType": "html",
  "title": "Invoice"
}
```

### Notes

- `printBackground` defaults to `true` so CSS backgrounds are included.
- `format` defaults to `A4`.
- `waitForMillis` can delay PDF generation for client-side rendering, up to 60 seconds.
- File names are sanitized to valid Key-Value Store record keys and get a `.pdf` suffix if missing.

# Actor input Schema

## `html` (type: `string`):

Raw HTML to render into a PDF. If both HTML and URL are provided, HTML is used.

## `url` (type: `string`):

Optional page URL to render when HTML is not provided.

## `fileName` (type: `string`):

Key-Value Store record name for the generated PDF.

## `format` (type: `string`):

Paper format passed to Playwright page.pdf.

## `printBackground` (type: `boolean`):

Include CSS backgrounds in the PDF.

## `landscape` (type: `boolean`):

Render the PDF in landscape orientation.

## `scale` (type: `number`):

Scale of the webpage rendering passed to Playwright page.pdf (0.1–2). Invoices commonly use 1.25.

## `margin` (type: `object`):

Optional PDF margins. Values can use units like px, in, cm, or mm.

## `viewportWidth` (type: `integer`):

Browser viewport width in pixels.

## `viewportHeight` (type: `integer`):

Browser viewport height in pixels.

## `waitForMillis` (type: `integer`):

Extra time to wait before generating the PDF, useful for client-side rendering.

## Actor input object example

```json
{
  "html": "<!doctype html><html><head><title>Example PDF</title></head><body><h1>Hello from Apify</h1><p>This HTML will be rendered as a PDF.</p></body></html>",
  "url": "https://example.com",
  "fileName": "output.pdf",
  "format": "A4",
  "printBackground": true,
  "landscape": false,
  "scale": 1,
  "margin": {
    "top": "0.4in",
    "right": "0.4in",
    "bottom": "0.4in",
    "left": "0.4in"
  },
  "viewportWidth": 1280,
  "viewportHeight": 720,
  "waitForMillis": 0
}
```

# Actor output Schema

## `overview` (type: `string`):

No description

## `keyValueStore` (type: `string`):

No description

# API

You can run this Actor programmatically using our API. Below are code examples in JavaScript, Python, and CLI, as well as the OpenAPI specification and MCP server setup.

## JavaScript example

```javascript
import { ApifyClient } from 'apify-client';

// Initialize the ApifyClient with your Apify API token
// Replace the '<YOUR_API_TOKEN>' with your token
const client = new ApifyClient({
    token: '<YOUR_API_TOKEN>',
});

// Prepare Actor input
const input = {
    "html": "<!doctype html><html><head><title>Example PDF</title></head><body><h1>Hello from Apify</h1><p>This HTML will be rendered as a PDF.</p></body></html>",
    "url": "https://example.com",
    "fileName": "output.pdf",
    "format": "A4",
    "printBackground": true,
    "landscape": false,
    "scale": 1,
    "margin": {
        "top": "0.4in",
        "right": "0.4in",
        "bottom": "0.4in",
        "left": "0.4in"
    },
    "viewportWidth": 1280,
    "viewportHeight": 720,
    "waitForMillis": 0
};

// Run the Actor and wait for it to finish
const run = await client.actor("rainminer/html-to-pdf-converter").call(input);

// Fetch and print Actor results from the run's dataset (if any)
console.log('Results from dataset');
console.log(`💾 Check your data here: https://console.apify.com/storage/datasets/${run.defaultDatasetId}`);
const { items } = await client.dataset(run.defaultDatasetId).listItems();
items.forEach((item) => {
    console.dir(item);
});

// 📚 Want to learn more 📖? Go to → https://docs.apify.com/api/client/js/docs

```

## Python example

```python
from apify_client import ApifyClient

# Initialize the ApifyClient with your Apify API token
# Replace '<YOUR_API_TOKEN>' with your token.
client = ApifyClient("<YOUR_API_TOKEN>")

# Prepare the Actor input
run_input = {
    "html": "<!doctype html><html><head><title>Example PDF</title></head><body><h1>Hello from Apify</h1><p>This HTML will be rendered as a PDF.</p></body></html>",
    "url": "https://example.com",
    "fileName": "output.pdf",
    "format": "A4",
    "printBackground": True,
    "landscape": False,
    "scale": 1,
    "margin": {
        "top": "0.4in",
        "right": "0.4in",
        "bottom": "0.4in",
        "left": "0.4in",
    },
    "viewportWidth": 1280,
    "viewportHeight": 720,
    "waitForMillis": 0,
}

# Run the Actor and wait for it to finish
run = client.actor("rainminer/html-to-pdf-converter").call(run_input=run_input)

# Fetch and print Actor results from the run's dataset (if there are any)
print(f"💾 Check your data here: https://console.apify.com/storage/datasets/{run.default_dataset_id}")
for item in client.dataset(run.default_dataset_id).iterate_items():
    print(item)

# 📚 Want to learn more 📖? Go to → https://docs.apify.com/api/client/python/docs/quick-start

```

## CLI example

```bash
echo '{
  "html": "<!doctype html><html><head><title>Example PDF</title></head><body><h1>Hello from Apify</h1><p>This HTML will be rendered as a PDF.</p></body></html>",
  "url": "https://example.com",
  "fileName": "output.pdf",
  "format": "A4",
  "printBackground": true,
  "landscape": false,
  "scale": 1,
  "margin": {
    "top": "0.4in",
    "right": "0.4in",
    "bottom": "0.4in",
    "left": "0.4in"
  },
  "viewportWidth": 1280,
  "viewportHeight": 720,
  "waitForMillis": 0
}' |
apify call rainminer/html-to-pdf-converter --silent --output-dataset

```

## MCP server setup

```json
{
    "mcpServers": {
        "apify": {
            "type": "http",
            "url": "https://mcp.apify.com/?tools=fetch-actor-details,rainminer/html-to-pdf-converter"
        }
    }
}
```

The hosted server signs you in with OAuth on first connect, so no API token belongs in this config. Clients without OAuth support can send an `Authorization: Bearer <APIFY_API_TOKEN>` header instead, using a token from API & Integrations in Apify Console (https://console.apify.com/settings/integrations).

## OpenAPI specification

Download the OpenAPI definition: https://api.apify.com/v2/actors/MrvnYR61iJ27FuJU0/builds/BVODZY35YzujdjwsF/openapi.json
