Coder Social home page Coder Social logo

nodejs-html-pdf's Introduction

Puppeteer HTML to PDF converter REST api

Deploy

This is a html to pdf converter rest api which converts web pages to pdfs using the headless chrome instance powered by Puppeteer.

Usage

POST /generate

Parameters

The api doesn't care much how you send the parameters. Wether it's form-data form-urlencoded or as raw json. It's all welcome here.

Parameter Type Description
url string Required if no html. The url of the webpage to convert to pdf
html string Required if no url. The html to convert to pdf
scale string Optional. Scale of the webpage rendering. Defaults to 1. Scale amount must be between 0.1 and 2
displayHeaderFooter boolean Optional. Display header and footer. Defaults to false
headerTemplate string Optional. HTML template for the print header. Should be valid HTML markup with following classes used to inject printing values into them: date, title, url, pageNumber, totalPages
footerTemplate string Optional. HTML template for the print footer. Should use the same format as the headerTemplate
printBackground boolean Optional. Print background graphics. Defaults to false
landscape boolean Optional. Paper orientation. Defaults to false
pageRanges string Optional. Paper ranges to print, e.g., '1-5, 8, 11-13'. Defaults to the empty string, which means print all pages
format string Optional. Paper format. If set, takes priority over width or height options. Defaults to 'Letter'
width integer Optional. Paper width, accepts values labeled with units
height integer Optional. Paper height, accepts values labeled with units
margin.top integer Optional. Top margin, accepts values labeled with units
margin.right integer Optional. Right margin, accepts values labeled with units
margin.bottom integer Optional. Bottom margin, accepts values labeled with units
margin.left integer Optional. Left margin, accepts values labeled with units
preferCSSPageSize boolean Optional. 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

Response

If the request was succesful the response will look like this:

{
    "success": true,
    "url": "https://immense-citadel-73637.herokuapp.com/exports/1564590445-necij.pdf",
    "path": "/exports/1564590445-necij.pdf",
    "expires": 1564590505
}

If one of the parameters was invalid the request will look something like:

{
    "success": false,
    "errors": [
        {
            "msg": "Must provide either url or html",
            "param": "url_html",
            "location": "body"
        }
    ]
}

Installation

Aside from the option to deploy this project on Heroku the instruction below is meant for either a local or standalone setup.

  1. npm install chromium & dependency
npm install chromium 

 yum install ipa-gothic-fonts xorg-x11-fonts-100dpi xorg-x11-fonts-75dpi xorg-x11-utils xorg-x11-fonts-cyrillic xorg-x11-fonts-Type1 xorg-x11-fonts-misc -y

 yum install pango.x86_64 libXcomposite.x86_64 libXcursor.x86_64 libXdamage.x86_64 libXext.x86_64 libXi.x86_64 libXtst.x86_64 cups-libs.x86_64 libXScrnSaver.x86_64 libXrandr.x86_64 GConf2.x86_64 alsa-lib.x86_64 atk.x86_64 gtk3.x86_64 ipa-gothic-fonts xorg-x11-fonts-100dpi xorg-x11-fonts-75dpi xorg-x11-utils xorg-x11-fonts-cyrillic xorg-x11-fonts-Type1 xorg-x11-fonts-misc
  1. Clone the repo
git clone https://github.com/fritsvt/puppeteer-html-to-pdf-converter.git
  1. Navigate in the project directory
cd puppeteer-html-to-pdf-converter
  1. Install the npm dependencies
npm install
  1. Copy the contents of config-example.json and place them in a new config.json
cp config-example.json config.json
  1. Fill in the config.json with your configuration
  2. Run the app
npm run start
 pm2 start index.js

Technologies

puppeteer

expressjs

express-slow-down

express-validator

cors

multer

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

License

MIT

nodejs-html-pdf's People

Contributors

fritsvt avatar krnkaf avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.