Code Monkey home page Code Monkey logo

phantom-html-to-pdf's Introduction

phantom-html-to-pdf

NPM Version License Build Status

The phantomjs development is on hold and as is this project. There are, and will be security issues rising! I recommend to evaluate other methods for html to pdf conversion. We have switched from phantomjs to headless chrome in jsreport and it works great.

node.js phantom wrapper for converting html to pdf in scale

Yet another implementation of html to pdf conversion in node.js using phantomjs. This one differs from others in performance and scalability. Unlike others it allocates predefined number of phantomjs worker processes which are then managed and reused using FIFO strategy. This eliminates phantomjs process startup time and it also doesn't flood the system with dozens of phantomjs process under load.

var fs = require('fs')
var conversion = require("phantom-html-to-pdf")();
conversion({ html: "<h1>Hello World</h1>" }, function(err, pdf) {
  var output = fs.createWriteStream('/path/to/output.pdf')
  console.log(pdf.logs);
  console.log(pdf.numberOfPages);
	// since pdf.stream is a node.js stream you can use it
	// to save the pdf to a file (like in this example) or to
	// respond an http request.
  pdf.stream.pipe(output);
});

Installation troubleshooting

  • windows works out of the box.

  • macOS sierra update works only with phantomjs2, see below

  • linux may need to additionally install fontconfig package
    Centos
    sudo yum install -y fontconfig
    Debian/Ubuntu
    sudo apt-get install -y libfontconfig

Global options

var conversion = require("phantom-html-to-pdf")({
	/* number of allocated phantomjs processes */
	numberOfWorkers: 2,
	/* timeout in ms for html conversion, when the timeout is reached, the phantom process is recycled */
	timeout: 5000,
	/* directory where are stored temporary html and pdf files, use something like npm package reaper to clean this up */
	tmpDir: "os/tmpdir",
	/* optional port range where to start phantomjs server */
	portLeftBoundary: 1000,
	portRightBoundary: 2000,
	/* optional hostname where to start phantomjs server */
	host: '127.0.0.1',
	/* use rather dedicated process for every phantom printing
	  dedicated-process strategy is quite slower but can solve some bugs
	  with corporate proxy */
	strategy: "phantom-server | dedicated-process",
	/* optional path to the phantomjs binary
	   NOTE: When using phantomjs 2.0, be aware of https://github.com/ariya/phantomjs/issues/12685 */
	phantomPath: "{path to phantomjs}",
	/* see phantomjs arguments for proxy setting details */
	proxy,proxy-type,proxy-auth,
	/* the collected console.log messages are trimmed by default */
	maxLogEntrySize: 1000
});

Local options

conversion({
	html: '<h1>Hello world</h1>',
	header: '<h2>This is the header</h2>',
	footer: '<div style="text-align:center">{#pageNum}/{#numPages}</div>',
	url: "http://jsreport.net",//set direct url instead of html
	printDelay: 0,//time in ms to wait before printing into pdf
	waitForJS: true,//set to true to enable programmatically specify (via Javascript of the page) when the pdf printing starts (see Programmatic pdf printing section for an example)
	waitForJSVarName: //name of the variable that will be used as a printing trigger, defaults to "PHANTOM_HTML_TO_PDF_READY" (see Programmatic pdf printing section for an example)
	allowLocalFilesAccess: false,//set to true to allow request starting with file:///
	// see PhantomJS options for paperSize - http://phantomjs.org/api/webpage/property/paper-size.html
	paperSize: {
		format, orientation, margin, width, height, headerHeight, footerHeight
	},
  	fitToPage: false, //whether to set zoom if contents don't fit on the page
	customHeaders: [],
        cookies: [{
                name: 'cookie-name',
                value: 'cookie-value',
                path: '/',
                domain: 'domain.com'//Leave blank when working on localhost - "." will get prepended to domain
        }],
	injectJs: [], // injects javascript files in the page
	settings: {
		javascriptEnabled : true,
		resourceTimeout: 1000
	},
	// see phantomjs docs - http://phantomjs.org/api/webpage/property/viewport-size.html
	viewportSize: {
		width: 600,
		height: 600
	},
	format: {
		quality: 100
	}
}, cb);

