javascript - Phantomjs merge PDF's -
i using phantomjs generate pdf's url's. works fine, every url get's transferred seperate pdf. want bunch of url's transferred 1 pdf several page. ideas how done (preferable in javacsript)?
thnx,
henk
code used now:
phantomjs pdf_export_urls.js
pdf_export_urls.js:
"use strict"; var renderurlstofile, arrayofurls, system; var file_format = 'pdf'; var base_url = 'http://'; system = require("system"); /* render given urls @param array of urls render @param callbackperurl function called after finishing each url, including last url @param callbackfinal function called after finishing */ renderurlstofile = function(urls, callbackperurl, callbackfinal) { var getfilename, next, page, retrieve, urlindex, webpage; urlindex = 0; webpage = require("webpage"); page = null; getfilename = function(url) { var filename = ""; filename = url // remove http/https prefix .replace(/https?:\/\//gi, '') // convert slashes , dots underscores. .replace(/[\/:.]+/gi, '_'); return filename.substr(filename.length - 4) + "." + file_format; }; next = function(status, url, file) { page.close(); callbackperurl(status, url, file); return retrieve(); }; retrieve = function() { var url; if (urls.length > 0) { url = urls.shift(); urlindex++; page = webpage.create(); page.viewportsize = { width: 1280, // height of 1 autosizes content height. height: 1 }; var full_url = base_url + url; page.settings.useragent = "phantom.js bot"; return page.open(full_url, function(status) { var file; file = getfilename(url); // file=file.slice(4,15) + '.pdf'; if (status === "success") { // // clip rendered output height of content. // var height = page.evaluate(function() { return document.body.offsetheight }), // width = page.evaluate(function() { return document.body.offsetwidth }); // console.log("page height: " + height); // console.log("page width: " + width); // page.cliprect = {top: 0, left: 0, width: width, height: height}; return window.settimeout((function() { page.render('output/' + file); return next(status, url, file); }), 200); } else { return next(status, url, file); } }); } else { return callbackfinal(); } }; return retrieve(); }; arrayofurls = null; var fs = require('fs'); var url_list = fs.read('url_list.txt'); //console.log('read data:', url_list); arrayofurls = url_list.split(/[\r\n]+/); // if (system.args.length > 1) { // arrayofurls = array.prototype.slice.call(system.args, 1); // } else { // console.log("usage: phantomjs render_multi_url.js [domain.name1, domain.name2, ...]"); // arrayofurls = ["www.google.com", "www.bbc.co.uk", "phantomjs.org"]; // } renderurlstofile(arrayofurls, (function(status, url, file) { var full_url = base_url + url; if (status !== "success") { return console.log("unable render '" + full_url + "'"); } else { return console.log("rendered '" + full_url + "' @ '" + file + "'"); } }), function() { return phantom.exit(); });
Comments
Post a Comment