简体   繁体   English

在html-pdf用法未呈现图像后进行EJS视图查看

[英]Sails EJS-view after html-pdf usage does not render image

I need to be able to render html views (nothing new there) and to provide the same views as PDF file to the final user. 我需要能够呈现html视图(那里没有新内容)并向最终用户提供与PDF文件相同的视图。

The first case works perfectly on my side : I managed to render a simple html5 document aswell as an image two times (the base64 version + the standard way). 第一种情况对我而言效果很好:我设法渲染了一个简单的html5文档以及一张图像两次(base64版本+标准方式)。

Unfortunately, when I try to convert it to a PDF file thanks to html5-to-pdf or html-pdf , the non-base64 version of the <img> tag does not work and is signaled as a missing resource within the PDF File. 不幸的是,由于html5-to-pdfhtml-pdf ,当我尝试将其转换为PDF文件时, <img>标记的非base64版本无法正常工作,并且在PDF文件中被表示为缺少资源。

The code I'm using is the following : 我正在使用的代码如下:

Controller : 控制器:

//res is the response parameter provided to the controller

var pdf = require('html-pdf');

var variables = {

};

ejs.renderFile('./views/samplepdf.ejs', variables, function(err, result) {
  // render on success
  if (result) {
    html = result;

    pdf.create(html).toStream(function(err, stream){
      res.contentType("application/pdf");
      stream.pipe(res);
    });

  }
  // render or error
  else {
    res.end('An error occurred');
    console.log(err);
  }
});

View : 查看:

<h2>Image</h2>
<img class="smaller" src="data:image/jpeg;base64,/validbase64ButIHadToSaveBecauseCharactersLimitExceeded" alt="Sample image" />
<img class="smaller" src="images/ymca.jpg" alt="Sample image #2" />

I've carefully read the Sails specification about Grunt tasks and assets management, therefore the given images/ymca.jpg does exist and can be access from the browser. 我已经仔细阅读了有关Grunt任务和资产管理的Sails规范,因此给定的images/ymca.jpg确实存在并且可以从浏览器访问。 I still do not know why does the PDF does not render it. 我仍然不知道为什么PDF无法呈现它。

all you have to do is use full path of image while render on pdf controller 您要做的就是在pdf控制器上渲染时使用完整的图像路径

var variables = {
  path: sails.config.appPath + 'assets/images/ymca.jpg'
};

view 视图

<img class="smaller" src="<%= path %>" alt="Sample image #2" />

while render on pdf using html-pdf, it does not work with absolute path. 使用html-pdf在pdf上呈现时,它不适用于绝对路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM