1.1.5 • Published 7 months ago

html-export-pdf-cli v1.1.5

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

html-export-pdf-cli

NPM version NPM Downloads LICENSE GitHub stars

Render HTML to PDF(with outline) using Puppeteer.

Features

  • 🖥️ Converting HTML to PDF for .html, .htm, .xhtml file formats
  • 📑 Added PDF outline
  • ⚙️ Configurations for generating PDF with Puppeteer —— pdfOptions
  • 📂 Multi-file PDF generation
  • ⌨️ Developed in TypeScript
  • ⏬ Download web page

Installation

# Global installation
npm install -g html-export-pdf-cli

# Local installation
npm install --save-dev html-export-pdf-cli

The installed html-export-pdf-cli command is available.

Usage

Generating a PDF

html-export-pdf-cli ./index.html -o result.pdf

Generating two PDFs

html-export-pdf-cli ./index.html ./home.html
# or
html-export-pdf-cli -t ./index.html -t ./home.html
html-export-pdf-cli --inputs ./index.html --inputs ./home.html

Generating some PDFs

This Library uses glob to parse the path you enter, and its syntax refers to the glob documentation.

html-export-pdf-cli ./pdf/*.html

Outline

html-export-pdf-cli ./index.html --outlineTags h1,h2 -o index.pdf

Using outlineContainerSelector specify an outline container selector

html-export-pdf-cli ./index.html --outlineContainerSelector .VPDoc --outlineTags h1,h2 -o index.pdf

Additional Scripts

html-export-pdf-cli ./index.html --additionalScripts a.js --additionalScripts b.js -o index.pdf

Additional Styles

html-export-pdf-cli ./index.html --additionalStyles a.css --additionalStyles b.css -o index.pdf

PDF Options

You can customize all configuration parameters for Puppeteer to generate PDF except path.

PropertyModifiersTypeDescriptionDefault
footerTemplateoptionalstringHTML template for the print footer. Has the same constraints and support for special classes as PDFOptions.headerTemplate.
formatoptionalPaperFormatletter.
headerTemplateoptionalstringHTML template for the print header. Should be valid HTML with the following classes used to inject values into them:- date formatted print date- title document title- url document location- pageNumber current page number- totalPages total pages in the document
heightoptionalstring | numberSets the height of paper. You can pass in a number or a string with a unit.
landscapeoptionalbooleanWhether to print in landscape orientation.false
marginoptionalPDFMarginSet the PDF margins.undefined no margins are set.
omitBackgroundoptionalbooleanHides default white background and allows generating PDFs with transparency.false
pageRangesoptionalstringPaper ranges to print, e.g. 1-5, 8, 11-13.The empty string, which means all pages are printed.
preferCSSPageSizeoptionalbooleanGive any CSS @page size declared in the page priority over what is declared in the width or height or format option.false, which will scale the content to fit the paper size.
printBackgroundoptionalbooleanSet to true to print background graphics.false
scaleoptionalnumberScales the rendering of the web page. Amount must be between 0.1 and 2.1
timeoutoptionalnumberTimeout in milliseconds. Pass 0 to disable timeout.30_000
widthoptionalstring | numberSets the width of paper. You can pass in a number or a string with a unit.

Format

html-export-pdf-cli ./index.html -s A0 -o index.pdf
# or
html-export-pdf-cli ./index.html --pageSize A0 -o index.pdf

Margin

html-export-pdf-cli ./index.html -m top=10,bottom=10,left=10,right=10 -o index.pdf
# or
html-export-pdf-cli ./index.html --margin top=10,bottom=10,left=10,right=10 -o index.pdf

The order can be customized, even omitting a few.

html-export-pdf-cli ./index.html -m top=10 -o index.pdf
html-export-pdf-cli ./index.html -m right=10 -o index.pdf
html-export-pdf-cli ./index.html -m right=10,left=10 -o index.pdf
html-export-pdf-cli ./index.html -m top=10,left=10 -o index.pdf

Page Ranges

html-export-pdf-cli ./index.html --pageRanges 1-5 -o index.pdf

OmitBackground

html-export-pdf-cli ./index.html --omitBackground -o index.pdf

etc.

PaperFormat

PDFMargin

PDF File Name Convention

Rules for automatically generating PDF file names.

URLFilename
https://www.example.com/www.example.com.pdf
https://www.example.com:80/www.example.com.pdf
https://www.example.com/resourceresource.pdf
https://www.example.com/resource.extensionresource.pdf
https://www.example.com/path/path.pdf
https://www.example.com/path/to/path_to.pdf
https://www.example.com/path/to/resourceresource.pdf
https://www.example.com/path/to/resource.extresource.pdf
file:///User/path/to/resource.htmlresource.pdf

Options

KeyTypeCLI optionDescriptionDefault Value
inputsstring[]--inputsInput one or more local or online paths.[]
outFilestring--outFileOutput file name.{input}.pdf
outDirstring--outDirOutput directory.process.cwd()
headlessboolean | new--headlessWhether to run the browser in headless mode.new
PDFOptionsobjectValid options to configure PDF generation via Page.pdf().
scalenumber--scaleScales the rendering of the web page. Amount must be between 0.1 and 2.1
headerTemplatestring--headerTemplateHTML template for the print header.
footerTemplatestring--footerTemplateHTML template for the print footer.
preferCSSPageSizeboolean--preferCSSPageSizeGive any CSS @page size declared in the page priority over what is declared in the width or height or format option.false
printBackgroundboolean--printBackgroundSet to print background graphics.false
omitBackgroundboolean--omitBackgroundHides default white background and allows generating PDFs with transparency.false
pageRangesstring--pageRangesPaper ranges to print, e.g. 1-5, 8, 11-13.
marginobject-m, --marginSet the PDF margins. e.g. top=10,bottom=10,left=10,right=10.
landscapeboolean-l, --landscapeWhether to print in landscape orientation.false
pageSizestring-s, --page-sizePrint to Page Size size.letter
widthstring | number-w, --widthPrint to Page Width width in MM.
heightstring | number-h, --heightPrint to Page Height height in MM.
timeoutnumber-t, --timeoutSet a max timeout of ms.
htmlboolean-x, --htmlOutput html file.
blockLocalboolean-b, --blockLocalDisallow access to filesystem for local files.false
blockRemoteboolean-r, --blockRemoteDisallow requests to remote servers.false
outlineContainerSelectorstring--outlineContainerSelectorSpecify an outline container selector.
allowedPathsstring[]--allowedPathsOnly allow access to given filesystem paths, repeatable.[]
ignoreHTTPSErrorsboolean--ignoreHTTPSErrorsWhether to ignore HTTPS errors during navigation.false
allowedDomainsstring[]--allowedDomainsOnly allow access to given remote domains, repeatable.[]
outlineTagsstring[]--outline-tagsAdd outlines (bookmarks) to PDF.h1,h2,h3,h4,h5,h6
additionalScriptsstring[]--additional-scriptsadditional script tags to the HTML document.[]
additionalStylesstring[]--additional-stylesadditional style tags to the HTML document.[]
browserEndpointstring--browserEndpointUse a remote Chrome server with browserWSEndpoint.
browserArgsstring[]--browserArgsLaunch Chrome with comma separated args.[]
mediastring--mediaEmulate print or screen media, defaults to print.print
debugboolean--debugDebug.false
warnboolean--warnEnable warning logs.false

License

This library is under the MIT License.