phantomjs2

This package includes phantomjs 1.9.x distribution. If you like to rather use latest phantomjs you can provide it in the phantomPath option.

Install phantomjs-prebuilt and then...

var conversion = require("phantom-html-to-pdf")({
	phantomPath: require("phantomjs-prebuilt").path
});

conversion({
	html: "foo",   
}, function (err, res){});

Kill workers

//kill all phantomjs workers when using phantom-server strategy
conversion.kill();

Header and footer

It is possible to specify a custom header and a custom footer using HTML.

In the header and footer there is no access to the rest of the document and thus styling with classes and leveraging external CSS does not work. Only inline styles works. This is a limitation on the current PhantomJS implementation.

To print page numbers, use the directives {#pageNum} and {#numPages}, respectively to add current page number and total number of pages. For example:

<div style='text-align:center'>{#pageNum}/{#numPages}</div>

It's also possible to use JavaScript. But note that the JavaScript code has no access to the rest of the HTML document either. Here is an example to modify the paging start:

<span id='pageNumber'>{#pageNum}</span>
<script>
    var elem = document.getElementById('pageNumber');
    if (parseInt(elem.innerHTML) <= 3) {
        elem.style.display = 'none';
    }
</script>

Programmatic pdf printing

If you need to programmatic trigger the pdf printing process (because you need to calculate some values or do something async in your page before printing) you can enable the waitForJS local option, when waitForJS is set to true the pdf printing will wait until you set a variable to true in your page, by default the name of the variable is PHANTOM_HTML_TO_PDF_READY but you can customize it via waitForJSVarName option.

Example:

local options:

conversion({
	html: "<custom html here>",
	waitForJS: true,
	viewportSize: {
		width: 600,
		height: 600
	},
	format: {
		quality: 100
	}
}, cb);

custom html:

<h1></h1>
<script>
	// do some calculations or something async
	setTimeout(function() {
		window.PHANTOM_HTML_TO_PDF_READY = true; //this will start the pdf printing
	}, 500);
</script>

Image in header

To be able to display an image in the header or footer you need to add the same image to the main content and hide it with style="display:none".

Further notes

You may find some further information and usage examples in the jsreport documentation or try pdf printing in the online playground.

Warming up

The phantomjs instances are sinned up when the requests comes. This usually leads to a bit slower first requests. The pre-warmup can be easily done by calling an "empty" conversion the same number of times as the numberOfWorkers config.

License

See license strong text

phantom-html-to-pdf's People

Contributors

algm avatar araczkowski avatar bjrmatos avatar chips5k avatar madarche avatar mardaneus86 avatar michaelkorn avatar peterdeme avatar pofider avatar timruffles avatar troyfendall avatar vinceg avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

phantom-html-to-pdf's Issues

Node Security Project failing

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚               โ”‚ Remote Memory Exposure                                                                                              โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ Name          โ”‚ request                                                                                                             โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ CVSS          โ”‚ 5.3 (Medium)                                                                                                        โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ Installed     โ”‚ 2.42.0                                                                                                              โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ Vulnerable    โ”‚ >=2.2.6 <2.47.0 || >2.51.0 <=2.67.0                                                                                 โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ Patched       โ”‚ >=2.68.0                                                                                                            โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ Path          โ”‚ XXXXXXX > [email protected] > [email protected] > [email protected]                                           โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ More Info     โ”‚ https://nodesecurity.io/advisories/309                                                                              โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚               โ”‚ Regular Expression Denial of Service                                                                                โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ Name          โ”‚ hawk                                                                                                                โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ CVSS          โ”‚ 5.3 (Medium)                                                                                                        โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ Installed     โ”‚ 1.1.1                                                                                                               โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ Vulnerable    โ”‚ < 3.1.3  || >= 4.0.0 <4.1.1                                                                                         โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ Patched       โ”‚ >=3.1.3 < 4.0.0 || >=4.1.1                                                                                          โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ Path          โ”‚ XXXXXX > [email protected] > [email protected] > [email protected] > [email protected]                              โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ More Info     โ”‚ https://nodesecurity.io/advisories/77                                                                               โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜```

Images wont print in the PDF

I am using "phantom-html-to-pdf" to convert to PDF the HTML output of my process. The HTML has 2 images and they are not printed. Instead i get a rectangle where the image is supposed to be printed and also in a wrong location.
wrong-output.pdf

If i decided to press CTRL-P i can print the HTML output using my PDF Printer without any issue.
correct-output.pdf

Is there any reason why you think is doing this? Is there any trick to enable the printing of the images and CSS associated to the HTML?

Regards,

Can't we set password protection?

I want to use this module for creating pdf file. but with a functionality of password protection. can we do that using this module?? or not?

DOC : fontconfig package installation

Depending on your host system you must install fontconfig or libfontconfig to make phantomjs works. This is not automated in installation script.
On Centos:
sudo yum install -y fontconfig

On Debian/Ubuntu:
sudo apt-get install -y libfontconfig

Maybe a note on the README is necessary (to avoid loosing 3 hours as me...).

Breaks when setting numberOfWorkers without setting strategy to 'dedicated-process'

Love the concept of this module - exactly what we needed :)

Have just overcome an issue:

  • I tested without multiple workers: No problem
  • I set numberOfWorkers: 6, and hit the error pasted in below
  • I was able to fix by setting the strategy: 'dedicated-process'

Might be a documentation issue?

Would also love some guidance on how many workers a standalone server with X GB of RAM can handle...

Custom fonts not rendering properly

I tried to put some custom fonts in HTML. It seems to work in general, but it doesn't work when italic style is desired. Italic style works for standard fonts but not for custom.

Font face declaration:

@font-face {
    font-family: 'FontBI';
    src: url('FontBI.TTF') format('truetype'); 
    font-weight: 400;
    font-style: italic;
}

HTML:

<div style="font-family:'FontBI'; font-style:italic; font-weight:bold;">
Italic Font test
</div>

Font-weight:bold is working, but font-style:italic isn't!

Any ideas how to solve this or it is a bug in phantomjs?

How to send cookies when accessing images on a page

Hi, thanks for a great library. It works, but I have the problem. When requests for images are made ( tags) cookies are not sent and headers as well.

Here is an example.

 return htmlToPdf(
            {
                settings: {
                    javascriptEnabled: true,
                    resourceTimeout: 10000
                },

                cookies: [{
                    name: 'cookie-name',
                    value: 'cookie-value',
                    path: '/',
                    domain: 'domain.com'//Leave blank when working on localhost - "." will get prepended to domain
                }],
                customHeaders: headers,
                html: html,
                format: {
                    quality: 100
                }
            },
            function (err, pdf) {
                console.log(pdf.logs);
                console.log(pdf.numberOfPages);
                pdf.stream.pipe(writableStream).on('finish', () => {
                    callback(filename);
                });
            });

I have tried to debug requests using an intercepting proxy software, but neither headers nor cookies are sent when requests are made to download images.

How this problem can be solved ?

dynamic Header/ footer

Hi,

I would like create a dynamic header and footer for the last page. It is possible?

I saw that I just can create the same header and footer with local options.

Thanks

pick up console logs

I'm running the package in a heroku dyno and I'm trying to find a way to get logs from the template picked up by phantom and shown in my heroku console,

I tried adding this to the serverScript.js

page.onConsoleMessage = function(msg, lineNum, sourceId) {
console.log('CONSOLE: ' + msg + ' (from line #' + lineNum + ' in "' + sourceId + '")');
};

but that didn't work..

I haven't worked with phantom at all before.

Extra spacing between characters while generating pdf on RHEL

Hi Team,
We are facing issue regarding extra spacing between characters while generating pdf in redhat. When the pdf is generated through MAC/Windows, Every character looks as it is displayed in html. We have already ran the command "yum install fontconfig freetype libfreetype.so.6 libfontconfig.so.1 libstdc++.so.6".
Please suggest whats wrong with the setup.
Attached is the pdf which was generated.
Thanks.
HealthReport-1.pdf

img src=""

I have some stuff that needs to fetch images dynamically, is there any way to wait until it all is rendered before printing?

CONNECTION REFUSED when converting

Hello,

I've trid your solution and I issue this error when trying your sample code:

[2015-03-31 08:21:33.153] [ERROR] cld-apps.services.invoices - Could not generate invoice pdf cause "{ [Error: connect ECONNREFUSED]
  code: 'ECONNREFUSED',
  errno: 'ECONNREFUSED',
  syscall: 'connect' }"

Any clues of what happens ?

My piece of code:

var conversion = require('phantom-html-to-pdf')({
        numberOfWorkers: 2,
        timeout: 5000,
        tmpDir: '../temp'
    });

exports.downloadInvoice = function (req, res, next) {
    log.info('Downloading invoice "%s (%s)"', req.invoice._id, req.invoice.number);
    conversion({ html: '<h1>Hello World</h1>' }, function(err, pdf) {
        if (err) {
            log.error('Could not generate invoice pdf cause "%s"', util.inspect(err));
            return next(new jmcnetException.TechnicalException(err));
        }
        log.debug('pdf is "%s"', util.inspect(pdf));
        pdf.stream.pipe(res);

    });
};

knowing when the PDF has finished to generate

Hi,

I have a file system watcher that watch a folder. When a file is detected, I read the file and I convert it into PDF with phantom-html-to-pdf.

My concern is when too many files is handled, Phantomsjs crashs.

I would like tell at phantomjs to manage a maximum 20 files for exemple. Is it possible?

Or would you advise me how I can make this without this error?

Thanks

Is numberOfWorkers implemented?

I'm a bit confused that I can't see numberOfWorkers anywhere in the codebase aside from the README and a place where it's assigned to an options object. It seems unused.

It's also not in PhantomJS itself.

I'm sure I'm missing something :) If it isn't doing anything this project doesn't seem to match the README (e.g re options nor FIFO?).

