【问题标题】:Image is not showing in PDF when converting from html in phantom-html2pdf从 phantom-html2pdf 中的 html 转换时,图像未显示在 PDF 中
【发布时间】: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


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    试试这个,我在转换HTML和ejs的同时,参考下面的方法,在PDF中添加了一张图片,

    proto-node-pdf

    【讨论】:

      猜你喜欢
      • 2019-01-18
      • 1970-01-01
      • 1970-01-01
      • 2019-12-04
      • 2022-11-02
      • 1970-01-01
      • 2015-10-26
      • 1970-01-01
      • 2021-11-19
      相关资源
      最近更新 更多