简体   繁体   中英

Creating pdf Blob from array and viewing it

I'm having a trouble with getting my pdf to show up. I get the file data as byte array. This is what I have at the moment:

    // data is originally String {0: % 1:P 2:D ...}
    const byteArray = _.map(data); // ["%", "P", "D", "F", "-", "1", ".", "4", "↵", ...]
    const blob = new Blob(byteArray, {type: 'application/pdf'});
    const blobURL = URL.createObjectURL(blob);
    window.open(blobURL),

Blob shows up when I log it and it has proper length and all. For some reason the pdf opens but is empty. Only the header in the tab is right (so there must be something right?). There is a possibility that the fault is in the backend where I get the data from but I'm not sure as I don't have access to it. You may also suggest better formats to transfer the data from backend if that is necessary.

Could the data be presented wrong in it's original form?

Edit:

When I do the request with postman, it displays the file correctly without me doing anything

I solved the problem by adding responseType='arraybuffer' to the request config. After that this worked well.

const blob = new Blob([byteArray], {type: 'application/pdf'});
const blobURL = URL.createObjectURL(blob);
window.open(blobURL)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM