1.0.2 • Published 6 months ago

pdfapi-client v1.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
6 months ago

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
1.0.2

6 months ago

1.0.1

6 months ago