0

I am using the package hopding/pdf-lib from github within the browser to generate a pdf. I get the result as a uint8array. I can send this to the browser using rndme/download from github which stores the pdf first to the local disk and then sends it to a new browser tab and opens it in the pdf viewer. As I dont want to store it to disk i tried the following code:

    const pdfBytes = await pdfDoc.save();  // create the pdf as uint8array
    //download(pdfBytes, fn, "application/pdf");
    let blb = new Blob(pdfBytes, {type: 'application/pdf'});
    let link = window.URL.createObjectURL(blb);
    window.open(link);

This opens the new tab with the pdf viewer, however it is empty. I checked the blob with the debugger and it tells me:

Blob { size: 772729, type: "application/pdf" }

There are no error messages. Why is the target empty?

solidgumby
  • 2,594
  • 1
  • 16
  • 9
fanThomas
  • 127
  • 2
  • 6

1 Answers1

1

The pdfBytes should be passed into the blob constructor in an array. The first argument of the blob constructor should be an array containing all the objects to be inserted into the blob.

An error is not being thrown because pdfBytes is an array of numbers, so all the numbers in the array are being inserted into the blob, rather than pdfBytes itself.

The blob creation should look like this:

let blb = new Blob([ pdfBytes ], {type: 'application/pdf'})
// notice how pdfBytes is passed inside of an array
one23four56
  • 151
  • 6