【问题标题】:Node.js: Adding SVG to a specific page of PDF by pdfmakeNode.js:通过 pdfmake 将 SVG 添加到 PDF 的特定页面
【发布时间】:2018-09-10 09:49:41
【问题描述】:

我正在尝试通过pdfmake 创建 PDF 内容。但是,pdfmake 不支持在 v0.1.38 中添加 SVG。 所以我使用SVG-to-PDFKit 来实现这一点。这是我的代码:

var printer = new pdfMakePrinter(fontDescriptors);

var doc = printer.createPdfKitDocument(pdfDoc);

SVGtoPDF(doc, s.data, s.x, s.y, s.options); // add this line for SVG-to-PDFKit to insert SVG while s contain SVG data

var chunks = [];
var result;

doc.on('data', function(chunk) {
    chunks.push(chunk);
});
doc.on('end', function() {
    result = Buffer.concat(chunks);
    resolve(result);
});
doc.end();

SVG 已成功添加到最后一页。如何将 SVG 添加到特定页面?

【问题讨论】:

    标签: node.js svg pdfmake node-pdfkit


    【解决方案1】:

    阅读源代码和document后,添加{ bufferPages: true }完美解决您的需求。

    var printer = new pdfMakePrinter(fontDescriptors);
    
    var doc = printer.createPdfKitDocument(pdfDoc, { bufferPages: true }); // magic here!
    doc.switchToPage(x); // to page x, where x start from 0
    
    SVGtoPDF(doc, s.data, s.x, s.y, s.options); // add this line for SVG-to-PDFKit to insert SVG while s contain SVG data
    
    var chunks = [];
    var result;
    
    doc.on('data', function(chunk) {
        chunks.push(chunk);
    });
    doc.on('end', function() {
        result = Buffer.concat(chunks);
        resolve(result);
    });
    doc.end();
    

    【讨论】:

    • 太棒了,完美的答案!
    猜你喜欢
    • 1970-01-01
    • 2015-05-19
    • 2017-05-20
    • 1970-01-01
    • 1970-01-01
    • 2023-01-15
    • 2017-10-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多