如何将图像从本地写入PDFkit?

时间:2018-03-21 07:30:30

标签: node.js

以下是从本地文件夹将图像写入pdf的代码。执行后我无法得到以下错误的原因。在pdf中没有什么是打印

doc.pipe(fs.createWriteStream("pdfTemp/" + name_file + ".pdf"));
       // callback();
        // PDF Creation logic goes here
        doc.fontSize(15).text('Wally Gator !', 50, 50);

        console.log("inside PDF3");
        // Set the paragraph width and align direction
        doc.text('www.', {
            width: 410,
            align: 'left'
        });

        var newVAr = "newTemp/" + name_file + ".png";

        console.log("inside PDF");

        doc.image(newVAr, 325, 214, {
            fit: [200, 200]

        })

            .stroke();
        console.log("inside PDF2");
        callback();
        doc.end();

执行上面的代码后,PDF正在生成,但是我遇到了错误。

Error: stream.push() after EOF
    at readableAddChunk (_stream_readable.js:156:17)
    at PDFDocument.Readable.push (_stream_readable.js:134:10)
    at PDFDocument._write (R:\pdftest\node_modules\pdfkit\js\document.js:156:12)
    at PDFReference.finalize (R:\pdftest\node_modules\pdfkit\js\reference.js:81:21)
    at PDFReference.finalize (R:\pdftest\node_modules\pdfkit\js\reference.js:10:59)
    at PDFReference.end (R:\pdftest\node_modules\pdfkit\js\reference.js:74:21)
    at PNGImage.finalize (R:\pdftest\node_modules\pdfkit\js\image\png.js:83:15)
    at R:\pdftest\node_modules\pdfkit\js\image\png.js:114:28
    at Deflate.onEnd (zlib.js:227:5)
    at emitNone (events.js:91:20)

1 个答案:

答案 0 :(得分:1)

           doc = new PDF();
           doc.pipe(fs.createWriteStream("pdfTemp/" + name_file + ".pdf"));
           // callback();
            // PDF Creation logic goes here
            doc.fontSize(15).text('Wally Gator !', 50, 50);

            console.log("inside PDF3");
            // Set the paragraph width and align direction
            doc.text('www.', {
                width: 410,
                align: 'left'
            });

            var newVAr = "newTemp/" + name_file + ".png";

            console.log("inside PDF");

            doc.image(newVAr, 325, 214, {
                fit: [200, 200]

            })

                .stroke();
            console.log("inside PDF2");
            callback();
            doc.end();

需要声明doc = new PDF();在循环内