HTML to PDF API with Node.js / Python / PHP
Blog / HTML to PDF
HTML to PDF

HTML to PDF API with Node.js / Python / PHP

Demetra Theodorou · Apr 13, 2025

Generate PDFs from HTML in Node.js, Python, or PHP with the PDFGate API. Copy-paste code examples, output options, and no rendering engine to host.

With a cloud-based HTML to PDF API like PDFGate, you can convert dynamic HTML templates into high-quality, secure PDFs in seconds — no server maintenance, no rendering hacks.

This guide walks you through how to call the PDFGate API in Node.js, Python, and PHP, with copy-paste examples and integration tips.

Key takeaways

  • Call the PDFGate API from Node.js, Python, PHP, or any language that can POST JSON over HTTPS.
  • One request with your HTML (or a URL) and a page size returns a finished PDF, with no rendering engine to host.
  • Shape the output with pageSizeType, margins, orientation, headers, and footers.

What You’ll Need

Every request is a single POST to https://api.pdfgate.com/v1/generate/pdf with your API key passed as a Bearer token in the Authorization header. You send your HTML (or a URL) and a page size as JSON, and the response body is the binary PDF, so you save it to a file or stream it straight to the user. A non-200 status returns a JSON error you can log and handle.

Option 1: Node.js (Using Axios)

Install Axios

1npm install axios

Sample Code

1const axios = require('axios');2const fs = require('fs');34const html = `<html><body><h1>Hello PDFGate</h1></body></html>`;56axios.post('https://api.pdfgate.com/v1/generate/pdf', {7  html,8  pageSizeType: "a4"9}, {10  headers: {11    'Authorization': 'Bearer YOUR_API_KEY',12    'Content-Type': 'application/json'13  },14  responseType: 'arraybuffer'15})16.then(response => {17  fs.writeFileSync('output.pdf', response.data);18  console.log('PDF saved!');19})20.catch(error => {21  console.error('Error:', error.response?.data || error.message);22});

Option 2: Python (Using Requests)

Install Requests

1pip install requests

Sample Code

1import requests23html = "<html><body><h1>Hello PDFGate</h1></body></html>"45response = requests.post(6    "https://api.pdfgate.com/v1/generate/pdf",7    headers={8        "Authorization": "Bearer YOUR_API_KEY",9        "Content-Type": "application/json"10    },11    json={12        "html": html,13        "pageSizeType": "a4"14    }15)1617if response.status_code == 200:18    with open("output.pdf", "wb") as f:19        f.write(response.content)20    print("PDF saved successfully.")21else:22    print("Failed to generate PDF:", response.text)

Option 3: PHP (Using cURL)

Sample Code

1<?php2$html = "<html><body><h1>Hello PDFGate</h1></body></html>";34$payload = json_encode([5    "html" => $html,6    "pageSizeType" => "a4"7]);89$ch = curl_init("https://api.pdfgate.com/v1/generate/pdf");10curl_setopt($ch, CURLOPT_HTTPHEADER, [11    "Authorization: Bearer YOUR_API_KEY",12    "Content-Type: application/json"13]);14curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);15curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);16$response = curl_exec($ch);1718if ($response) {19    file_put_contents("output.pdf", $response);20    echo "PDF saved.";21} else {22    echo "Error: " . curl_error($ch);23}24curl_close($ch);25?>

Output Options You Can Customize

OptionDescription
pageSizeTypea0, a1, a2, a3, a4, a5, a6, ledger, tabloid, legal, letter
margine.g. “1cm”, “0.5cm”
landscapetrue for horizontal page orientation
header“<div>Header</div>”
footer“<div>Footer</div>”

Learn more about the customization options available with PDFGate’s HTML to PDF API.

Pro Tip: Use Templates with Data Injection

If you’re generating invoices, reports, or contracts, combine PDFGate with a template engine like:

  • Handlebars / EJS in Node.js
  • Jinja2 in Python
  • Blade in Laravel

This lets you pass real-time data into your HTML and render user-specific documents automatically.

In addition, if you prefer no code, the same generation runs inside automated workflows through our integrations.

Why PDFGate for HTML to PDF

FeaturePDFGate Advantage
HTML → PDFConvert HTML or URLs into pixel-perfect PDFs
SecureEncryption + no default file storage
FastRender in seconds, even at high volume
API-firstWorks in any language or framework
Easy setupCopy-paste integration across platforms

Frequently asked questions

Which languages can call the PDFGate API?

Any language that can make an HTTPS POST request. This guide shows Node.js with Axios, Python with Requests, and PHP with cURL, but the same request works from Go, Java, Ruby, C#, and others.

Do I need an API key?

Yes. Create a free PDFGate account, generate an API key from the dashboard, and pass it as a Bearer token in the Authorization header of each request.

Can I convert a live URL instead of an HTML string?

Yes. Swap the html field for a url field pointing at a publicly reachable page. PDFGate renders it in a browser engine and returns the finished PDF the same way.

How do I set the page size, margins, or orientation?

Pass options such as pageSizeType, margin, landscape, header, and footer in the request body. Standard sizes like A4 and Letter are supported, along with a custom page size of your own dimensions.

Does PDFGate store the generated file?

No. PDFGate does not store files by default. The API returns the PDF in the response for you to save or forward, so sensitive documents do not sit on a third-party server.

Start generating PDFs

If you want to generate PDFs from HTML using Node.js, Python, or PHP, PDFGate gives you a fast, secure, and reliable API with no infrastructure headaches.

👉 Start your free trial now at PDFGate.com— you’ll be converting your first HTML to PDF in less than 5 minutes.