var page = require('webpage').create();//获取webpage var fs = require('fs'); var system = require('system'), address,filename; if (system.args.length === 1) { console.log('Usage: URL error'); phantom.exit(); } address = system.args[1];//请求的路径 address = address.replace(/%26/g, '&'); filename = (new Data()).getTime(); page.viewportSize = { width: 750, } page.paperSize = { //生成A4大小的PDF文件 format: 'A4', orientation: 'portrait', margin: '0.8cm' } page.open(address, function(status) { var info = fs.read('tmp.txt');//读取DOM字符串 var result = page.evaluate(function(info) { try { document.querySelector('#dom').innerHTML = info;//将dom字符串拼接 } catch (e) { console.log(e); } return document.querySelector('#dom').innerHTML;//返回页面 }, info); setTimeout(function() { page.paperSize = { format: 'A4', orientation: 'portrait', margin: '0.8cm' }; page.viewportSize = { width: 750, }; //生成PDF文件 page.render('pages/' + filename + '.pdf', { format: 'pdf', quality: '100' }); console.log('http://example.com/pages/' + filename + '.pdf');//返回PDF文件的访问路径 phantom.exit(); }, 500); })
OK,以上就是全部接口的内容,全部的代码可以访问https://github.com/kim095/node-phantom进行下载.希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
您可能感兴趣的文章: