Grab a URL and convert the HTML to PDF using PhantomJS. Phantom renders the printing version. Can be used for generating bills, protocols, lists, etc. from a website.
Current State
Right now this project is in a bit of a pickle - PhantomJS, the headless browser it relies on has been discontinued. The excellent Puppeteer is the logical successor, however it is not a like-for-like drop in and has some annoying dependency issues on linux. In favour of not breaking our current enterprise applications that rely on this system, I recommend new users check out https://www.npmjs.com/package/url2pdf3 for the time being.
This does not mean that the project has been discontinued - I have a number of mission critical systems that rely on it personally, it just means that we are going to be very pragmatic about any big updates.
npm install url2pdf --save
node examles.js
Then look into the project folder "pdfTemp"
var url2pdf = require("url2pdf");
url2pdf.renderPdf("http://www.google.com")
.then(function(path){
console.log("Rendered pdf @", path);
});
So you have made your html in Jade etc and now you want to just turn it into a PDF without creating a whole website just for this purpose? Easy! Just do as below:
var url2pdf = require("url2pdf");
url2pdf.renderFromHTML("<html><body><h1>HELLO WORLD</h1></body></html>")
.then(function(path){
console.log("Rendered pdf @", path);
});
var url2pdf = require("url2pdf");
function myRoute(req, res){
url2pdf.renderPdf(url)
.then(function (path) {
res.sendFile(path);
})
.catch(function(err){
res.status(500).json(err);
})
}
url2pdf comes with an auto cleanup function that will delete old files in your temp directory. For a manual cleanup disable the auto cleanup in the function call:
url2pdf.renderFromHTML("htmlString ..." ,{
autoCleanFileAgeInSec: -1 // set disabled in options
}).then( ...
To clean the tmp folder call the following function, passing in the age in seconds you would like to delete:
url2pdf.cleanUp(5); // clean up all files older than 5 seconds
You can set the configuration options globally by editing the url2pdf.opts
object. The default settings are shown below
{
paperSize: {format: "A4", orientation: 'portrait', margin: '1cm'},
saveDir: path.join(__dirname, "pdfTemp"), // path for temporary files
idLength: 30 // file ID length; adjust to avoid conflicts or just get smaller filenames
loadTimeout: 800, // in ms; time for rendering the page
autoCleanFileAgeInSec: 24 * 3600 // [s]; older files are removed; set to "-1" to disable remove
};
If you want to just edit the settings for one render, you can do this by passing in just the object fields you want to change as the second argument:
url2pdf.renderPdf("http://www.google.com", {paperSize: {orientation: "landscape"}});
If you load a bigger webpage (images etc.), ensure, the loadTimeout is long enough to get everything!
There is a problem with PhantomJS related to this very long thread: ariya/phantomjs#12685
This is the hacky workaround for the moment:
html {
zoom: 0.55; /*workaround for phantomJS2 rendering pages too large*/
}
Just run node example.js
which runs the available functions. If you are contributing code - at the very least run it to make sure it all seems ok
In adding this to another project I discovered the community has extended the project. While I am still using this in production at the time of this edit, there hasn't been a flurry of upgrades because it just keeps working.
However you might also want to check out these projects that have built on url2pdf: https://www.npmjs.com/package/url2pdf3
https://www.npmjs.com/package/url2pdf-plus (now deprecated in favour of url2pdf3 but apparently has some features not moved forward)