Node.js nodejs和hummus配方中的Mailmerge pdf
我正在尝试进行一个简单的邮件合并,其中收件人信息插入到pdf模板的顶部 模板为1页,但结果最多可达数百页 我有数组中的所有收件人对象,但需要找到一种方法来循环,并为每个收件人创建一个唯一的页面 我不确定hummus recipe是否是正确的工具,因此非常感谢您对如何实现这一点的任何意见 我的演示像这样Node.js nodejs和hummus配方中的Mailmerge pdf,node.js,pdf,hummus.js,Node.js,Pdf,Hummus.js,我正在尝试进行一个简单的邮件合并,其中收件人信息插入到pdf模板的顶部 模板为1页,但结果最多可达数百页 我有数组中的所有收件人对象,但需要找到一种方法来循环,并为每个收件人创建一个唯一的页面 我不确定hummus recipe是否是正确的工具,因此非常感谢您对如何实现这一点的任何意见 我的演示像这样 const HummusRecipe = require('hummus-recipe') const recepients = [ {name: "John Doe", address
const HummusRecipe = require('hummus-recipe')
const recepients = [
{name: "John Doe", address: "My Streetname 23", zip: "23456", city: "My City"},
{name: "Jane Doe", address: "Another Streetname 56 ", zip: "54432", city: "Her City"}
//'.......'
]
const template = 'template.pdf';
const pdfDoc = new HummusRecipe(template, 'output.pdf');
function createPdf(){
pdfDoc
.editPage(1)
.text(recepients[0].name, 30, 60)
.text(recepients[0].address, 30, 75)
.text(recepients[0].zip + ' ' + recepients[0].city, 30, 90)
.endPage()
//. appendpage(template) - tried versions of this in loops etc., but can't get it to work.
.endPDF();
}
createPdf();
这显然只创建了一个收件人为[0]的页面。我已经尝试了各种方法来使用.appendpage(模板)循环,但是我不能追加然后编辑同一个页面
在与鹰嘴豆泥配方的创建者和其他人交谈后,解决方案变得有些明显。不可能附加一个页面然后再修改它,也不可能多次修改同一页面 然后,解决方案是在两次传递中生成最终pdf。首先创建一个masterPdf,模板附加在for循环中,保存此文件,然后编辑每个页面 我已经创建了一个工作代码,如下所示。我已经使函数异步,因为我需要在lambda上运行它,需要som控制返回等。同样,通过这种方式,可以在AWS S3上保存tmp文件和最终pdf 下面的代码从1,6mb模板创建一个200页的pdf大约需要60秒。任何关于优化的想法都将不胜感激
const HummusRecipe = require('hummus-recipe');
const template = 'input/input.pdf';
const recipients = [
{name: 'John Doe', address: "My Streetname 23"},
{name: 'Jane Doe', address: "Another Streetname 44"},
//...etc.
]
async function createMasterPdf(recipientsLength) {
const key = `${(Date.now()).toString()}.pdf`;
const filePath = `output/tmp/${key}`;
const pdfDoc = new HummusRecipe(template, filePath)
for (i = 1; i < recipientsLength; i++) {
pdfDoc
.appendPage(template)
.endPage()
}
pdfDoc.endPDF();
return(filePath)
}
async function editMasterPdf(masterPdf, recipientsLength) {
const key = `${(Date.now()).toString()}.pdf`;
const filePath = `output/final/${key}`;
const pdfDoc = new HummusRecipe(masterPdf, filePath)
for (i = 0; i < recipientsLength; i++) {
pdfDoc
.editPage(i+1)
.text(recipients[i].name, 75, 100)
.text(recipients[i].address, 75, 115)
.endPage()
}
pdfDoc.endPDF()
return('Finished file: ' + filePath)
}
const run = async () => {
const masterPdf = await createMasterPdf(recipients.length);
const editMaster = await editMasterPdf(masterPdf, recipients.length)
console.log(editMaster)
}
run()
const HummusRecipe=require('hummus-recipe');
常量模板='input/input.pdf';
常量收件人=[
{姓名:'John Doe',地址:'My Streetname 23'},
{姓名:'Jane Doe',地址:'Another Streetname 44'},
//……等等。
]
异步函数createMasterPdf(recipientsLength){
const key=`${(Date.now()).toString()}.pdf`;
const filePath=`output/tmp/${key}`;
const pdfDoc=新HummusRecipe(模板、文件路径)
对于(i=1;i{
const masterPdf=await createMasterPdf(recipients.length);
const editMaster=wait editMasterPdf(masterPdf,recipients.length)
console.log(editMaster)
}
运行()