【发布时间】:2022-02-06 17:52:11
【问题描述】:
我正在开发一个证书生成器,其中我有一个名称列表,我想通过这些名称进行分析并将它们显示在证书上,然后将该证书下载为 pdf。然后我继续下一个名字等等。
const _exportPdf = async () => {
// if namelist is not uploaded
if (nameList == null){
console.log("Name List not uploaded");
return;
}
// if name is uploaded
Object.keys(nameList).forEach(async function(key){
const name = nameList[key];
// export the certificate as a pdf
await html2canvas(document.querySelector("#capture")).then(canvas => {
console.log("exporting "+name);
setVisibleName(name);
const imgData = canvas.toDataURL('image/png');
const pdf = new jsPDF('l', 'pt', "a4");
const imgProps= pdf.getImageProperties(imgData);
var width = pdf.internal.pageSize.getWidth();
var height = (imgProps.height * width) / imgProps.width;
pdf.addImage(imgData, 'PNG', 0, 0,width, height);
const filename = name + ".pdf";
pdf.save(filename);
})
});
}
目前我的样本名称列表中有5个名称,当我使用此功能时,我可以看到证书上的名称发生了变化,但是所有下载的5个证书上都写有“样本名称”(样本名称是默认值的可见名称)。
需要帮助。
【问题讨论】:
标签: reactjs async-await react-hooks jspdf