Package Exports
- html2pdfconverter-sdk
Readme
๐ HTML2PDFConverter SDK for Node.js
๐ Convert HTML, URLs, or uploaded
.htmlfiles into high-quality PDFs effortlessly โ powered by the HTML2PDFConverter API.
๐งฉ Overview
The HTML2PDFConverter SDK provides a simple, reliable interface for Node.js developers to integrate PDF conversion into their apps, scripts, and serverless functions.
With a single function call, you can:
- Convert HTML strings or public URLs into PDFs.
- Stream and upload large HTML files (up to 100MB).
- Automatically wait for the job to complete.
- Get the finished PDF as a
Bufferor auto-save to disk. - Optionally receive a
jobIdfor asynchronous webhook processing.
โ๏ธ Installation
npm install html2pdfconverter-sdkor with Yarn:
yarn add html2pdfconverter-sdk๐ Authentication
Every request must include your API key:
x-api-key: <YOUR_API_KEY>๐ Quick Start
Convert inline HTML directly to a PDF buffer and save it locally:
import { PdfClient } from "html2pdfconverter-sdk";
import fs from "fs";
const client = new PdfClient({
apiKey: process.env.PDF_API_KEY!,
});
(async () => {
const pdf = await client.convert({
html: "<h1>Hello PDF!</h1>",
pdfOptions: { format: "A4", printBackground: true },
});
fs.writeFileSync("output.pdf", pdf);
console.log("โ
PDF saved as output.pdf");
})();๐งฑ Usage Examples
const pdf = await client.convert({
url: "https://example.com/invoice",
pdfOptions: { format: "A4" },
saveTo: "invoice.pdf",
});
console.log("โ
Invoice downloaded");2๏ธโฃ Convert large HTML file via multipart upload
await client.convert({
filePath: "./big-report.html",
pdfOptions: { format: "A4", landscape: true },
saveTo: "./report.pdf",
});3๏ธโฃ Asynchronous conversion with webhook
await client.convert({
filePath: "./long.html",
pdfOptions: { format: "A4" },
webhookUrl: "https://yourapp.com/webhooks/pdf",
});
console.log("Job queued โ will notify via webhook.");4๏ธโฃ Check job status manually
const pdf = await client.getJob("d7c3f1...");
fs.writeFileSync("downloaded.pdf", pdf);
console.log("โ
PDF downloaded via getJob");5๏ธโฃ Verify webhook authenticity
import express from "express";
import bodyParser from "body-parser";
import { PdfClient } from "html2pdfconverter-sdk";
const client = new PdfClient({
apiKey: process.env.PDF_API_KEY!,
webhookSecret: process.env.PDF_WEBHOOK_SECRET!,
});
const app = express();
app.post(
"/webhooks/pdf",
bodyParser.raw({ type: "application/json" }),
(req, res) => {
try {
const signature = req.headers["x-pdf-service-signature"] as string;
const event = client.verifyWebhook(req.body, signature);
console.log("โ
Webhook verified:", event);
res.sendStatus(200);
} catch (err) {
console.error("โ Invalid signature:", err);
res.sendStatus(401);
}
}
);๐งฐ Options Reference
| Option | Type | Required | Description |
|---|---|---|---|
html |
string |
โ๏ธ | Raw HTML string to convert |
url |
string |
โ๏ธ | Publicly accessible webpage |
filePath |
string |
โ๏ธ | Path to .html file (for large uploads) |
pdfOptions |
object |
โ๏ธ | Puppeteer-like PDF options (e.g. format, margin, etc.) |
webhookUrl |
string |
โ๏ธ | Optional webhook for async notification |
pollIntervalMs |
number |
โ๏ธ | How often to poll job status (default 2000) |
timeoutMs |
number |
โ๏ธ | Max time to wait for completion (default 180000) |
saveTo |
string |
โ๏ธ | Save output PDF to path (returns file path instead of buffer) |
๐ฆ Returned Object
When webhookUrl is provided:
- Returns
jobId: string
When conversion completes successfully (and webhookUrl is not provided):
- If
saveTois not set โ returns aBuffer. - If
saveTois set โ returns the file path. - Throws on failure or timeout.
๐ฌ Error Handling
The SDK throws clear, typed errors:
try {
await client.convert({ html: "<bad>" });
} catch (err) {
console.error("โ Conversion failed:", err.message);
}Common errors:
- Unauthorized: invalid API key
- File size exceeds plan limit
- Conversion timed out
- PDF conversion failed: Rendering error
โก Performance Tips
โ Use multipart upload for HTML >5 MB โ Optimize images and fonts in HTML โ Prefer hosted URLs for heavy assets โ Use webhooks for long-running jobs โ Enable gzip compression on your client
๐ Plan Limits (per plan)
| Plan | Max HTML Size | Max Timeout |
|---|---|---|
| Free | 5 MB | 30 s |
| Basic | 10 MB | 5 min |
| Standard | 25 MB | 10 min |
| Premium | 50 MB | 15 min |
| Growth | 75 MB | 15 min |
| Scale | 100 MB | 15 min |
| ProMax | 100 MB | 15 min |
| Enterprise | 100 MB | 15 min |
๐งฉ TypeScript Support
This SDK is written in TypeScript and ships with full types.
import { PdfClient } from "html2pdfconverter-sdk";๐งโ๐ป Example CLI usage
You can even build a quick CLI:
npx html2pdfconverter-sdk convert ./file.html --save ./output.pdf๐งโ๐ซ FAQ
Q: Can I convert local HTML files with images?
Yes, but ensure images are accessible or embedded as base64.
Q: How long are PDFs hosted?
Files are available for a limited time via signed URLs (expires in 1 hour by default).
Q: What about rate limits?
Up to 100 requests per 15 minutes by default.
Q: Can I use it in frontend apps?
We recommend server-side usage since API keys must be kept secret.
๐งพ License
MIT ยฉ HTML2PDFConverter.
๐ Contribute
Issues and pull requests welcome at ๐ https://github.com/html2pdfconverter/html2pdfconverter-sdk