I'm trying to use the jsPDF library to generate a PDF document from an HTML template in a Node.js environment. However, I'm encountering an error:
ReferenceError: document is not defined
Here is the relevent code:
generatePdf(formEntryId: string, customerId: string): ArrayBuffer {
const template = this.templateService.getTemplate('testPdf')
const doc = new jsPDF()
doc.html(template, {
callback: function (doc) {
doc.save('testFile.pdf')
}
})
return doc.output('arraybuffer')
}
I understand that this error typically occurs when trying to access the document object in a Node.js environment. However, I'm not directly accessing document in my code. How can I resolve this error and generate the PDF document successfully in a Node.js environment?
Any insights or suggestions would be greatly appreciated. Thank you!
It looks like using doc.html
will end up calling to html2canvas
which isn't viable in the node
environment. There are a few issues around this problem and it looks like there's no real solution other than running a puppeteer instance (or similar headless environment) and generating the pdf through that