這是我的 result.ejs 檔案代碼:
<div style="width: 50%; margin: auto;">
<table class="table">
<thead>
<tr>
<th>SUBJECT</th>
<th>MARKS OBTAINED</th>
</tr>
</thead>
<tbody>
<tr>
<th>pHYSICS</th>
<td>80</td>
</tr>
<tr>
<th>Chemistry</th>
<td>80</td>
</tr>
<tr>
<th>mathematics</th>
<td>80</td>
</tr>
<tr>
<th>computer</th>
<td>80</td>
</tr>
</tbody>
</table>
</div>
在我的 user.js 中,我正在渲染 ejs 檔案:
router.get("/result",async(req,res)=>{
res.render("result");
})
我了解 pdfkit 的基礎知識。但我只用它來列印 pdf 格式的文本,如下所示:
const PDFDocument = require('pdfkit');
const fs = require('fs');
const doc = new PDFDocument();
doc.pipe(fs.createWriteStream('output.pdf'));
doc
.fontSize(25)
.text('Some text with an embedded font!', 100, 100);
但是現在我需要以 pdf 格式列印 ejs 檔案表資料。請幫我找出來。謝謝你。
uj5u.com熱心網友回復:
是的,你可以這樣做。首先,您需要渲染檔案,然后將其傳遞給 pdf。您只需要先將 ejs 模板化,然后將模板字串傳遞給 filename xyz.pdf,您就可以在 pdf 中看到 ejs 模板化。
// render the ejs file
ejs.renderFile(path.join(__dirname, 'path-to-your-ejs'), data, {}, function(err, str) {
if (err) return res.send(err);
// str now contains your rendered html
//your pdf object should be used here
pdf.create(str).toFile("name of the file", function(err, data) {
if (err) return res.send(err);
res.send("File created successfully");
});
});
從參考這個鏈接,查看鏈接了解更多資訊。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/353706.html
標籤:javascript 节点.js MongoDB 表达 ejs
下一篇:將api回應迭代為html
