npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

electron-pdf-remarkable

v1.1.1

Published

A command line tool to generate PDF from URL, HTML or Markdown files

Downloads

28

Readme

electron-pdf

NPM version build status Downloads js-standard-style

A command line tool to generate PDF from URL, HTML or Markdown files with electron.

I have a blog post explain why PDF Generation On The Web

Production ready? See it in action for the Myanmar Election!

Install

npm install electron-pdf -g

For gnu/linux installations without a graphical environment:

$ sudo apt-get install xvfb # or equivalent
$ export DISPLAY=':99.0'
$ Xvfb :99 -screen 0 1024x768x24 > /dev/null 2>&1 &
$ electron-pdf ...

There is also an example docker machine here.

Node Usage

Electron PDF can be used inside of an application, or more commonly as the engine for a pdf rendering service. For instance, to handle http requests using Express. The following snipppets show you how you can get started.

The application must run in an Electron process

In package.json

"start": "DEBUG=electronpdf:* electron index.js",
"watch": "DEBUG=electronpdf:* nodemon --exec electron index.js"

You can use the same instance

var ElectronPDF = require('electron-pdf')
var express = require('express')
var bodyParser = require('body-parser')
var app = express()
app.use(bodyParser.json())

var exporter = new ElectronPDF()
exporter.on('charged', () => {
	//Only start the express server once the exporter is ready
	app.listen(port, hostname, function() {
		console.log(`Export Server running at http://${hostname}:${port}`);
	})
})
exporter.start()

And handle multiple export job instances

app.post('/pdfexport', function(req,res){
	// derive job arguments from request here
	// 
	const jobOptions = {
	  /**
	    r.results[] will contain the following based on inMemory
          false: the fully qualified path to a PDF file on disk
          true: The Buffer Object as returned by Electron
	    
	    Note: the default is false, this can not be set using the CLI
	   */
	  inMemory: false 
	}
	exporter.createJob(source, target, options, jobOptions).then( job => {
	job.on('job-complete', (r) => {
    		console.log('pdf files:', r.results)
    		// Process the PDF file(s) here
    	})
    	job.render()
	})	
})

Using an in memory Buffer

If you set the inMemory setting to true, you must also set closeWindow=true or you will get a segmentation fault anytime the window is closed before the buffer is sent on the response. You then need to invoke job.destroy to close the window.

Sample Code:

const jobOptions = { inMemory: true, closeWindow: false }
exporter.createJob(source, target, options, jobOptions).then( job => {
	job.on('job-complete', (r) => {
	  //Send the Buffer here
	  process.nextTick(() => {job.destroy()})
	})
})

Events

The API is designed to emit noteworthy events rather than use callbacks. Full documentation of all events is a work in progress.

Command Line Usage

For Ad-hoc conversions, Electron PDF comes with support for a CLI.

To generate a PDF from a HTML file

$ electron-pdf index.html ~/Desktop/index.pdf

To generate a PDF from a Markdown file

$ electron-pdf index.md ~/Desktop/index.pdf

To generate a PDF from a Markdown file with custom CSS (defaults to Github markdown style)

$ electron-pdf index.html ~/Desktop/index.pdf -c my-awesome-css.css

To generate a PDF from a URL

$ electron-pdf https://fraserxu.me ~/Desktop/fraserxu.pdf

Rendering Options

Electron PDF gives you complete control of how the BrowserWindow should be configured, and when the window contents should be captured.

To specify browser options

The BrowserWindow supports many options which you may define by passing a JSON Object to the --browserConfig option.

Some common use cases may include:

  • height and width - electron-pdf calculates the browser height and width based off of the dimensions of PDF page size multiplied by the HTML standard of 96 pixels/inch. So only set these values if you need to override this behavior
  • show - to display the browser window during generation
$ electron-pdf https://fraserxu.me ~/Desktop/fraserxu.pdf --browserConfig '{"show":true}'

To generate a PDF after the an async task in the HTML

electron-pdf ./index.html ~/Desktop/README.pdf -e

In your application, at the point which the view is ready for rendering

document.body.dispatchEvent(new Event('view-ready'))

Observing your own event

If the page you are rending is under your control, and you wish to modify the behavior of the rendering process you can use a CustomEvent and an observer that will be triggered after the view is ready but before it is captured.

your-page.html
document.body.dispatchEvent(new CustomEvent('view-ready', { detail: {layout: landscape} }))
your-exporter.js

As an example, suppose you wanted to change the orientation of the PDF

job.observeReadyEvent( (detail) => {
    return new Promise( (resolve,reject) => {
      if( detail && detail.landscape ){
        job.changeArgValue('landscape', true)
      }
      resolve()
    })
})

All Available Options

Electron PDF exposes the printToPDF settings (i.e. pageSize, orientation, margins, etc.) available from the Electron API. See the following options for usage.


  A command line tool to generate PDF from URL, HTML or Markdown files

  Options
    --help                     Show this help
    --version                  Current version of package
    -i | --input               String - The path to the HTML file or url
    -o | --output              String - The path of the output PDF
    
    --browserConfig            String - A valid JSON String that will be parsed into the options passed to electron.BrowserWindow
    -c | --css                 String - The path to custom CSS
    -b | --printBackground     Boolean - Whether to print CSS backgrounds.
                                 false - default
    -s | --printSelectionOnly  Boolean - Whether to print selection only
                                 false - default
    -p | --pageSize            String - Can be A3, A4, A5, Legal, Letter, Tabloid or an Object containing height and width in microns
                                "A4" - default
    -l | --landscape           Boolean - true for landscape, false for portrait.
                                 false - default
    -m | --marginsType          Integer - Specify the type of margins to use
                                 0 - default
                                 1 - none
                                 2 - minimum
    -d | --disableCache        Disable HTTP caching
    -w | --outputWait          Integer – Time to wait (in MS) between page load and PDF creation.  If used in conjunction with -e this will override the default timeout of 10 seconds
    -e | --waitForJSEvent      String - The name of the event to wait before PDF creation
                               'view-ready' - default
    

  Usage
    $ electron-pdf <input> <output>
    $ electron-pdf <input> <output> -l

  Examples
    $ electron-pdf http://fraserxu.me ~/Desktop/fraserxu.pdf
    $ electron-pdf ./index.html ~/Desktop/index.pdf
    $ electron-pdf ./README.md ~/Desktop/README.pdf -l
    $ electron-pdf ./README.md ~/Desktop/README.pdf -l -c my-awesome-css.css

Inspired by electron-mocha

Other Formats

Want to use the same options, but export to PNG or snapshot the rendered HTML? Just set the output filename to end in .png or .html instead!

  Examples
    $ electron-pdf http://fraserxu.me ~/Desktop/fraserxu.pdf
    $ electron-pdf http://fraserxu.me ~/Desktop/fraserxu.html
    $ electron-pdf http://fraserxu.me ~/Desktop/fraserxu.png

Extensions

If you need powerpoint support, pdf-powerpoint picks up where Electron PDF leaves off by converting each page in the PDF to a PNG and placing them on individual slides.

License

MIT