/html-to-pdf

Chromium-based HTML to PDF API

Primary LanguageJavaScriptMIT LicenseMIT

Chromium-based HTML to PDF API

Docker Image Size (latest by date)

Run in docker container:

docker run -p 3000:3000 -d --restart always cempehlivan/html-to-pdf

Run in NodeJS:

cd app
..
npm install
..
node app.js

swagger documentation url: http://localhost:3000/api-docs

html to pdf endpoint: http://localhost:3000/converter/htmltopdf

Parameters

Parameter Type Description
html HTML string required
options PDFOptions (Optional)

PDF Options

Property Type Description
displayHeaderFooter? boolean (Optional) Whether to show the header and footer.
footerTemplate? string (Optional) HTML template for the print footer. Has the same constraints and support for special classes as headerTemplate.
format? PaperFormat (Optional)
headerTemplate? string

(Optional) HTML 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

height? string | number (Optional) Sets the height of paper. You can pass in a number or a string with a unit.
landscape? boolean (Optional) Whether to print in landscape orientation.
margin? PDFMargin (Optional) Set the PDF margins.
omitBackground? boolean (Optional) Hides default white background and allows generating pdfs with transparency.
pageRanges? string (Optional) Paper ranges to print, e.g. 1-5, 8, 11-13.
path? string (Optional) The path to save the file to.
preferCSSPageSize? boolean (Optional) Give any CSS @page size declared in the page priority over what is declared in the width or height or format option.
printBackground? boolean (Optional) Set to true to print background graphics.
scale? number (Optional) Scales the rendering of the web page. Amount must be between 0.1 and 2.
timeout? number (Optional) Timeout in milliseconds
width? string | number (Optional) Sets the width of paper. You can pass in a number or a string with a unit.

Paper Format

The sizes of each format are as follows: - Letter: 8.5in x 11in

  • Legal: 8.5in x 14in

  • Tabloid: 11in x 17in

  • Ledger: 17in x 11in

  • A0: 33.1in x 46.8in

  • A1: 23.4in x 33.1in

  • A2: 16.54in x 23.4in

  • A3: 11.7in x 16.54in

  • A4: 8.27in x 11.7in

  • A5: 5.83in x 8.27in

  • A6: 4.13in x 5.83in

PDF Margin

Property Modifiers Type Description
bottom? string | number (Optional)
left? string | number (Optional)
right? string | number (Optional)
top? string | number (Optional)

Post JSON

{
    "html": "<h1>test</h1>",
    "options": {
        "format": "A4",
        "printBackground": true,
        "margin": {
            "top": "10.5mm",
            "left": "10.5mm",
            "right": "10.5mm",
            "bottom": "10.5mm"
        }
    }
}

Response JSON

{
    "status": "success",
    "data": {
        "pdf": "<base64pdf>"
    }
}

If you get an error on Ubuntu

The API may give an error due to the lack of some packages on Ubuntu. You can resolve this error by running the following command.

sudo apt-get install -y libgtk2.0-0 libgtk-3-0 libnotify-dev libgconf-2-4 libnss3 libxss1 libasound2 libxtst6 xauth xvfb libgbm-dev

Packages used