我需要将多个图像转换为单个 PDF。我能够为一张图像创建 PDF。当我有多个图像时,问题就来了。如何创建多页 PDF。
Gau*_*pal 10
我通过下面的代码得到了我想要的输出。
PDFDocument = require('pdfkit');
fs = require('fs');
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('./test.jpg', {
fit: [500, 400],
align: 'center',
valign: 'center'
});
doc.addPage()
.image('./1.png', {
fit: [500,400],
align: 'center',
valign: 'center'
});
doc.end()
Run Code Online (Sandbox Code Playgroud)
这是正确的方法:
var pdf = new (require('pdfkit'))({
autoFirstPage: false
});
var img = pdf.openImage('./myImage.jpg');
pdf.addPage({size: [img.width, img.height]});
pdf.image(img, 0, 0);
pdf.end();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11287 次 |
| 最近记录: |