This article describes the implementation method of generating PDF based on Phantomjs. Share it for your reference, as follows:
Recently, in project development, I encountered the need to generate PDFs. Of course, generating PDFs is not a new requirement; I can choose to use open source pdfkit or other node pdf modules, or generate PDFs by calling the pdf library under .net/python. But in my opinion, it will take too much time for these things no matter what (the content of the pdf report is very complicated), so it is better to push all the drawing implementation logic to the familiar HTML+CSS to the simple and fast. In this way, for changes in the pdf format and changes in the graph calculation logic to template engines such as ejs and Jade, it is a good choice for future modification, maintenance and expansion. So choosing phantomjs to load the page to generate PDF is not a good choice for me. At the same time, the only browser I need to be compatible with is webkit, and there is no disgusting browser compatibility concern. So I just do it. I spent half an hour on the project to configure the automated script of phantomjs (which can be automatically quipped in each environment), and realized PDF conversion of a simple page.
(From the official pdf demo):
var page = require('webpage').create(), system = require('system'), address, output, size; if ( < 3 || > 5) { ('Usage: URL filename [paperwidth*paperheight|paperformat] [zoom]'); (' paper (pdf output) examples: "5in*7.5in", "10cm*20cm", "A4", "Letter"'); (1); } else { address = [1]; output = [2]; = { width: 600, height: 600 }; if ( > 3 && [2].substr(-4) === ".pdf") { size = [3].split('*'); = === 2 ? { width: size[0], height: size[1], margin: '0px' } : { format: [3], orientation: 'portrait', margin: '1cm' }; } if ( > 4) { = [4]; } (address, function (status) { if (status !== 'success') { ('Unable to load the address!'); (); } else { (function () { (output); (); }); } }); }
On the node calling side, use the exec call command line to get the file and return to the node response stream:
guid utils:
'use strict'; var guid = function () { var uid = 0; = function () { uid = uid % 1000; var now = new Date(); var utc = new Date(() + () * 60000); return () + uid++; } } = { guid: new guid() };
pdfutil:
'use strict'; var exec = require('child_process').exec; var utils = require('./utils').utils; var nodeUtil = require('util'); var outPut = function (id, req, res) { var path = ("tmp/%", ()); var port = ; var pdfUrl = ("%s://%s%s/pdf/%s", , , ( port == 80 || port == 443 ? '' : ':' + port ), id); exec(("phantomjs tool/ %s %s A4", pdfUrl, path), function (error, stdout, stderr) { if (error || stderr) { (500, error || stderr); return; } ('Content-Type', 'application/pdf'); (path); }); }; = { outPut: outPut };
The response code can also be converted to a command line call of java/c#... to get the pdf and push it into the response stream. Everything is so simple to do.
Node also has a node-phantom module, but the pdf style generated by it is a bit weird, so in the end, I insisted on using the exec method.
Also, phantomjs generates PDF and will not bring the background color and background image of the css, so for this, it specifically uses the solid color image img tag, and position: relative or absolute to locate the text. Fortunately, users will not see it on this page.
For more information about JavaScript, readers who are interested in reading this site's special topic:Summary of JavaScript switching effects and techniques》、《Summary of JavaScript search algorithm skills》、《Summary of JavaScript animation special effects and techniques》、《Summary of JavaScript Errors and Debugging Skills》、《Summary of JavaScript data structure and algorithm techniques》、《JavaScript traversal algorithm and skills summary"and"Summary of JavaScript mathematical operations usage》
I hope this article will be helpful to everyone's JavaScript programming.