htmlnode.jsexpressejs

Nodejs how to generate a pdf file from ejs file


I'm writing a nodejs method which I want to convert an ejs file to a pdf file

router.get("/uknow", (req, res) => {
    ejs.renderFile(path.join(__dirname, "../../views/resultat.ejs"),  (err, data) => {
    if (err) {
          res.send(err);
    } else {
        
        pdf.create(data).toFile("report.pdf", function (err, data) {
            if (err) {
                res.send(err);
            } else {
                res.send("File created successfully");
            }
        });
    }
});
})

this is the method which I wrote and I get this response in post man

{
    "path": "C:\\Users\\HP\\Desktop\\exemple CRUD\\blog_api\\views\\resultat.ejs"
}

i print the error and this is what i get ReferenceError:

C:\Users\HP\Desktop\exemple CRUD\blog_api\views\resultat.ejs:41
    39| </head>
    40| <body class="spacer">
 >> 41|     <h2 class="text-center my-3"><%= data[0].imei.name %></h2>
    42|     <h5 class="text-center my-3">Imei -<%= data[0].imei.name %> </h5>
    43|     <h3 class="text-center my-3">Resultat des tests</h3>
    44|     <hr>

data is not defined
    at eval (eval at compile (C:\Users\HP\Desktop\exemple CRUD\blog_api\node_modules\ejs\lib\ejs.js:661:12), <anonymous>:10:26)
    at resultat (C:\Users\HP\Desktop\exemple CRUD\blog_api\node_modules\ejs\lib\ejs.js:691:17)
    at tryHandleCache (C:\Users\HP\Desktop\exemple CRUD\blog_api\node_modules\ejs\lib\ejs.js:272:36)
    at Object.exports.renderFile (C:\Users\HP\Desktop\exemple CRUD\blog_api\node_modules\ejs\lib\ejs.js:489:10)
    at C:\Users\HP\Desktop\exemple CRUD\blog_api\routes\apis\post.js:121:9
    at Layer.handle [as handle_request] (C:\Users\HP\Desktop\exemple CRUD\blog_api\node_modules\express\lib\router\layer.js:95:5)
    at next (C:\Users\HP\Desktop\exemple CRUD\blog_api\node_modules\express\lib\router\route.js:137:13)
    at Route.dispatch (C:\Users\HP\Desktop\exemple CRUD\blog_api\node_modules\express\lib\router\route.js:112:3)
    at Layer.handle [as handle_request] (C:\Users\HP\Desktop\exemple CRUD\blog_api\node_modules\express\lib\router\layer.js:95:5)
    at C:\Users\HP\Desktop\exemple CRUD\blog_api\node_modules\express\lib\router\index.js:281:22
    at Function.process_params (C:\Users\HP\Desktop\exemple CRUD\blog_api\node_modules\express\lib\router\index.js:335:12)
    at next (C:\Users\HP\Desktop\exemple CRUD\blog_api\node_modules\express\lib\router\index.js:275:10)
    at Function.handle (C:\Users\HP\Desktop\exemple CRUD\blog_api\node_modules\express\lib\router\index.js:174:3)
    at router (C:\Users\HP\Desktop\exemple CRUD\blog_api\node_modules\express\lib\router\index.js:47:12)
    at Layer.handle [as handle_request] (C:\Users\HP\Desktop\exemple CRUD\blog_api\node_modules\express\lib\router\layer.js:95:5)
    at trim_prefix (C:\Users\HP\Desktop\exemple CRUD\blog_api\node_modules\express\lib\router\index.js:317:13) {
  path: 'C:\\Users\\HP\\Desktop\\exemple CRUD\\blog_api\\views\\resultat.ejs'
}

and nothing in the console. I need some help.


Solution

  • Render the file first (with your data), then pass the rendered HTML to the pdf'er

    Untested, try this:

    router.get("/uknow", (req, res) => {
    
      // your missing data, wheres it coming from?
      // it should also look like:
      // let data = { data: [{ imei: { name: '', ...}}, ...], ...}
    
      // render the ejs file
      ejs.renderFile(path.join(__dirname, '..', '..', 'views', 'resultat.ejs'), data, {}, function(err, str) {
        if (err) return res.send(err);
    
        // str now contains your rendered html
        pdf.create(str).toFile("report.pdf", function(err, data) {
          if (err) return res.send(err);
    
          res.send("File created successfully");
        });
      });
    });