[英]phantomjs - render HTML to PDF
I have problems with rendering a HTML file to a PDF file. 我在将HTML文件渲染为PDF文件时遇到问题。 I pass two arguments to the command line.
我将两个参数传递给命令行。 The first is the HTML input file and the second the PDF output
第一个是HTML输入文件,第二个是PDF输出
/var/bin/phantomjs-2.1.1-linux-x86_64/bin/phantomjs /var/www/nodejs/html_to_pdf.js /root/input.html /root/hello.pdf
var page = require('webpage').create(),
args = require('system').args,
f = require('fs').open(args[1], 'r');
page.paperSize = {
format : 'A4',
orientation : 'portrait',
margin : {
top : '1cm',
left : '1cm',
bottom : '1cm',
right : '1cm'
}
};
page.content = f.read();
page.setContent(page.content, page);
page.render(args[2]);
phantom.exit();
No errors is returned and no output PDF file? 没有返回错误并且没有输出PDF文件?
Here is the input file 这是输入文件
http://www.filedropper.com/input_3 http://www.filedropper.com/input_3
I'd suggest rewrite to page.open
a file: 我建议重写为
page.open
文件:
var page = require('webpage').create();
var args = require('system').args;
var fs = require('fs');
function getFileUrl(str) {
var pathName = fs.absolute(str).replace(/\\/g, '/');
// Windows drive letter must be prefixed with a slash
if (pathName[0] !== "/") {
pathName = "/" + pathName;
}
return encodeURI("file://" + pathName);
};
page.paperSize = {
format : 'A4',
orientation : 'portrait',
margin : {
top : '1cm',
left : '1cm',
bottom : '1cm',
right : '1cm'
}
};
page.open(getFileUrl(args[1]), function(){
page.render(args[2]);
phantom.exit();
});
getFileUrl
is from this answer getFileUrl
是从这个答案
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.