Skip to content

Node lib to converts HTML with property binding or url to PDF files.

License

Notifications You must be signed in to change notification settings

MyUnisoft/HTML-PDF

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Nov 27, 2023
b39d7ac Β· Nov 27, 2023
Nov 24, 2023
Sep 2, 2022
Mar 11, 2022
Sep 2, 2022
Feb 20, 2021
Dec 28, 2021
Feb 20, 2021
Apr 6, 2021
Feb 20, 2021
Sep 2, 2022
Oct 16, 2021
Nov 27, 2023
Nov 27, 2023
Sep 29, 2021
Oct 16, 2021

Repository files navigation

HTML-PDF

Node lib to converts HTML with property binding or url to PDF files.

npm version license size

🚧 Requirements

πŸš€ Getting Started

This package is available in the Node Package Repository and can be easily installed with npm or yarn

$ npm i @myunisoft/html-to-pdf
# or
$ yarn add @myunisoft/html-to-pdf

πŸ“š Usage

async function main() {
  const browser = await initBrowser();
  let readable;

  try {
    readable = Readable.from(generatePDF(browser, [{ content: html }], pdfOptions));
  }
  finally {
    await terminateBrowser(browser);
  }

  return readable;
}

main().catch(console.error);

API

initBrowser(options?: PuppeteerLaunchOptions): Promise< Browser >
import { initBrowser } from "@myunisoft/html-to-pdf";

const browser = await initBrowser();

Options payload is described by the following TypeScript interface:

import {
  LaunchOptions,
  BrowserLaunchArgumentOptions,
  BrowserConnectOptions,
  Product
} from "puppeteer";

type PuppeteerLaunchOptions = LaunchOptions & BrowserLaunchArgumentOptions & BrowserConnectOptions & {
    product?: Product;
    extraPrefsFirefox?: Record<string, unknown>;
}
generatePDF(browser: Browser, files: pdfFile[], options?: PuppeteerPDFOptions): AsyncIterableIterator< Buffer >

files and options arguments are described with the following TypeScript interface/type:

interface pdfFile {
  content?: string,
  url?: string
}

type PuppeteerPDFOptions = PDFOptions & { paginationOffset?: number };
import * as pdf from "@myunisoft/html-to-pdf";

const browser = await pdf.initBrowser();

const pdfOptions = {};
const content = "..HTML HERE..";

const readable = Readable.from(
  pdf.generatePDF(browser, [{ content }], pdfOptions)
);
// Do the work with readable (pipe to an http response for example).
terminateBrowser(browser: Browser): Promise< void >
import * as pdf from "@myunisoft/html-to-pdf";

const browser = await pdf.initBrowser();

try {
  // DO THE WORK HERE
}
finally {
  await pdf.terminateBrowser(browser);
}

Contributors ✨

All Contributors

Thanks goes to these wonderful people (emoji key):


Nicolas Hallaert

πŸ’» πŸ“– ⚠️

Gentilhomme

πŸ›‘οΈ πŸ‘€ πŸ“–

PierreD

πŸ’»

License

MIT