npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

pdfapi-client

v1.0.2

Published

Node.js client for PDFApi.dev - HTML to PDF conversion service

Readme

Node.js Client for pdfapi.dev

A Node.js/TypeScript client library for pdfapi.dev - HTML to PDF conversion service that supports:

  • CSS styling
  • Custom headers and footers
  • Images and other assets
  • Multiple page formats
  • Configurable margins and scaling

Installation

npm install pdfapi-client
# or
yarn add pdfapi-client

Quick Start

import { PDFApiClient, PageFormat } from 'pdfapi-client';
import { writeFileSync } from 'fs';

// Initialize the client
const client = new PDFApiClient('your-api-key');

// Convert HTML to PDF
const pdf = await client.convertHtml('<h1>Hello World</h1>');

// Save to file
writeFileSync('output.pdf', pdf);

Examples

HTML with CSS

const html = `
<html>
    <head>
        <link rel="stylesheet" href="style.css">
    </head>
    <body>
        <h1>Styled Document</h1>
        <p>This is a styled paragraph.</p>
    </body>
</html>
`;

const css = 'h1 { color: navy; } p { color: gray; }';

const pdf = await client.convertHtml(
    html,
    { 'style.css': css },
    { format: PageFormat.A4 }
);

With Header and Footer

import { readFileSync } from 'fs';

// Header template - must be a complete HTML document
const header = `
<html>
<head>
    <style>
        body {
            font-family: Arial, sans-serif;
            font-size: 10px;
            margin: 0;
            padding: 8px;
        }
        .header {
            text-align: center;
            border-bottom: 1px solid #ddd;
        }
    </style>
</head>
<body>
    <div class="header">My Document - {page}</div>
</body>
</html>`;

// Footer template - must be a complete HTML document
const footer = `
<html>
<head>
    <style>
        body {
            font-family: Arial, sans-serif;
            font-size: 10px;
            margin: 0;
            padding: 8px;
        }
        .footer {
            text-align: right;
            color: #666;
        }
    </style>
</head>
<body>
    <div class="footer">Page {page} of {pages}</div>
</body>
</html>`;

const pdf = await client.convertHtml(
    '<h1>Document with Header and Footer</h1>',
    {},
    {
        format: PageFormat.A4,
        margin: { top: 40, bottom: 40, left: 20, right: 20 },
        headerFile: header,
        footerFile: footer
    }
);

// You can also load header/footer from files
const headerFromFile = readFileSync('header.html', 'utf-8');
const footerFromFile = readFileSync('footer.html', 'utf-8');

Note: Both header and footer templates must be complete HTML documents with <html>, <head>, and <body> tags. The following variables are available in templates:

  • {page} - current page number
  • {pages} - total number of pages
  • {title} - document title (if set in the main HTML)
  • {url} - document URL (if applicable)

With Images

import { readFileSync } from 'fs';

const html = `
<html>
    <body>
        <h1>Document with Image</h1>
        <img src="image.png" alt="My Image">
    </body>
</html>
`;

const imageBuffer = readFileSync('path/to/image.png');

const pdf = await client.convertHtml(
    html,
    { 'image.png': imageBuffer },
    { format: PageFormat.A4 }
);

Configuration Options

Page Formats

Available page formats:

  • PageFormat.A4 (default)
  • PageFormat.A3
  • PageFormat.Letter
  • PageFormat.Legal
  • And more...

Margins

Margins can be specified in pixels:

const config = {
    margin: {
        top: 20,
        bottom: 20,
        left: 20,
        right: 20
    }
};

Other Options

  • scale: Scale factor for the output (default: 1.0)
  • landscape: Whether to use landscape orientation (default: false)

Input Types

The library accepts various input types:

  • Strings (for direct content)
  • Buffers (for binary content)
  • Readable streams (for streaming content)

This applies to HTML content, CSS, images, and header/footer files.

Error Handling

import { PDFApiClient, ConversionError, ProblemDetail } from 'pdfapi-client';

try {
    const pdf = await client.convertHtml('<h1>Test</h1>');
} catch (error) {
    if (error instanceof ConversionError) {
        console.error(`Conversion failed: ${error.message}`);
        console.error(`Status: ${error.status}`);
        
        // Detailed problem information
        const problem: ProblemDetail = error.problem;
        console.error(`Type: ${problem.type}`);
        console.error(`Title: ${problem.title}`);
        console.error(`Detail: ${problem.detail}`);
        console.error(`Instance: ${problem.instance}`);
    }
}

The error object contains detailed information about what went wrong:

  • status: HTTP status code (e.g., 400, 403, 404)
  • problem.type: A URI reference that identifies the problem type
  • problem.title: A short, human-readable summary of the problem
  • problem.detail: A detailed explanation specific to this occurrence of the problem
  • problem.instance: A URI reference that identifies the specific occurrence of the problem
  • problem.properties: Additional properties about the error (if any)

License

Copyright 2024 pdfapi.dev

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Development

Releasing New Version

  1. Update version:
npm version patch  # for bug fixes (1.0.0 -> 1.0.1)
npm version minor  # for new features (1.0.0 -> 1.1.0)
npm version major  # for breaking changes (1.0.0 -> 2.0.0)
  1. Push with tags:
git push --follow-tags

The GitHub Action will automatically:

  • Run tests (if PDFAPI_KEY is set)
  • Build the package
  • Publish to npm