错误:写完后pdfkit

时间:2017-05-03 10:31:09

标签: javascript node.js pdfkit

首次在服务器端生成Pdf文件但是当试图创建另一个文件时,它会在服务器端显示写入错误。

pdf.pipe(fs.createWriteStream('./dist/files/tmpPdfFile/' + randomName + '.pdf'));

res.writeHead(200, {
    'Content-Type': 'application/pdf',
    'Access-Control-Allow-Origin': '*',
    'Content-Disposition': 'attachment; filename=output.pdf'
});

// Pipe generated PDF into response
pdf.pipe(res);

pdf.addPage();

var count = 0

// if(imagesArr.length>0){
imagesArr.forEach(function (image) {
    if (imagesArr.indexOf(image) === 0) {
        pdf.fontSize(16).fillColor("darkgray")
        pdf.text('ACCIDENT IMAGES', 10, 10, {
            bold: true,
            align: 'center'
        });
    }
    if (count < 2) {
        if (count < 1) {
            pdf.image(image.body, 30, 40, {
                width: 500,
                height: 300
            });
            pdf.fontSize(9).fillColor('black');
            pdf.text('Image Type: ' + image.imgType + ' Date: image.dateTime  Time: image.dateTime  Address: image.dateTime', 30, 342);
        } else {
            pdf.image(image.body, 30, 360, {
                width: 500,
                height: 300
            });
            pdf.fontSize(9).fillColor('black');
            pdf.text('Image Type: ' + image.imgType + ' Date: image.dateTime  Time: image.dateTime  Address: image.dateTime', 30, 660);
        }
        count++;
    } else {
        count = 0
        pdf.addPage();
    }

    if (imagesArr.indexOf(image) === imagesArr.length - 1) {

        pdf.end();
        // window.open(pdf.dataURI());
        stream.on('finish', function () {
            // blob = stream.toBlob('application/pdf')

            // or get a blob URL for display in the browser
            // window.open('output.pdf'); 
        })

0 个答案:

没有答案
相关问题