Giter Club home page Giter Club logo

url2pdf's Introduction

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: ariya/phantomjs#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

url2pdf's People

Contributors

felixfurtmayr avatar

Watchers

 avatar  avatar

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.