Phantomjs generates multi-page PDF examples
Recent use Phantomjs PDF generation, which encountered some problems, led to the failure of PDF generation, such as the emergence of blank files or too much data on a page, are due to the lack of well-formatted. Especially the paging problem, I feel that there is very little information. Except for some information on Stack Overflow, the Chinese community can hardly see it. Attached is the revised version. rasterize.js to explain:
var page = require('webpage').create(), system = require('system'), address, output, size; if (system.args.length < 3 || system.args.length > 5) { console.log('Usage: rasterize.js URL filename [paperwidth*paperheight|paperformat] [zoom]'); console.log(' paper (pdf output) examples: "5in*7.5in", "10cm*20cm", "A4", "Letter"'); phantom.exit(1); } else { address = system.args[1]; output = system.args[2]; /*size of browser*/ page.viewportSize = { width: 600, height: 600 }; /* if (system.args.length > 3 && system.args[2].substr(-4) === ".pdf") { size = system.args[3].split('*'); page.paperSize = size.length === 2 ? { width: size[0], height: size[1], margin: '0px' } : { format: 'A4', orientation: 'portrait', margin: '1cm' }; } */ /* ie and chrome view diffrent format of pdf */ page.settings.userAgent = 'Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.117 Safari/537.36'; page.paperSize = { format: 'A4', orientation: 'portrait', margin: '0.8cm' }; page.zoomFactor = 1; page.settings.loadImages = true; //some question about the page language page.open(address, function (status) { if (status !== 'success') { console.log('Unable to load the address!'); } else { //page.render(output); //phantom.exit(); window.setTimeout(function () { page.render(output); phantom.exit(); }, 200); //setting the time is enough to loading the page. document.ready } }); }
PDF formatting
About the settings properties of page s, Here Can understand, more in-depth can understand the ____________ WebPage Module.
The settings we need are basically page format, zooming, loading pictures and so on, but there are some exceptions, which are explained below.
1 page.paperSize = { format: 'A4', orientation: 'portrait', margin: '0.8cm' };
The setting code of the official example is commented out, because only three parameters are passed in until. pdf. If written in general mode, they can certainly be passed in as external parameters.
Form: A4 paper, you can set "5in*7.5in", "10cm*20cm", "Letter" and so on.
orientation: paper is vertical, or landscape
margin: {left:'0.8cm', top:'0.8cm', right:'0.8cm', bottom:'0.8cm'}
1 page.zoomFactor = 1; 2 page.settings.loadImages = true;
zoomFactor: Page Scaling Ratio
loadImages: Page load images
1 page.settings.userAgent = 'Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.117 Safari/537.36';
This setting is relatively uncommon, not mentioned in the general example, because it is found that the format of pdf generated by using chrome and IE is somewhat different (in terms of paging). Because of the preference for Chrome browsing format, this value is set to solve this inconsistency problem.
The setTimeout method in page.open waits for the page to execute js and regenerate pdf. Of course, we don't know how long it will take for js to execute. Actually, I've tried to load content in ajax mode, but that's the problem.
For more information, you can refer to the header and footer and page number annotation. Here.
PDF paging
For paging, better control, no code (js) settings, page style can be used:
style = "page-break-after: always;"
To control the size of each page, use < div style= "page-break-after: always;" > content </div>.
Choose more style = "page-break-before: always;" and style = "page-break-in: avoid;" which prevents content from splitting into two pages.