Wednesday, 3 May 2017

Error: Write After End pdfkit

For the First Time It Generate Pdf file at server side But when to try to create another it show an error of write after end in server side.

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'); 
        })



via shubham arora

No comments:

Post a Comment