简体   繁体   English

Node.js读取外部图像并以pdf格式编写

[英]Nodejs read an external image and write as pdf

I have a html file where I have some variable like {Ticket}. 我有一个html文件,其中有一些变量,例如{Ticket}。 In nodejs I am trying to replace that variable into an image what I have. 在nodejs中,我试图将变量替换为图像。 So basically the output will be a pdf file. 因此,基本上输出将是pdf文件。

So far now my code looks like this 到目前为止,我的代码看起来像这样

ticket.html looks like this ticket.html看起来像这样

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    <table>
        <tr>
            <td></td>
            <td>{Ticket}</td>
        </tr>
    </table>
</body>
</html>

var html = fs.readFileSync('ticket.html', 'utf8'); //html file
var GetImage = fs.readFileSync('QRTicket.png'); //image file
var customHtml = customHtml.replace('{Ticket}', GetImage);
pdf.create(customHtml, options).toFile('QRImage.pdf', function(err, res) {
    if (err) return console.log(err);
    console.log(res); 
});

But its creating a blank pdf file. 但是它创建了一个空白的pdf文件。 I am using html-pdf to generate pdf file. 我正在使用html-pdf生成pdf文件。 I am struggling to get this done but its not working. 我正在努力做到这一点,但没有成功。 So any help and suggestions will be really appreciable 因此,任何帮助和建议都将非常可观

You need to insert the image into the page differently (using the file:// protocol). 您需要将图像不同地插入页面(使用file://协议)。 There is actually an example on the project's GitHub page that shows you how to do this here and here . 实际上,该项目的GitHub页面上有一个示例,向您展示了如何在此处此处执行此操作。

In your case, this would translate to the following: 就您而言,这将转换为以下内容:

<!DOCTYPE html>
<html>
<head>
  <title></title>
</head>
<body>
  <table>
    <tr>
      <td></td>
      <td><img src="{Ticket}"></td>
    </tr>
  </table>
</body>
</html>

and the JS file: 和JS文件:

const fs = require('fs');
const pdf = require('html-pdf');

const html = fs.readFileSync('./ticket.html', 'utf8');
const customHtml = html.replace('{Ticket}', `file://${require.resolve('./QRTicket.png')}`);

pdf.create(customHtml).toFile('QRImage.pdf', (err, res) => {
  if (err) return console.log(err);
  console.log(res);
});

Edit: 编辑:

This example assumes the following folder structure. 本示例假定以下文件夹结构。

.
├── index.js
├── node_modules
├── package.json
├── package-lock.json
├── QRTicket.png
└── ticket.html

Run with the command node index.js and it will generate the desired PDF with the image. 使用命令node index.js运行,它将与图像生成所需的PDF。

I have modified my ticket.html and now I want to search and replace multiple strings at a one place. 我已经修改了ticket.html,现在我想在一处搜索并替换多个字符串。 So now my ticket.html is like this 所以现在我的ticket.html就是这样

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    <table>
        <tr>
            <td></td>
            <td><img src="{{Ticket}}"></td>
        </tr>
        <tr>
            <td>asdf</td>
            <td>tyui</td>
        </tr>
    </table>
</body>
</html>

Now the express.js looks like this 现在express.js看起来像这样

const fs = require('fs');
const pdf = require('html-pdf');
const replaceOnce = require('replace-once');

const html = fs.readFileSync('ticket.html', 'utf8');
const image = 'file:///'+path.join(appRoot.path, 'file.png');

var find = ['{{Ticket}}', 'asdf', 'tyui'];
var replace = [image, 'Wills', 'Smith'];

const customHtml = replaceOnce(html, find, replace, 'gi')

pdf.create(customHtml).toFile('QRImage.pdf', (err, res) => {
  if (err) return console.log(err);
  console.log(res);
});

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

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