Reputation: 55
I am trying to create a pdf from rendered ejs file but cannot render image properly no matter how I try.
my app.js :
let express = require("express");
let app = express();
let ejs = require("ejs");
let pdf = require("html-pdf");
let path = require("path");
app.use( express.static( "public" ) );
app.get("/generateReport", (req, res) => {
ejs.renderFile("./views/report-template.ejs", (err, data) => {
if (err) {
res.send(err);
} else {
pdf.create(data).toFile("certificate.pdf", function (err, data) {
if (err) {
res.send(err);
} else {
res.send("File created successfully");
}
});
}
});
})
app.listen(3000, function(){
console.log('Server listening on port 3000');
});
----------
my report-template.ejs file
<html>
<body>
<img src="certificate.jpg" width=675 height=792>
</body>
</html>
I've saved certificated.jpg in all the locations including root,public etc..but it does'nt work..Please help ..I'm new to coding and node.
Upvotes: 3
Views: 710
Reputation: 11
this work for me, parameter "base" in options pdf
//base: ${req.protocol}://${req.get('host')}
var options = {
"format": "A4",
"orientation": "portrait",
base: `${req.protocol}://${req.get('host')}`,
};
pdf.create(resulthtml, options).toStream(function(err, stream) {
if (err) res.end('An error occurred on create pdf');
res.setHeader('Content-disposition', 'inline; filename="' + stream.filename + '"');
res.setHeader('Content-type', 'application/pdf');
stream.pipe(res);
});
then now in template
<img src="/public/image.png">
or simply
<img src="image.png">
Upvotes: 1