【发布时间】:2015-09-15 15:10:58
【问题描述】:
我正在尝试使用 phantom-html2pdf 将我的 html 文件转换为 PDF。我的html代码是。
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
<link href='https://fonts.googleapis.com/css?family=Lobster' rel='stylesheet' type='text/css'>
<style>
p.serif {
font-family: "Times New Roman", Times, serif;
}
p.sansserif {
font-family: Arial, Helvetica, sans-serif;
}
#i{
font-family: 'Indie Flower', cursive;;
}
#h{
font-family: Papyrus;}
#l{
font-family: 'Shadows Into Light', cursive;}
#s{
font-family: 'Sigmar One', cursive;}
#o{
font-family: 'Orbitron', sans-serif;}
</style>
</head>
<body>
<img src="https://upload.wikimedia.org/wikipedia/commons/c/c9/Moon.jpg" style="width:304px;height:228px;">
<h1>CSS font-family</h1>
<p class="serif">This is a paragraph, shown in the Times New Roman font.</p>
<p class="sansserif">This is a paragraph, shown in the Arial font.</p>
<div id="i">a quick brown fox jumps over the lazy dog </div>
<div id="h">a quick brown fox jumps over the lazy dog</div>
<div id="l">a quick brown fox jumps over the lazy dog</div>
<div>a quick brown fox jumps over the lazy dog</div>
<div id="s">a quick brown fox jumps over the lazy dog</div>
</body>
</html>
我想使用 node 模块使用 phantom-html2pdf 将相同的 html 转换为 PDF 代码是
var pdf = require('phantom-html2pdf');
var options = {
"html" : "b.html",
"css" : "style.css",
"paperSize":{ format: 'Letter', orientation: 'portrait', border: '1cm' }};
/* "js" : "Path to additional JavaScript file",
"runnings" : "Path to runnings file. Check further below for explanation.",
"deleteOnAction" : true/false (Deletes the created temp file once you access it via toBuffer() or toFile()) */
pdf.convert(options, function(result) {
/* Using a buffer and callback */
result.toBuffer(function(returnedBuffer) {});
/* Using a readable stream */
var stream = result.toStream();
/* Using the temp file path */
var tmpPath = result.getTmpPath();
/* Using the file writer and callback */
result.toFile(id[0]+'.pdf', function() {console.log(tmpPath)});
});
但图像未以 PDF 格式显示。如何让图像以 PDF 格式显示。 有没有其他选项可以将 html 文件转换为 PDF?
【问题讨论】:
-
可能是 ssl 问题,用 --ssl-protocol=any 试试看是否有帮助。
-
您好,感谢您的回复,能否请您在我的代码中提供一些调用协议的代码
-
这是 phantomjs 的命令行选项
-
即使我尝试转换本地图像。但它没有转换为 pdf
标签: javascript node.js phantomjs html2pdf