1 year ago

#349113

test-img

Ana Cláudia Faria

Generate PDF with pdfkit

I'm changing the configuration of the website regarding the a pdf creation and download by the user. The idea here is that the pdf is created from data from the DB and it's not stored, but directly offered to the user for download. The original website was using html-pdf, but I'm unable to install it successfully, as I always have issue with phantomjs, phantomjs-prebuilt, etc. I tried using pdfkit, but i'm not successful, and since I'm not very experienced the problem certainly is something I'm not quite getting.

Can someone give me some pointers, please?


let     express             = require('express'),
        router              = express.Router(),
        asyncMiddleware     = require('../utils/asyncMiddleware'),
        auth                = require('../middleware/authentication'),
        // pdf                 = require('html-pdf'),
        blobStream          = require('blob-stream'),
        pdf                 = require('pdfkit'),
        juice               = require('juice'),
        {User, Report}      = require('../models');







router.get('/:reportId?', auth.isLoggedIn, asyncMiddleware( async(req, res) => {

    let reqUser = req.user;
    //get the report
    let report = await Report.findById( req.params.reportId ).populate('ownerManagerId');
    
    if( !report ) {
        req.flash('error', req.__('flash.error.reportNotFound') );
        return res.redirect('back');
    }

    //check if user on the request is the user of the report or is the group admin stored when the report was generated 
    if(  !reqUser._id.equals(report.ownerId) && !reqUser._id.equals(report.ownerManagerId) && req.user.role !=="admin" ){
        req.flash('error', req.__('flash.error.forbiddenAccess') );
        return res.redirect('back');
    }
   
    //report data
    let data = {
        layout: 'pdf_layout',
        styles: [],
        scripts: [],
        user: reqUser,
        lang: req.lang,
        pageTitle: req.__('pageTitle-calculator-report'),
        reportOwnerManager: report.ownerManagerId,
        reportDate: formatDateInReport( report.createdAt ),
        calcData: report.inputs,
        output: report.outputs
    }

    //add some helper functions to ejs views
    res.locals.formatNum = formatNum;
    res.locals.findInputCorr = findInputCorr;
   
    res.render('calculadora/relatorioPdf', data, (err1, html)=>{
        if(err1) return res.send(err1);
        //inline all css on html
        let inlineHtml = juice(html);
        
        //set pdf options
        var pdfOptions = { 
            width: "297mm",
            height: "420mm",        // allowed units: mm, cm, in, px
            //format: "A4",        // allowed units: A3, A4, A5, Legal, Letter, Tabloid
            //orientation: "portrait",
            border: {
                top: "30mm",            // default is 0, units: mm, cm, in, px
                right: "20mm",
                bottom: "0",
                left: "20mm"
            }
        };

        // //create the pdf and stream it to response   
        // pdf.create(inlineHtml, pdfOptions).toStream( (err2, stream)=>{
        //     if(err2) return res.send(err2);
        //     //set headers so the file is downloaded instead of shown in browser
        //     res.writeHead(200, {
        //         'Content-Type': 'application/pdf',
        //         'Content-disposition': `attachment; filename=relatorio-${report.id}.pdf`
        //     });
        //     //redirect stream to response
        //     stream.pipe(res);
        // });

        //set headers so the file is downloaded instead of shown in browser
        const stream = res.write(200, {
            'Content-Type': 'application/pdf',
            'Content-disposition': `attachment; filename=relatorio-${report.id}.pdf`
        });

        buildPDF(
            (chunk) => stream.write(chunk),
            () => stream.end()
        );


        function buildPDF() {
            const doc = new pdf();
            doc.on('data', dataCallback);
            doc.end('data', endCallback);
            doc.pipe(res);
            doc.end();
        }
    });//render

}))//get´´´

node.js

pdf-generation

pdfkit

html-pdf

0 Answers

Your Answer

Accepted video resources