我正在从动态 Html 创建 pdf,但使用 node.js 在 pdf 中得到不需要的模式
I am creating pdf from dynamic Html but getting unwanted pattern in pdf using node.js
我正在使用 node.js htmltopdf
包从动态 Html
创建 pdf
。我有一个医生类别列表和数据集,每个类别键中都有医生数组。但是当我生成 pdf 时,它给了我错误的 pdf 格式,这是我不想要的。
我有一个像这样的数组中的医生类别列表
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
标题从内循环中取出来,否则它会为每个医生写一次。
会是这样的:
...
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>`
})
...
我正在使用 node.js htmltopdf
包从动态 Html
创建 pdf
。我有一个医生类别列表和数据集,每个类别键中都有医生数组。但是当我生成 pdf 时,它给了我错误的 pdf 格式,这是我不想要的。
我有一个像这样的数组中的医生类别列表
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
标题从内循环中取出来,否则它会为每个医生写一次。
会是这样的:
...
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>`
})
...