我正在从动态HTML创建pdf,但是却使用node.js

时间:2019-01-23 20:41:24

标签: node.js express html-to-pdf

我正在使用node.js pdf包从动态Html创建htmltopdf。我有医生类别和数据集的列表,每个类别键中都有Doctors数组。但是当我生成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

如何获得所需的格式?

1 个答案:

答案 0 :(得分:0)

您需要将15 Characters Original String=samplequotereqs 131=samplequotereqs 16 Characters Original String=samplequotereqs 131= otereqs1 div和data-section标题从内部循环中删除,否则为每个Doctor编写一次。

会是这样的:

h3