Skip to content

CityFurniture/html-pdf-node

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

44 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

@cityfurniture/city-html-pdf

A robust HTML to PDF conversion utility designed for Node.js applications.

Important: This utility facilitates the conversion of HTML content or public URLs to PDF format, seamlessly integrating with Node.js environments.

For more information, visit the repository: https://github.com/CityFurniture/html-pdf-node

Installation

To get started with @cityfurniture/city-html-pdf, install the package using npm:

npm install @cityfurniture/city-html-pdf

Usage

To convert HTML content to a PDF file, utilize the generatePdf method as follows:

const cityHtmlToPdf = require('@cityfurniture/city-html-pdf')

let options = { format: 'A4' }

let file = { content: '<h1>Welcome to @cityfurniture/city-html-pdf</h1>' }
// Alternatively, you can convert a public URL to PDF:
// let file = { url: "https://example.com" };

cityHtmlToPdf.generatePdf(file, options).then((pdfBuffer) => {
  console.log('PDF Buffer:-', pdfBuffer)
})

generatePdf ( [file], [options], [callback] )

Parameters

file <Object> File object should have one of the following properties:

  • url <string> Any public url of the PDF.
  • content<string> HTML file content of the PDF.

options <Object> Options object should have the following properties:

  • args <Array<string>> Additional arguments to pass to the browser instance. The list of Chromium flags can be found here. This options will overwrite the default arguments. The default arguments are ['--no-sandbox', '--disable-setuid-sandbox'].
  • path <string> The file path to save the PDF to. If path is a relative path, then it is resolved to current working directory. If no path is provided, the PDF won't be saved anywhere.
  • scale <number> Scale of the webpage rendering. Defaults to 1. Scale amount must be between 0.1 and 2.
  • displayHeaderFooter <boolean> Display header and footer. Defaults to false.
  • headerTemplate <string> HTML template to print the header. Should be valid HTML markup with following classes used to inject printing values into them:
    • date formatted date
    • title document title
    • url document location
    • pageNumber current page number
    • totalPages total pages in the document
  • footerTemplate <string> HTML template to print the footer. Should use the same format as the headerTemplate.
  • printBackground <boolean> Print background graphics. Defaults to false.
  • landscape <boolean> Paper orientation. Defaults to false.
  • pageRanges <string> Paper ranges to print, e.g., '1-5, 8, 11-13'. Defaults to the empty string, which means print all pages.
  • format <string> Paper format. If set, takes priority over width or height options. Defaults to 'Letter'.
  • width <string|number> Paper width, accepts values labeled with units.
  • height <string|number> Paper height, accepts values labeled with units.
  • margin <Object> Paper margins, defaults to none.
    • top <string|number> Top margin, accepts values labeled with units.
    • right <string|number> Right margin, accepts values labeled with units.
    • bottom <string|number> Bottom margin, accepts values labeled with units.
    • left <string|number> Left margin, accepts values labeled with units.
  • preferCSSPageSize <boolean> Give any CSS @page size declared in the page priority over what is declared in width and height or format options. Defaults to false, which will scale the content to fit the paper size.

Return value

Promise which resolves with PDF buffer.

generatePdfs ( [files], [options], [callback] )

Parameters

files <Array<Object>> File object should have one of the follwing properties:

  • url <string> Any public url of the PDF.
  • content<string> HTML file content of the PDF.

options <Object> Options object should have the following properties:

  • args <Array<string>> Additional arguments to pass to the browser instance. The list of Chromium flags can be found here. This options will overwrite the default arguments. The default arguments are ['--no-sandbox', '--disable-setuid-sandbox'].
  • path <string> The file path to save the PDF to. If path is a relative path, then it is resolved to current working directory. If no path is provided, the PDF won't be saved anywhere.
  • scale <number> Scale of the webpage rendering. Defaults to 1. Scale amount must be between 0.1 and 2.
  • displayHeaderFooter <boolean> Display header and footer. Defaults to false.
  • headerTemplate <string> HTML template to print the header. Should be valid HTML markup with following classes used to inject printing values into them:
    • date formatted date
    • title document title
    • url document location
    • pageNumber current page number
    • totalPages total pages in the document
  • footerTemplate <string> HTML template to print the footer. Should use the same format as the headerTemplate.
  • printBackground <boolean> Print background graphics. Defaults to false.
  • landscape <boolean> Paper orientation. Defaults to false.
  • pageRanges <string> Paper ranges to print, e.g., '1-5, 8, 11-13'. Defaults to the empty string, which means print all pages.
  • format <string> Paper format. If set, takes priority over width or height options. Defaults to 'Letter'.
  • width <string|number> Paper width, accepts values labeled with units.
  • height <string|number> Paper height, accepts values labeled with units.
  • margin <Object> Paper margins, defaults to none.
    • top <string|number> Top margin, accepts values labeled with units.
    • right <string|number> Right margin, accepts values labeled with units.
    • bottom <string|number> Bottom margin, accepts values labeled with units.
    • left <string|number> Left margin, accepts values labeled with units.
  • preferCSSPageSize <boolean> Give any CSS @page size declared in the page priority over what is declared in width and height or format options. Defaults to false, which will scale the content to fit the paper size.

Return value

Promise which resolves with array of object which contains file objects with PDF buffers.

Example:

let options = { format: 'A4' }
let file = [{ url: 'https://example.com', name: 'example.pdf' }]

generatePdfs(file, options).then((output) => {
  console.log('PDF Buffer:-', output) // PDF Buffer:- [{url: "https://example.com", name: "example.pdf", buffer: <PDF buffer>}]
})

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 74.0%
  • HTML 26.0%