JavaScript / TypeScript SDK
The official JavaScript SDK for PDFik allows seamless integration with Node.js (18+), browser, and Edge environments.
It includes native TypeScript types, ES Module (ESM) and CommonJS (CJS) compatibility, and relies on native fetch with zero runtime dependencies.
Installation
Install via your preferred package manager:
npm install @pdfik/client
# or
yarn add @pdfik/client
# or
pnpm add @pdfik/client
Quick Start
Convert Public URL to PDF
import { PdfikClient } from '@pdfik/client';
// Initialize the client
const client = new PdfikClient({
apiKey: 'sk_live_...' // Get your API key from the dashboard
});
async function generatePdf() {
try {
// 1. Submit the URL to be rendered
const job = await client.urlToPdf('https://example.com', {
options: {
format: 'A4',
landscape: false,
printBackground: true,
margin: { top: '10mm', right: '10mm', bottom: '10mm', left: '10mm' }
}
});
console.log(`Job created: ${job.jobId}. Waiting for rendering...`);
// 2. Poll until the job completes
const result = await client.waitForJob(job.jobId);
console.log(`Job finished! Pages: ${result.pagesCount}`);
// 3. Download the PDF bytes
const pdfBytes = await client.downloadPdf(job.jobId);
// Save to file (Node.js example)
const fs = require('fs');
fs.writeFileSync('output.pdf', pdfBytes);
console.log('PDF saved to output.pdf');
} catch (error) {
console.error('Failed to generate PDF:', error);
}
}
generatePdf();
Convert Raw HTML to PDF
const job = await client.htmlToPdf('<h1>Hello World</h1><p>Sent from PDFik SDK</p>', {
options: {
format: 'Letter',
displayHeaderFooter: true,
headerTemplate: "<span style='font-size: 10px;'>My Invoice Header</span>",
footerTemplate: "<span style='font-size: 10px;'>Page <span class='pageNumber'></span></span>"
}
});
Get the direct download URL
Get the direct API download URL for the generated PDF (requires the "X-API-Key" header to download):
const { downloadUrl } = await client.getFileUrl(job.jobId);
console.log(`Direct Download URL: ${downloadUrl}`);
API Reference
PdfikClient
constructor(config: { apiKey: string; baseUrl?: string })
Methods
urlToPdf(url, opts): Submits a job to convert a public URL to a PDF. Returns aJobCreatedResponse.htmlToPdf(html, opts): Submits a job to convert raw HTML markup to a PDF. Returns aJobCreatedResponse.getJob(jobId): Fetches the status of a specific job. Returns aJobStatusResponse.waitForJob(jobId, opts): Helper that pollsgetJobuntil the job isdoneorfailed. Throws aPdfikErrorif the job fails or times out.opts.timeoutMs(default:120000ms)opts.pollIntervalMs(default:2000ms)
getFileUrl(jobId): Returns the direct download URL for the PDF (requires the "X-API-Key" header).downloadPdf(jobId): Downloads and returns the PDF file as aUint8Array.