首页 > 解决方案 > 我正在从动态 Html 创建 pdf,但使用 node.js 在 pdf 中获取不需要的模式

问题描述

我正在使用 node.js包pdf从动态创建。我有一个医生类别列表和数据集,每个类别键中都有医生数组。但是当我生成pdf时,它给了我不想要的错误格式的pdf。Htmlhtmltopdf

我有一个像这样的数组中的医生类别列表

  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

我怎样才能得到我想要的格式?

标签: node.jsexpresshtml-to-pdf

解决方案


您需要将data-sectiondiv 和h3title 从内部循环中取出,否则它会为每个 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>`

})
...

推荐阅读