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

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

How to Convert HTML to PDF Using PDFGate in Your Favorite Language

If you’re looking to generate PDFs from HTML using your backend stack — whether it’s Node.js, Python, or PHP — you don’t need to set up your own rendering engine or rely on outdated libraries.

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.


🧠 What You’ll Need

• An API key from PDFGate (sign up free)

• Your server or backend environment (Node.js, Python, or PHP)

• An HTML string or URL you want to convert


📦 Option 1: Node.js (Using Axios)

✅ Install Axios

npm install axios

✅ Sample Code

const axios = require('axios');
const fs = require('fs');

const html = `<html><body><h1>Hello PDFGate</h1></body></html>`;

axios.post('https://api.pdfgate.com/v1/generate/pdf', {
  html,
  pageSizeType: "a4"
}, {
  headers: {
    'Authorization': 'Bearer YOUR_API_KEY',
    'Content-Type': 'application/json'
  },
  responseType: 'arraybuffer'
})
.then(response => {
  fs.writeFileSync('output.pdf', response.data);
  console.log('PDF saved!');
})
.catch(error => {
  console.error('Error:', error.response?.data || error.message);
});

🐍 Option 2: Python (Using Requests)

✅ Install Requests

pip install requests

✅ Sample Code

import requests

html = "<html><body><h1>Hello PDFGate</h1></body></html>"

response = requests.post(
    "https://api.pdfgate.com/v1/generate/pdf",
    headers={
        "Authorization": "Bearer YOUR_API_KEY",
        "Content-Type": "application/json"
    },
    json={
        "html": html,
        "pageSizeType": "a4"
    }
)

if response.status_code == 200:
    with open("output.pdf", "wb") as f:
        f.write(response.content)
    print("PDF saved successfully.")
else:
    print("Failed to generate PDF:", response.text)

🐘 Option 3: PHP (Using cURL)

✅ Sample Code

<?php
$html = "<html><body><h1>Hello PDFGate</h1></body></html>";

$payload = json_encode([
    "html" => $html,
    "pageSizeType" => "a4"
]);

$ch = curl_init("https://api.pdfgate.com/v1/generate/pdf");
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    "Authorization: Bearer YOUR_API_KEY",
    "Content-Type: application/json"
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
$response = curl_exec($ch);

if ($response) {
    file_put_contents("output.pdf", $response);
    echo "PDF saved.";
} else {
    echo "Error: " . curl_error($ch);
}
curl_close($ch);
?>

📄 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>”

💡 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.


✅ Summary

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

Ready to Go?

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.