从phantom-html2pdf中的html转换时,图像未以PDF格式显示

时间:2015-09-15 15:10:58

标签: javascript node.js phantomjs html2pdf

我正在尝试使用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>

我想使用节点模块使用phantom-html2pdf在PDF中转换相同的html 代码是

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吗?

2 个答案:

答案 0 :(得分:0)

我使用了图像的完整路径。 例如:

<img src="file:///Users/linden/Desktop/earth.jpg" />

答案 1 :(得分:0)

试试这个,我在通过参考以下转换 HTML 和 ejs 的同时向 PDF 添加了一个图像,

proto-node-pdf