image (png/jpg) not rendered in pdf

The generated pdf does not render the images that are in the html, although the temporary generated html file does display them properly. Is there a special settings or method to have them in the pdf?

Link are not working

The links referencing other part of the page (ex:

  • Home
  • in the html before pdf conversion) are not working in the pdf document. Clicking on those links on the pdf ask to open a new file which does not work either, instead of pointing at the right location within the current pdf file. Any idea, solutions?

    Provide phantomjs output somehwere

    Currently we are just ignoring the ouput from phantomjs. The output can contain very important messages which should be provided to the caller.

    Docker

    Trying to run this in docker gives me

    /node_modules/phantom-html-to-pdf/lib/conversion.js:83
    if (opt.phantomPath) {
    ^

    TypeError: Cannot read property 'phantomPath' of undefined
    at module.exports (/node_modules/phantom-html-to-pdf/lib/conversion.js:83:12)
    at Object. (/lib/index.js:11:45)
    at Module._compile (module.js:397:26)
    at Object.Module._extensions..js (module.js:404:10)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Module.require (module.js:353:17)
    at require (internal/module.js:12:17)
    at Object. (/example/index.js:2:15)
    at Module._compile (module.js:397:26)

    any ideas?

    bad quality images

    Hello, thanks for the package!
    I'm using chart js to create charts in my site, then i'm converting it to base64 and send it to the server for the pdf creating. the quality is much worse than in the site. do you have any idea why?
    the pdf:
    204026330_concentration (4).pdf
    the site:
    site

    Debug: internal, implementation, error

    Now trying to run my server on an AWS instance running ubuntu, and hitting:

    Debug: internal, implementation, error
        TypeError: Uncaught error: Cannot read property 'stream' of undefined
        at /var/apps/pdf-generator/index.js:88:14
        at /var/apps/pdf-generator/node_modules/phantom-html-to-pdf/lib/dedicatedProcessStrategy.js:31:24
        at ChildProcess.exithandler (child_process.js:641:7)
        at ChildProcess.EventEmitter.emit (events.js:98:17)
        at maybeClose (child_process.js:743:16)
        at Process.ChildProcess._handle.onexit (child_process.js:810:5)
    

    Here's my simple server:

    'use strict';
    
    var Hapi = require('hapi');
    var htmlToPdf = require('phantom-html-to-pdf')({
      numberOfWorkers: 6,
      strategy: 'dedicated-process'
    });
    
    var server = new Hapi.Server();
    server.connection({
      // host: 'localhost',
      port: 3000
    });
    
    // Inert required to return a file: http://hapijs.com/tutorials/serving-files
    server.register(require('inert'), function (err) {
      // Add the route
      server.route([
        {
          method: 'GET',
          path: '/pdf',
          handler: function (request, reply) {
            generatePdf(request.url.query.url, reply);
          }
        }
      ]);
    
      // Start the server
      server.start(function () {
        console.log('Server running at:', server.info.uri);
      });
    });
    
    function generatePdf (url, reply) {
      console.log('Rendering', url);
    
      htmlToPdf({
        url: url
      }, function(err, pdf) {
        reply(pdf.stream).type('application/pdf');
      });
    }
    

    Any ideas what could be leading to this issue?

    Unlike locally, changing the strategy doesn't seem to make any difference :(

    Feature request: conversion.waitFor

    Thanks for both the tool and answering earlier issues today!

    I have a webpage that downloads a set of files and then runs a script to translate the content to the right language. This works great, unless the PDF is generated before the content has been translated.

    conversion.printDelay is useful, but isn't reliable enough.

    Instead, I want to run a custom JS test to check whether or not the page has actually been loaded. If it hasn't, then I'd wait 100ms and try again. This polling technique is common with Selenium-based testing frameworks (e.g. Protractor).

    I'm going to be looking into this tomorrow, so any guidance on how to implement this with (or as an extension/pull-request for) phantom-html-to-pdf would be very welcome :)

    Timeout error

    We are currently using phantom-html-to-pdf to generate a user report pdf, but on one specific linux server running nginx we're getting a 504 timeout error no matter what we try. It is as if it never gets to startup phantomjs. There aren't any errors (except timeout) in any of our logs or anything.
    We have a different server running almost the exact same setup, where the setup works completely fine. The server where it doesn't work is a more closed test server, where only a selected range of IP-adresses have access. Other than that we can't find any noticeable differences that should affect this package.

    We have tried to specify a port range and opening access for localhost requests in that range in the nginx configuration, but without luck. All localhost access should already be accepted, but we tried it nonetheless. We also tried changing the node version around to see if that had any effect, which it didn't. Giving the package a larger timeout in the global setting didn't change anything but the amount of time it took before the timeout error page was displayed.

    Our implementation looks like this:

    var converter = require('phantom-html-to-pdf')({
    	host: '127.0.0.1'
    });
    
    converter({
    	html: html,
    	header: '<div>{#pageNum}</div>',
    	footer: '<div></div>',
    	waitForJS: true,
    	waitForJSVarName: 'PHANTOM_HTML_TO_PDF_READY',
    	allowLocalFilesAccess: true,
    	paperSize: {
    		format: 'A4',
    		orientation: 'landscape',
    		margin: '0.5in',
    		headerHeight: '0.3in',
    		footerHeight: '0.7in'
    	},
    	settings: {
    		javascriptEnabled: true
    	},
    	format: {
    		quality: 100
    	},
    	phantomPath: require('phantomjs-prebuilt').path
    }, function(err, pdf) {
    	if (err) {
    		console.log(err);
    		return;
    	}
    
    	console.log(pdf.logs);
    	console.log(pdf.numberOfPages);
    	pdf.stream.pipe(res);
    });
    

    Here's some server information which could be useful:

    • Linux server v. 3.10.0-514.el7.x86_64
    • Nginx v. 1.10.2
    • Node v. 4.2.6 (also tried running version 6 but there was no difference)
    • MongoDB v. 2.6.12
    • phantom-html-to-pdf v. 0.4.7
    • phantomjs-prebuilt v. 2.1.12

    We are currently running out of ideas and hoping that someone could help us figure out what we can do to fix this?

    Upgrade from 0.3.5 -> 0.4.1 breaks image rendering

    When we upgraded from 0.3.5 to 0.4.1, we are noticing that tags are failing to render properly.

    We are using express 4 and an interesting quirk we noticed is that the pdf will render properly if we first pass the html fed to the phantom server in our express app. So, for example, in the code below, the image renders appropriately if we make a request to '/page' and then '/file' but not if we just call '/file'

    var converter = require("phantom-html-to-pdf")()
    app.get('/page', function(req, res, next) {
       var htmlForPdf = _.template(loadTemplate('pdf_report.html.erb'))()
       res.render('pdf', {pdf: htmlForPdf})
    })
    
    app.get('/file', function(req,res,next) {
       var htmlForPdf = _.template(loadTemplate('pdf_report.html.erb'))()
       converter({html: htmlForPdf}, function(err, file) {
         file.stream.pipe(res)
       })
    }
    

    Print pdf with node

    Hi, sorry for stupid question, but how to:

    1. send converted pdf file to printing query via nodejs script? (not by window.PHANTOM_HTML_TO_PDF_READY = true; in html script tag)
    2. save converted pdf document on the local file system by node? :)
      There is no docs about it, it's a pity.

    PhantomJS 2.x support?

    Is there anything blocking this module (or others like phantom-workers) from having the dependency for PhantomJS updated to the 2.x series?

    Possibility to inject scripts

    PhantomJS allows for the possibility to inject JS files into the page. I couldn't find this functionality here. If it fits the roadmap I could submit a PR, but I don't have a good solution on how to unit test this.

    The edit is contained in the lib/scripts/conversionScriptPart.js:

    Adding these lines to the page.onInitialized function

        if (body.injectScripts && body.injectScripts.length > 0) {
          body.injectScripts.forEach(function(script) {
            page.injectJs(script);
          });
        }

    This functionality could then be triggered by calling conversion like this:

    conversion({
        url: "http://example.com",
        injectScripts: [
          __dirname + '/polyfill.js' // <-- include the full path to the script(s) to be injected
        ],
        waitForJS: true
      }, function(err, pdf) {
        ...
      })

    Thoughts?

    Include custom cookies in request

    Hi, not sure where else to post this, but i need the ability to include a cookie when phantom js requests a file. I can see how i might do this by modifying your source code (which i'd rather not do), but i cannot work out where/how and in what format to supply cookies in the initial conversion function.

    Below is the code i am using, i'd assumed i could just pass a cookies property in the options object. Tried several different formats, and tried to trace through your code but had no luck.

    conversion({
    url: request.post.location',
    // waitForJS: true,
    // waitForJSVarName: 'ready_to_print'
    }, function(err, pdf) {
    console.log(pdf.logs);
    console.log(pdf.numberOfPages);
    pdf.stream.pipe(response);
    });

    Tried:

    cookie: [[name, value]]
    cookie: [{name: name, value: value}]
    cookie: ["name=value"]

    None of the above worked.

    I've looked at phantomjs itself which recommends using addCookie methods, but since i dont have access to the phantom objects themselves, im not sure how to go about this (again without modifying the code).

    thanks!

    Callem.

    Openshift issue

    Hello,

    Your library works perfectly locally but when used in openshift, I encounter trouble.

    I receive this error. Do you know why ? Is it possible to configure your package to use specific ports ?

    events.js:72
            throw er; // Unhandled 'error' event
                  ^
    Error: listen EACCES
        at errnoException (net.js:905:11)
        at Server._listen2 (net.js:1024:19)
        at listen (net.js:1065:10)
        at net.js:1147:9
        at dns.js:72:18
        at process._tickCallback (node.js:442:13)
    

    I've read this interresting article in which they say that phantom generates a little local server bind to localhost. Openshift does not allow that.

    I was wondering if there is a way to initialiez your package with another variable that localhost ?

    http://stackoverflow.com/questions/32260650/create-a-pdf-from-html-and-send-it-to-the-user-from-a-buffer/34039207

    I have tried initializing the library using this :

    var conversion = require("phantom-html-to-pdf")({
      host: process.env.OPENSHIFT_NODEJS_IP,
      phantomPath: require('path').dirname(process.env.PHANTOMJS_EXECUTABLE) + '/'
    });
    

    But his does not work.

    Using the library with this :

     var conversion = require("phantom-html-to-pdf")({
        strategy: "dedicated-process"
    });
    

    Is working locally but not in openshift.
    In openshift, I receive a "failed to load pdf error" from my browser but no error server side ...

    No text on mac

    I've installed and used the package as noted for macs - phantomjs-prebuilt.
    But the rendered pdf file does not have any text on it. Can you please advise what to do in this situation?

    Converted pdf texts are not showing clearly

    Hi ,

    I am using this module to convert html string to PDF file in node.js like this,

    var htmlstr="<h1>Test</h1><p>Hello world</p>";
    var conversion = require("phantom-html-to-pdf")();
    conversion({ html: htmlstr}, function(err, pdf) {
      console.log(pdf.logs);
      console.log(pdf.numberOfPages);  
      pdf.stream.pipe(fs.createWriteStream(__dirname+'/destination.pdf'));   
    });

    after that I viewed pdf file ,in that the text is not showing .

    Can't set footer

    We need to set the content of the footer as part of the paperSize call (in order to add page numbering), so I want to be able to set up the config below.

      paperSize: {
        format: 'A4',
        orientation: 'portrait',
        footer: {
          height: '1cm',
          // Pass as a string to overcome a limitation of node-phantom
          // See description here: http://stackoverflow.com/questions/17102287/footers-contents-dont-seem-to-work
          contents: 'function(pageNum, numPages) { if(pageNum > 1) { return \'<div style="text-align: center;font-family:Arial;font-size:12px;color:#333">\' + pageNum + \'</div>\'; } }'
        },
        margin: {
          top: '2cm',
          left: '2cm',
          right: '2cm',
          bottom: '1cm'
        }
      },
    

    Unfortunately, it seems there's no support for footer. I'm going to take a look under the hood now to see if I can suggest a solution :)

    How to reset pageNum? How to achieve mass PDF generation

    Hi,

    I am wondering if it is possible to reset {#pageNum} variable in footer? There is requirement for mass PDF generation and that includes page numbering. So everytime there is content for new PDF, pageNum should be reset to 1. That would be convenient for printing large number of PDFs (so all PDF will be in one file).

    Is there any option for that? What about merging PDFs with that library?

    Thank you for any answer.

    Remove temporary file

    Actually the module create temporary file (html, and pdf) for the generation of the pdf.

    Can the plugin remove them when the pdf is created (or in case of fail) ?

    Thanks

    Mac conversion not working

    macOS Sierra 10.12.3
    node 6.9.1
    phantomjs: 2.1.1

    var app = express();
    
    app.get('/', function (req, res) {
        var conversion = require("phantom-html-to-pdf")({
            timeout: 5000
        });
    
        conversion({ html: "<h1>Hello World</h1>" }, function (err, pdf) {
            if (err) {
                console.log(err);
                res.status(200).send(err);
            } else {
                console.log(pdf.logs);
                console.log(pdf.numberOfPages);
                pdf.stream.pipe(res);
            }
        });
    });
    

    result

    { Error: connect ECONNREFUSED 127.0.0.1:56033
        at Object.exports._errnoException (util.js:1026:11)
        at exports._exceptionWithHostPort (util.js:1049:20)
        at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1085:14)
      code: 'ECONNREFUSED',
      errno: 'ECONNREFUSED',
      syscall: 'connect',
      address: '127.0.0.1',
      port: 56033 }
    

    And the port number keeps changing?
    Any help on that please?

    Links aren't clickable

    Here is the code that I'm using to render the HTML file (published here) - https://redhat-microservices.github.io/lab_swarm_forge-keycloak/

    var conversion = require("phantom-html-to-pdf")();
    var fs         = require('fs');
    
    var res = fs.createWriteStream('generated_content/hol.pdf');
    var src = fs.readFileSync('/Users/chmoulli/MyProjects/litoria/generated_content/hol.html');
    
    conversion({
          html: src.toString(),
          allowLocalFilesAccess: true,
          format: {
              quality: 100
          }
        }, function(err, pdf) {
        console.log(pdf.logs);
        console.log(pdf.numberOfPages);
        pdf.stream.pipe(res);
    });
    

    The pdf file generated looks good excepted that the links aren't clickable

    screenshot 2016-08-31 11 23 44
    screenshot 2016-08-31 11 23 31

    Picture in the header.

    The only way I've found to inyect a picture in the header is also necessary to put it in the body with none display.

    ECONNREFUSED when running into a Docker Container

    Hi there,
    I can't make phantom-html-to-pdf works in a dockerized environment. I run the nodeJS docker image named node:7.4-alpine which runs an application containing phantom-html-to-pdf.
    Each time I try to generate a pdf I've got an exception:

     [2017-01-24 07:27:21.245] [ERROR] cld-apps.services.invoices - Could not generate invoice pdf cause "{ Error: connect ECONNREFUSED 127.0.0.1:34315
      at Object.exports._errnoException (util.js:1022:11)
      at exports._exceptionWithHostPort (util.js:1045:20)
      at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1090:14)
    
      code: 'ECONNREFUSED',
      errno: 'ECONNREFUSED',
      syscall: 'connect',
      address: '127.0.0.1',
      port: 34315 }"
    [2017-01-24 07:27:21.247] [ERROR] cld-apps.core.express - { Error: connect ECONNREFUSED 127.0.0.1:34315
      at Object.exports._errnoException (util.js:1022:11)
      at exports._exceptionWithHostPort (util.js:1045:20)
      at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1090:14)
    From previous event:
      at exports.previewInvoiceDoc (/home/clduser/cld-apps/packages/invoices/server/controllers/ctrlInvoiceDoc.js:439:4)
      at Layer.handle [as handle_request] (/home/clduser/cld-apps/node_modules/express/lib/router/layer.js:95:5)
      at next (/home/clduser/cld-apps/node_modules/express/lib/router/route.js:131:13)
      at /home/clduser/cld-apps/packages/system/server/oauth2/middlewares/authorization.js:184:12
      at _combinedTickCallback (internal/process/next_tick.js:67:7)
      at process._tickCallback (internal/process/next_tick.js:98:9)
      code: 'ECONNREFUSED',
      errno: 'ECONNREFUSED',
      syscall: 'connect',
      address: '127.0.0.1',
      port: 34315 }
    

    Of course this works well outside a container.

    Any clues to make it works ?

    EDIT:
    My piece of code:

    var conversion = require('phantom-html-to-pdf')({
    	/* number of allocated phantomjs processes */
    	numberOfWorkers: jmcnetConfig.getInt('commons.phantomJS.poolSize', 2),
    	/* timeout in ms for html conversion, when the timeout is reached, the phantom process is recycled */
    	timeout: jmcnetConfig.getInt('commons.phantomJS.timeout', 10000),
    	/* directory where are stored temporary html and pdf files, use something like npm package reaper to clean this up */
    	tmpDir: jmcnetConfig.get('commons.phantomJS.tempDir')
    });
      ...
      conversion({
    	html: helperInvoice.getHtmlInvoiceFromTemplate(context, templateName, template.template, 'server-messages'),
    	header: helperInvoice.getHtmlHeader(context, templateName, template, 'server-messages'),
    	footer: helperInvoice.getHtmlFooter(context, templateName, template, 'server-messages'),
    	paperSize: {
    		format: template.paperSize,
    		margin: template.margin,
    		orientation: template.orientation,
    		headerHeight: template.headerHeight,
    		footerHeight: template.footerHeight
    	}
    }, function (err, pdf) {
    	if (err) {
    		log.error('Could not generate invoice pdf cause "%s"', dbg(err));  <--- we see this log
    		deferred.reject(err);
    	} else deferred.resolve(pdf);
    });
    
    

    Error during rendering report

    I'm working on a web application using phantom-html-to-pdf, localmente works but in production the following error occurs

    Error during rendering report: spawn /opt/venfi/node_modules/phantom-html-to-pdf/node_modules/phantomjs/lib/phantom\phantomjs.exe ENOENT

    Centos
    Node v6.10.3

    Any idea what could be the issue here?
    Thank you.

    none of the file resources are getting recognized

    i used below code to replace the file names with absolute file path, still it is giving me protocol is unknown, i have font files, image files to include in pdf

    var dirName = path.join('file://', __dirname);
    dirName = dirName + '/';
    

    html = html.replace(/src="/g, 'src="'+dirName).replace(/src: url(/g,'src: url('+dirName);

    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.