0

I have an HTML(node ejs template) file. which contains some images in it. the HTML page is working fine. when I open the file in the browser but facing image issue when, I am converting the HTML into pdf using the node html-pdf

Repositry structure 
views-
 ---pages
 ---img
      invoice.ejs
 ---createPDF-html-pdf.js

invoive.ejs

<body>
    <img src=<%= dbObject.headerImg %> alt="Creating Email Magic" width="800"  height="200" style="display: block;" />
    <table border="0" cellpadding="0" cellspacing="0" width="100%">
        <tr>
            <td
                style="border-top: 2px solid #898989; width: 33%;  padding: 6px 0 6px 0; color: #153643; font-family: Arial, sans-serif; font-size: 12px; line-height: 16px;">
                House No: <%= dbObject.houseNumber %>
            </td>

        </tr>
    </table>
</body>

createPDF-html-pdf.js

let ejs = require("ejs");
let pdf = require("html-pdf");
let path = require("path");

init()
function init() {
    let dbObject = {
        headerImg: path.join( __dirname, 'views', 'img' , 'header.png').replace(/\\/g, '/'),
  // D:/ejs-to-pdf/views/img/header.png
        houseNumber: " R-0001",
    }


    ejs.renderFile(path.join(__dirname, 'views/pages', "invoice.ejs"), {
        dbObject // passing object for custom values 
    }, (err, data) => {
        if (err) {
            console.log(`err  ${err}`)
        } else {
            var options = {
                format: 'A4'
            };
            pdf.create(data, options).toFile("pdf/pdfFileName.pdf", function (err, data) { // PDF file save in PDF folder 
                if (err) {
                    console.log(`err  :${err}`)
                } else {
                    console.log(` file genrated successfullt   ` , JSON.stringify(data))
                }
            });

        }
    })
}

0 Answers0