Search code examples
javascriptbase64data-uripdfmake

PDFMake Base64 generates malformed document


I'm using PDFMake to generate some PDF reports on client side. When I specify the definition for document and pass it to createPdf(DEFINITION_HERE) I get my PDF and it looks nice and shiny. But I'm saving it in the DB as Base64 encoded string and when I try to reopen it with data URI (ie: data:application/pdf;base64,JVBER...) the document is completely malformed.

My DB call looks like this:

await pdfFile.getDataUrl(async (result: string) => {
      await myServiceLayer.generatePdfReport(result, fileName, creator);
});

Anyone else had this issue with PDFMake library? Any ideas on what could be the cause of this?


Solution

  • OK, so after trying different things and debugging left and right, I stumbled upon this:

    https://github.com/bpampuch/pdfmake/issues/318

    So I removed call to .download() on my PDF file and checked for Base64 again, now it had correct value and file looks as it should.

    So in a nutshell, if we call multiple methods on PDFMake, usually something will get messed up.

    I had my .download() call just before my .getBase64() as I wanted to first download the file for the user and then also store it in DB as Base64, as .download() happened .getBase64() kept on giving me Base64 output which resulted in malformed PDF file.