Sunday, 16 April 2017

Creating and rendering a pdf from an ejs template

When I get a certain route called new page, I am trying to create a pdf file from an ejs file using the node-html-pdf or something like it, but with no success.

new page

  router.get('/newPage', function(req, res) {

    ejs.renderFile('secure/new_report_print', {
        data: {
            qryWaterLab: qryWaterLab,
            qryWaterContact: qryWaterContact,
            qryResult: qryResult,
            moment: moment,
            lSystemID: req.query.lSystemID,
            LabReportID: req.query.LabReportID
        },
        function(err, result) {
            // render on success
            if (result) {
                html =
                result;
            } else {
                res.end('An error occurred');
                console.log(err);
            }
          }
        });

        var options = {
            filename: 'report.pdf',
            format: 'A4',
            orientation: 'portrait',
            directory: './pdfs/',
            type: "pdf"
        };

        pdf.create(html, options).toFile(function(err, res) {
            if (err)
                return console.log(err);
            console.log(res);
        });



via user7659932

No comments:

Post a Comment