我正在使用獲取多個資料的 api 將我的 ejs 模板轉換為 pdf。我可以從 api 的 json 回應中將資料插入到模板中。但我面臨影像問題。該影像是一團 html 畫布。我將其轉換為 base64 并通過另一個 api 將其名稱插入到 db 中。
我的api回應,
"data2": [
{
"graph_img": "http://localhost:5000/public/images/graph_images_for_pdf/2021102161712PM.jpeg"
}
],
我如何在 ejs 模板中使用它,
<% 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>
<% }); %>
這給了我 pdf 中的 http://localhost:5000/public/images/graph_images_for_pdf/2021102161712PM.jpeg。如何改為添加影像。
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);
這就是我將檔案保存在我的 api 中的方式。其中 graphimage 是來自前端的字串。
uj5u.com熱心網友回復:
需要去掉字串開頭的base64簽名標簽,才能成功決議。代碼應該是這樣的
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);
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/333161.html
