将html转换为pdf后,img仅显示白色空白方格

问题描述

当我在 Windows 上使用 html-pdf 模块将 html 转换为 pdf 时,我的 img 标签显示像这样的白色方形

enter image description here

为什么会这样? 我的代码很基础

module.exports = (formData) => {
    const today = new Date();

    return `

<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>

<h1>My First heading</h1>
<img src="file:///C:/Users/User/Desktop/htmlIWithPdf/assets/bth.png" border="1" width="100" height="100" >

<p>My first paragraph.</p>

</body>
</html>

`
}

我用来将 html 转换为 pdf 的模块:

const pdf = require("html-pdf");
const fs = require("fs");
const pdfCreate = (req,res,next) => {
  const pdfTemplate = require("./index");
  var options = { format: "A4",orientation: "portrait" };

  pdf.create(pdfTemplate(),options).toStream(function (err,stream) {
    if (err) return next(err);

    stream.pipe(fs.createWriteStream("./foo.pdf"));

    // res.status(200);
    // res.send("Success");
  });
};

// module.exports.pdfCreate = pdfCreate;

pdfCreate()

解决方法

尝试用反斜杠替换正斜杠: <img src="file:\\\C:\\Users\\User\\Desktop\\htmlIWithPdf\\assets\\bth.png" border="1" width="100" height="100" >