如何修复重复的canvas.toBuffer或canvas.createPdfStream内容

时间:2019-05-08 20:54:19

标签: node-canvas

我在循环中使用canvas.toBuffer来为不同的内容生成pdf,但是所有生成的pdf都具有相同的内容。下面是我正在使用的循环的一个片段。

for (const d of data) {
    context.clearRect(0, 0, imageWidth, imageHeight);
    context.drawImage(image, 0, 0, imageWidth, imageHeight);
    context.fillText(d.name.toUpperCase(), 1685, 1556);
    fs.writeFileSync(`./assets/certs/${d.email}.pdf`,canvas.toBuffer());
}

数据Array<{name:String, email:String}>类型。 该名称应该在图像上打印每个用户的名称,但是事实证明,生成的所有pdf都只有第一个用户的名称。

我尝试从画布创建ReadStream,但最终遇到相同的问题。是我做错了什么还是错过了什么?

1 个答案:

答案 0 :(得分:0)

这有点像节点画布中的错误。在其上调用toBuffer()https://github.com/Automattic/node-canvas/issues/1195)后,无法重用PDF或SVG画布。

作为一种解决方法,您可以在循环的每次迭代中制作一个新的画布。