我正在使用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();
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句