I had a use case to generate a PDF report with images and then needed to save it as an attachment in the DB.
The journey pdf-reports module is great for this!
This little snippet shows how you can use the toBase64() function from the report to save a generated PDF in the DB, for use in the app:
const generatedPdf = await pdf.generatePdf({ html: pdfHtml });
console.log(generatedPdf)
var base64_prepresentation = await generatedPdf.toBase64()
db_obj.attachment_property = await Attachment.create({filename: "certificate.pdf", base64: base64_prepresentation, mediaType:'application/pdf'})
The other fun thing to do is to then take an attachment and add it to a PDF report: (showing an image in the PDF)
const pdfTemplate = handlebars.compile(`
<!DOCTYPE html>
<html>
<body>
<h1>HTML pdf</h1>
<img src="{{url_to_attachment}}">
</body>
</html>
`)
const pdfHtml = pdfTemplate({ url_to_attachment: db_obj.attachment_property.url() });
const generatedPdf = await pdf.generatePdf({ html: pdfHtml })
Any input on better practices here would be grealy appreciated!