如何使用任何npm包从数据库生成和下载具有动态图像路径的pdf



我正在使用express mongodb-ejs构建一个软件。我已经尝试了一些npm软件包来生成和下载pdf。但是,我无法生成具有动态图像路径的pdf。

我用过html pdf包,看起来不错。但是,它无法从数据库中呈现动态图像路径。这是我使用html pdf包的屏幕截图。

这是我询问这个与软件包相关的问题的详细问题链接。

有人能为我提供关于这个问题的详细解决方案吗?任何npm包都应该是好的,只要它能帮助我使用express下载pdf。

您可以使用一个名为pdfkit的流行节点模块。请运行以下程序并检查它。您需要在运行脚本的同一文件夹中有一个名为pro.jpg的映像。它将生成一个output.pdf文件。

以下是示例:

const PDFDocument = require('pdfkit');
const fs = require('fs');
// Create a document
const doc = new PDFDocument();
// Pipe its output somewhere, like to a file or HTTP response
// See below for browser usage
doc.pipe(fs.createWriteStream('output.pdf'));
// Add an image, constrain it to a given size, and center it vertically and horizontally
doc.image('pro.jpg', {
fit: [250, 300],
align: 'center',
valign: 'center'
});
// Add another page
doc
.addPage()
.fontSize(25)
.text('Here is some vector graphics...', 100, 100);
// Draw a triangle
doc
.save()
.moveTo(100, 150)
.lineTo(100, 250)
.lineTo(200, 250)
.fill('#FF3300');
// Apply some transforms and render an SVG path with the 'even-odd' fill rule
doc
.scale(0.6)
.translate(470, -380)
.path('M 250,75 L 323,301 131,161 369,161 177,301 z')
.fill('red', 'even-odd')
.restore();
// Add some text with annotations
doc
.addPage()
.fillColor('blue')
.text('Here is a link!', 100, 100)
.underline(100, 100, 160, 27, { color: '#0000FF' })
.link(100, 100, 160, 27, 'http://google.com/');
// Finalize PDF file
doc.end();

最新更新