在html-pdf用法未呈现图像后进行EJS-view导航 [英] Sails EJS-view after html-pdf usage does not render image

查看:96
本文介绍了在html-pdf用法未呈现图像后进行EJS-view导航的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要能够呈现html视图(没有新内容)并向最终用户提供与PDF文件相同的视图.

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.

第一种情况对我而言效果很好:我设法渲染了一个简单的html5文档以及一张图片两次(base64版本+标准方式).

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-to-pdfhtml-pdf,当我尝试将其转换为PDF文件时,<img>标记的非base64版本无法正常工作,并且在PDF文件中被表示为缺少资源

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.

我正在使用的代码如下:

The code I'm using is the following :

控制器:

//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);
  }
});

查看:

<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" />

我已经仔细阅读了有关Grunt任务和资产管理的Sails规范,因此给定的images/ymca.jpg确实存在并且可以从浏览器访问.我仍然不知道为什么PDF无法呈现它.

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. 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

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

查看

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

使用html-pdf在pdf上呈现时,它不适用于绝对路径.

while render on pdf using html-pdf, it does not work with absolute path.

这篇关于在html-pdf用法未呈现图像后进行EJS-view导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