0.5.7 • Published 6 years ago

url2pdf-plus v0.5.7

Weekly downloads
6
License
MIT
Repository
github
Last release
6 years ago

Deprecated! Please use url2pdf3 which uses up to date technology and is mantained!

Get it here: https://www.npmjs.com/package/url2pdf3

Url2PDF


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.

Installation

npm install url2pdf --save

Getting started

node examles.js

Then look into the project folder "pdfTemp"

API

PDF from URL

    var url2pdf = require("url2pdf");

    url2pdf.renderPdf("http://www.google.com")
    	.then(function(path){
	    	console.log("Rendered pdf @", path);
    	});

PDF From HTML String

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);
    	});

In Express route as a download

    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);
            })
    }

Join Pdfs

    var url2pdf = require("url2pdf");

   // read file in, delete file, return content
   const jointPdfFile = url2pdf.join(filepaths, '/exportDir', 'onlyFile');

   // alternative: leave the file and return url
   const jointPdfUrl = url2pdf.join(filepaths);

This requires ghostscript on your system which is executed over command line.

Manual cleanup

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

Configuration options

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
      debug: false // enable to log PhantomJS output in debugging mode  + PhantomJS console messages on your console
    };

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"}});

Notes

If you load a bigger webpage (images etc.), ensure, the loadTimeout is long enough to get everything!

Fonts/Page Too large?

There is a problem with PhantomJS related to this very long thread: https://github.com/ariya/phantomjs/issues/12685

This is the hacky workaround for the moment:

    html {
        zoom: 0.55; /*workaround for phantomJS2 rendering pages too large*/
    }

Tests

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

0.5.7

6 years ago

0.5.6

6 years ago

0.5.5

7 years ago

0.5.4

7 years ago

0.5.3

7 years ago

0.5.2

7 years ago

0.5.1

7 years ago