Giter VIP home page Giter VIP logo

crpdf's Introduction

crPDF

build version dependencies dev dependencies

Print HTML to PDF using Puppeteer with a wkhtmltopdf compatible CLI.

Motivation

While creating print versions of HTML on modern browsers is not fun, it is not an impossible feat. Getting HTML to PDF tools to render correctly on the other hand is very challenging. That is why this project leverages Puppeteer (Headless Chrome/Chromium) to generate PDFs from HTML. If you can get your layout to work using print preview on Chrome you will be good to go using this library.

Many of us have relied on wkhtmltopdf over the years, and it has great library support. To make transitioning as easy as possible crPDF matches the CLI options which allows you to use crPDF as a drop-in replacement.

Installation

Using NPM

Use NPM to install the latest version of crPDF, this package will install both a crpdf and wkhtmltopdf binary.

npm install -g crpdf

Manual

Download a binary for your platform from the releases page and place it in your $PATH.

curl -L -o crpdf <release url>
chmod +x crpdf

cp crpdf /usr/local/bin/crpdf

# Optionally set up an alias for wkhtmltopdf
ln -s /usr/local/bin/crpdf /usr/local/bin/wkhtmltopdf

Usage

The CLI interface is meant to be compatible with the wkhtmltopdf binary such that it can be used as a drop-in replacement for existing libraries using wkhtmltopdf.

Below is a list of options currently implemented by this project.

crpdf --help

  Usage: cli [options] [input] [output]


  Options:

    -V, --version                    output the version number
    -B, --margin-bottom <unitreal>   Set the page bottom margin
    -L, --margin-left <unitreal>     Set the page left margin (default 10mm)
    -R, --margin-right <unitreal>    Set the page right margin (default 10mm)
    -T, --margin-top <unitreal>      Set the page top margin
    -O, --orientation <orientation>  Set orientation to Landscape or Portrait (default Portrait)
    -s, --page-size <Size>           Set paper size to: A4, Letter, etc. (default A4)
    --page-height <unitreal>         Page height
    --page-width <unitreal>          Page width
    --page-ranges <range>            Page ranges to print, e.g., "1-5, 8, 11-13" (default all)
    --background                     Do print background
    --no-background                  Do not print background
    -h, --help                       output usage information

Note, --page-ranges is not available in wkhtmltopdf and thus not backwards compatible.

Contributing

I really appreciate any contribution you would like to make, so don't hesitate to report an issue or submit pull requests.

About me

Hi, my name is Jorgen Evens. By day I built things (mainly in PHP and JavaScript) for Ambassify and by night I tinker around with these kinds of projects.

crpdf's People

Contributors

jorgenevens avatar tlvince avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

tlvince icnocop

crpdf's Issues

Incompatible behavior if `-` is specified as the `output`

When I use wkhtmltox 0.12.5-1.msvc2015-win64 and run the following command:
wkhtmltopdf.exe TestPage.html -

wkhtmltopdf.exe will output the PDF file contents to the console (stdout) instead of to a file.

However, crPDF.exe will output the PDF to a file with the file name -.

I expected crPDF to behave the same as wkhtmltopdf and output the PDF file contents to the console.

Thank you.

UnhandledPromiseRejectionWarning: Error: ENOENT: no such file or directory, open '-'

I used wkhtmltopdf and everything worked, but then our hosting provider said to try your library, because wkhtmltopdf is heavy, and I got this error:

OSError at /admin/fulfilment-report/
wkhtmltopdf reported an error:
(node:17000) UnhandledPromiseRejectionWarning: Error: ENOENT: no such file or directory, open '-'
    at Object.fs.openSync (fs.js:646:18)
    at Object.fs.readFileSync (fs.js:551:33)
    at toPDF (/Users/mirzadelic/.nvm/versions/node/v8.15.0/lib/node_modules/crpdf/src/index.js:49:33)
    at Object.<anonymous> (/Users/mirzadelic/.nvm/versions/node/v8.15.0/lib/node_modules/crpdf/src/cli.js:47:1)
    at Module._compile (module.js:653:30)
    at Object.Module._extensions..js (module.js:664:10)
    at Module.load (module.js:566:32)
    at tryModuleLoad (module.js:506:12)
    at Function.Module._load (module.js:498:3)
    at Function.Module.runMain (module.js:694:10)
(node:17000) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:17000) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Any solution?

Thanks.

Add support for `--javascript-delay`

Hi.

Thank you for crPDF.

wkhtmltopdf supports the --javascript-delay command line option.

See https://wkhtmltopdf.org/usage/wkhtmltopdf.txt

If a javascript delay isn't supported in puppeteer, I'm thinking crPDF should just ignore the command line option.

Currently, crPDF outputs error: unknown option '--javascript-delay' and doesn't produce the expected output.

Thank you.

Add support for `--encoding`

wkhtmltopdf supports the --encoding command line option.

See https://wkhtmltopdf.org/usage/wkhtmltopdf.txt

If the ability to specify an encoding isn't supported in puppeteer, I'm thinking crPDF should just ignore the command line options.

Currently, crPDF outputs error: unknown option '--encoding' and doesn't produce the expected output.

Thank you.

Add support for `--user-style-sheet`

wkhtmltopdf supports the --user-style-sheet command line option.

See https://wkhtmltopdf.org/usage/wkhtmltopdf.txt

If the ability to specify a user style sheet isn't supported in puppeteer, I'm thinking crPDF should just ignore the command line options.

Currently, crPDF outputs error: unknown option '--user-style-sheet' and doesn't produce the expected output.

Thank you.

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.