node.js - 我正在从动态 Html 创建 pdf,但使用 node.js 在 pdf 中获取不需要的模式
问题描述
我正在使用 node.js包pdf
从动态创建。我有一个医生类别列表和数据集,每个类别键中都有医生数组。但是当我生成pdf时,它给了我不想要的错误格式的pdf。Html
htmltopdf
我有一个像这样的数组中的医生类别列表
let categories = ['Physician', 'Psychiatrist','Audiology',.... so on]
并有这样的数据集
let masterRecord = {
'Physician' : [{doctor-1 object},{doctor-1 object}, ... so on],
'Audiology' : [{doctor-1 object},{doctor-1 object}, ... so on],
'Psychiatrist' : [{doctor-1 object},{doctor-1 object}, ... so on],
.... so on
}
我试过这段代码
let masterRecord = body.masterRecord;
let categories = Object.keys(masterRecord);
categories.forEach(category=>{
masterRecord[category].forEach(doctor=>{
htmlData.partialTemplate += `<div class="data-section">
<h3 class="subtitle fancy" style="font-size: 24px;"><span>${category}
</span></h3>
<div class="data-row">
<b><p class="m-0 font-16">${doctor.Name},${doctor.Credentials}</p></b>
<p class="m-0 font-16">${doctor.Street}, ${doctor.City},
${doctor.State} ${doctor.Zip_Code}</p>
<div class="tele-fax-div">
<p class="m-0 font-16">Telephone: ${doctor.Telephone}</p>
<p class="m-0 pl-5 font-16">Fax: ${doctor.Fax}</p>
</div>
</div>
</div>`
})
})
htmlData.partialTemplate = htmlData.partialTemplate + ` </div>
</div>
</body>
</html>`
let finalHtml = htmlData.partialTemplate;
// console.log(finalHtml);
htmltopdf.createFromHtml(finalHtml, "doctors.pdf", function (err, success) {
if(err){
res.status(201).json({status : 201, success : false, Error : err});
}
if (success) {
res.status(200).json({status : 200, success : true, message : success});
}
});
我期待像pdf格式
Doctor Category-1 Name Here
Doctor-1 Info Here
Doctor-2 Info Here
Doctor-3 Info Here
Doctor-4 Info Here
Doctor Category-2 Name Here
Doctor-1 Info Here
Doctor-2 Info Here
Doctor-3 Info Here
Doctor-4 Info Here
Doctor Category-3 Name Here
Doctor-1 Info Here
Doctor-2 Info Here
Doctor-3 Info Here
Doctor-4 Info Here
但是从上面提到的代码中,我得到了这样的pdf
Doctor Category-1 Name Here
Doctor-1 Info Here
Doctor Category-1 Name Here
Doctor-2 Info Here
Doctor Category-1 Name Here
Doctor-3 Info Here
Doctor Category-1 Name Here
Doctor-4 Info Here
Doctor Category-2 Name Here
Doctor-1 Info Here
Doctor Category-2 Name Here
Doctor-2 Info Here
Doctor Category-2 Name Here
Doctor-3 Info Here
Doctor Category-2 Name Here
Doctor-4 Info Here
Doctor Category-3 Name Here
Doctor-1 Info Here
Doctor Category-3 Name Here
Doctor-2 Info Here
Doctor Category-3 Name Here
Doctor-3 Info Here
Doctor Category-3 Name Here
Doctor-4 Info Here
我怎样才能得到我想要的格式?
解决方案
您需要将data-section
div 和h3
title 从内部循环中取出,否则它会为每个 Doctor 编写一次。
它会是这样的:
...
categories.forEach( category =>
{
htmlData.partialTemplate += `<div class="data-section">
<h3 class="subtitle fancy" style="font-size: 24px;"><span>${category}</span></h3>`
masterRecord[category].forEach(doctor =>
{
htmlData.partialTemplate += `<div class="data-row">
<b><p class="m-0 font-16">${doctor.Name},${doctor.Credentials}</p></b>
<p class="m-0 font-16">${doctor.Street}, ${doctor.City},
${doctor.State} ${doctor.Zip_Code}</p>
<div class="tele-fax-div">
<p class="m-0 font-16">Telephone: ${doctor.Telephone}</p>
<p class="m-0 pl-5 font-16">Fax: ${doctor.Fax}</p>
</div>
</div>`
})
htmlData.partialTemplate += `</div>`
})
...
推荐阅读
- java - java AffineTransformOp 的奇怪行为
- node.js - 使用 nodejs 和套接字我试图在一个循环中一起加载流。有人可以告诉我哪里出错了吗?
- python - 合并具有相同键的元组
- javascript - JavaScript/three.js:THREE.BufferGeometry 不接收任何光线或阴影
- node.js - Heroku 应用程序无法在少数浏览器上加载
- c# - unity改变输出AudioMixerGroup
- java - 如何将字符串更改为日期格式
- azure - 将 Azure Pipelines 文件上传到 Onedrive 或类似文件
- c - 开罗图形:如何确定物理屏幕尺寸
- c# - C#:通过存储在字符串变量中的名称来反映类