JSPM

html2pdfconverter-sdk

1.0.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 1
  • Score
    100M100P100Q12659F
  • License MIT

Official Node.js SDK for the HTML2PDFConverter API

Package Exports

  • html2pdfconverter-sdk

Readme

๐Ÿ“„ HTML2PDFConverter SDK for Node.js

npm version License: MIT Node.js CI Downloads

๐Ÿš€ Convert HTML, URLs, or uploaded .html files 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 Buffer or auto-save to disk.
  • Optionally receive a jobId for asynchronous webhook processing.

โš™๏ธ Installation

npm install html2pdfconverter-sdk

or 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

๐Ÿงฑ 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 saveTo is not set โ†’ returns a Buffer.
  • If saveTo is 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
Starter 10 MB 60 s
Pro 25 MB 2 min
Scale 50 MB 5 min
Enterprise 100 MB 5 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/sdk