How to convert an array buffer to pdf & download/show pdf?

I’m new to ionic and typescript. I would appreciate a detailed explanation. Thanks in advance.

The data comes from a restful API. The pdf file is saved in the database as a blob file.

Data on DevTools/console :
-Array (1)
–File:
—data: (25474) [37, 80, 68, 70, 45, 49, 46, 55, 10, 10, 52, 32, 48, 32, 111, 98, 106, 10, 40, 73, 100, 101 , 110, 116, 105, 116, 121, 41, 10, 101, 110, 100, 111, 98, 106, 10, 53, 32, 48, 32, 111, 98, 106, 10, 40, 65, 100 , 111, 98, 101, 41, 10, 101, 110, 100, 111, 98, 106, 10, 56, 32, 48, 32, 111, 98, 106, 10, 60, 60, 10, 47, 70 , 105, 108, 116, 101, 114, 32, 47, 70, 108, 97, 116, 101, 68, 101, 99, 111, 100, 101, 10, 47, 76, 101, 110, 103, 116 , 104, 32, 50,…]
—type: “Buffer”

I keep reading the link you sent me, but I have to admit that my beginner-level knowledge I have in javascript isn’t helping me. Should I convert my array buffer into a blob? I want to convert it to pdf file, not JSON? Could you please help me with a more specific code using my array data? Anyway thanks for your time! :slight_smile:

I think you need to convert to Blob and then use the link to create the download


  blobToSaveAs(fileName: string, blob: Blob) {

    try {
      const url = window.URL.createObjectURL(blob);
      const link = document.createElement('a');
      if (link.download !== undefined) { // feature detection
        link.setAttribute('href', url);
        link.setAttribute('download', fileName);
        link.style.visibility = 'hidden';
        document.body.appendChild(link);
        link.click();
        document.body.removeChild(link);
      }
    } catch (e) {
      console.error('BlobToSaveAs error', e);
    }
  }

When converting to Blob you can provide the mime type to PDF. I don’t know that one by heart

Example:

 const blob = new Blob([exportText], { type: 'text/plain' });
1 Like

It worked!! Thanks so much for the help and for the fast replies!

1 Like

Cool. Can you post the mime type you used? (code creating the Blob)

Glad to be of help

blob = new Blob([this.array], { type: ‘application/pdf’ });

1 Like