I'm converting my ejs template to pdf using an api which fetches multiple data. i'm able to insert data to template from the json response from api. but i'm facing trouble with image. The image is a blob of html canvas. I converted it to base64 and inserted its name to db via another api.
my api response,
"data2": [
{
"graph_img": "http://localhost:5000/public/images/graph_images_for_pdf/2021102161712PM.jpeg"
}
],
how i used this in ejs template,
<% dataw.data2.forEach(function(a){ %>
<div class="row">
<div class="col-sm-6">
<div class="mb-4 pull-left">
<div > <%= a.graph_img %> </div>
</div>
</div>
</div>
<% }); %>
this gives me the http://localhost:5000/public/images/graph_images_for_pdf/2021102161712PM.jpeg in my pdf. How to add image instead.
var fs = require ("fs");
var image = graphimage;
var bitmap = Buffer.from(image, 'base64');
var _path = "./public/images/graph_images";
var imagename = nmae ".jpeg";
fs.writeFileSync(_path '/' imagename, bitmap);
this is how i save the file in my api. where graphimage is the string from frontend.
CodePudding user response:
You need to remove the base64 signature tag from the start of the string, so that it can be parsed successfully. The code should be like this
var fs = require ("fs");
var image = graphimage.replace("data:image/jpeg", "");
var bitmap = Buffer.from(image, 'base64');
var _path = "./public/images/graph_images";
var imagename = nmae ".jpeg";
fs.writeFileSync(_path '/' imagename, bitmap);