Node.js bindings for the Api2Pdf REST API.
Api2Pdf.com is a powerful REST API for document generation, file conversion, and automated content extraction in Node.js applications. It supports HTML to PDF, URL to PDF, HTML to image, URL to image, Microsoft Office document conversion, email and image file conversion, PDF page extraction, PDF password protection, file zipping, barcode and QR code generation, markdown conversion, structured PDF data extraction, and image previews or thumbnails for PDF, office, and email files. Api2Pdf is built on proven engines and libraries including wkhtmltopdf, Headless Chrome, PdfSharp, LibreOffice, and related tools to provide reliable PDF generation, document processing, and file transformation workflows through a single API.
The package preserves the classic flat method names from earlier api2pdf.node releases while expanding coverage to the current Api2Pdf Swagger v2 surface.
npm install api2pdfCreate an account at portal.api2pdf.com to get your API key.
const Api2Pdf = require("api2pdf")
async function run() {
const client = new Api2Pdf("YOUR-API-KEY")
const result = await client.chromeHtmlToPdf("<html><body><h1>Hello, world!</h1></body></html>")
if (result.Success) {
console.log(result.FileUrl)
} else {
console.log(result.Error)
}
}
run().catch(console.error)The default constructor uses https://v2.api2pdf.com.
If you want to route requests to a different Api2Pdf domain, pass a full base URL:
const Api2Pdf = require("api2pdf")
const client = new Api2Pdf("YOUR-API-KEY", "https://your-custom-domain.api2pdf.com")The package also exposes constants for common base URLs:
const Api2Pdf = require("api2pdf")
const client = new Api2Pdf("YOUR-API-KEY", Api2Pdf.BaseUrls.V2Xl)v2-xl.api2pdf.com provides much larger compute resources and is intended for heavier workloads, with additional cost compared to the default cluster.
Legacy hostname and port construction still works:
const client = new Api2Pdf("YOUR-API-KEY", "v2.api2pdf.com", 443)Most API methods return the standard Api2Pdf JSON payload unless you opt into binary output.
{
"FileUrl": "https://link-to-file-available-for-24-hours",
"MbOut": 0.08830547332763672,
"Cost": 0.00017251586914062501,
"Success": true,
"Error": null,
"ResponseId": "6e46637a-650d-46d5-af0b-3d7831baccbb"
}Important fields:
Success: whether the request succeeded.Error: error text when the request fails.FileUrl: URL of the generated file when the API returns standard JSON.ResponseId: identifier used byutilityDelete(...).
When outputBinary: true is supported and enabled, the method resolves to a Node.js Buffer instead of JSON.
Most flat methods accept an optional options object with these common properties:
filename: set the output file name.inline:trueto open in the browser,falseto trigger download behavior.useCustomStorageandstorage: send the output directly to your own storage target.outputBinary: when the endpoint supports it, request binary content instead of the standard JSON payload.extraHTTPHeaders: forward custom headers when Api2Pdf fetches a source URL.
Example custom storage configuration:
const result = await client.chromeHtmlToPdf("<p>Hello World</p>", {
useCustomStorage: true,
storage: {
method: "PUT",
url: "https://your-presigned-upload-url"
}
})const htmlPdf = await client.chromeHtmlToPdf("<p>Hello World</p>", {
options: {
delay: 3000,
displayHeaderFooter: true,
headerTemplate: "<div style=\"font-size:12px;\">Header</div>",
footerTemplate: "<div style=\"font-size:12px;\">Footer</div>",
landscape: true,
preferCSSPageSize: true
}
})
const urlPdf = await client.chromeUrlToPdf("https://www.api2pdf.com", {
extraHTTPHeaders: {
Authorization: "Bearer token-for-the-source-site"
},
options: {
puppeteerWaitForMethod: "WaitForNavigation",
puppeteerWaitForValue: "Load"
}
})const result = await client.chromeMarkdownToPdf("# Invoice\n\nThis PDF was generated from markdown.")const htmlImage = await client.chromeHtmlToImage("<p>Hello image</p>", {
options: {
fullPage: true,
viewPortOptions: {
width: 1440,
height: 900
}
}
})
const urlImage = await client.chromeUrlToImage("https://www.api2pdf.com")
const markdownImage = await client.chromeMarkdownToImage("# Screenshot\n\nGenerated from markdown.")const htmlPdf = await client.wkHtmlToPdf("<p>Hello World</p>", {
enableToc: true,
options: {
orientation: "landscape",
pageSize: "Letter"
},
tocOptions: {
disableDottedLines: "true"
}
})
const urlPdf = await client.wkUrlToPdf("https://www.api2pdf.com")For advanced wkhtmltopdf options, see the Api2Pdf wkhtmltopdf documentation.
Use LibreOffice endpoints for file and Office conversions.
Convert a file URL to PDF:
const result = await client.libreOfficeAnyToPdf(
"https://www.api2pdf.com/wp-content/themes/api2pdf/assets/samples/sample-word-doc.docx"
)Generate a thumbnail:
const result = await client.libreOfficeThumbnail(
"https://www.api2pdf.com/wp-content/themes/api2pdf/assets/samples/sample-word-doc.docx"
)Convert HTML or a URL to DOCX or XLSX:
const docx = await client.libreOfficeHtmlToDocx("<html><body><h1>Hello Word</h1></body></html>")
const xlsx = await client.libreOfficeHtmlToXlsx("https://www.api2pdf.com/wp-content/uploads/2021/01/sampleTables.html")For compatibility with earlier releases, the legacy libreOfficePdfToHtml(...) flat method is still available.
Convert a file URL to markdown:
const result = await client.markitdownToMarkdown("https://example.com/sample.docx")Extract structured content from a PDF URL:
const json = await client.openDataLoaderPdfToJson("https://example.com/sample.pdf")
const markdown = await client.openDataLoaderPdfToMarkdown("https://example.com/sample.pdf")
const html = await client.openDataLoaderPdfToHtml("https://example.com/sample.pdf")Merge PDFs:
const result = await client.pdfsharpMerge([
"https://LINK-TO-PDF-1",
"https://LINK-TO-PDF-2"
])Set a password:
const result = await client.pdfsharpAddPassword(
"https://LINK-TO-PDF",
"user-password",
"owner-password"
)Extract a page range:
const result = await client.pdfsharpExtractPages(
"https://LINK-TO-PDF",
0,
2
)For backward compatibility, legacy flat methods such as pdfsharpAddBookmarks(...) and pdfsharpCompress(...) remain available.
Create a zip from multiple files:
const zipBuffer = await client.zipGenerate(
[
{
url: "https://example.com/report.pdf",
fileName: "docs/report.pdf"
},
{
url: "https://example.com/image.png",
fileName: "images/image.png"
}
],
{
outputBinary: true
}
)zipFiles(...) is provided as an alias to zipGenerate(...).
Generate a barcode or QR code:
const imageBuffer = await client.zebraGenerateBarcode("QR_CODE", "https://www.api2pdf.com", {
width: 300,
height: 300,
showLabel: false,
outputBinary: true
})zebraGenerate(...) is provided as an alias to zebraGenerateBarcode(...).
For supported Zebra format values, see the Api2Pdf Zebra documentation.
Delete a generated file:
const pdf = await client.chromeHtmlToPdf("<p>Hello World</p>")
await client.utilityDelete(pdf.ResponseId)Check status or remaining balance:
const status = await client.utilityStatus()
const balance = await client.utilityBalance()Save binary output to disk:
const fs = require("node:fs/promises")
const pdfBuffer = await client.chromeHtmlToPdf("<p>Hello World</p>", {
outputBinary: true
})
await fs.writeFile("output.pdf", pdfBuffer)Use JSON output when you want a hosted file URL instead:
const result = await client.chromeHtmlToPdf("<p>Hello World</p>")
console.log(result.FileUrl)The repo uses a lightweight CommonJS layout:
index.jsas the package entrypointsrc/for the shipping librarytest/for regression and contract-style tests
Run the test suite with:
npm testPreview the published package contents with:
npm run pack:check