Convert PDF code string to arraybuffer/blob for client download

Asked

Viewed 697 times

0

I’ve been having problems for a while, so I decided to ask here, because I’m already out of options.

I’m getting a pdf of an api made in java. I get this pdf from the backend, in python, because I need to pass a token of authentication to consume the service. That said, I get a specific pdf code. Below, my request, and my return.

def get_relatorio(self, solicitacao, tokenAcesso):
    headers = {'Authorization' : 'Token ' + tokenAcesso, 'Accept' : 'application/pdf'}

    response = requests.get(self.uri + '/relatorios/solicitacao/' + solicitacao, headers=headers)

    return response.content

and my return comes in the following format:

%PDF-1.4
%
4 0 obj
<</ColorSpace/DeviceGray/Subtype/Image/Height 
50/Filter/FlateDecode/Type/XObject/Width 144/Length 
1325/BitsPerComponent 8>>stream
xLUeϽ{"pf8E\cE.D)0`WKڬ5eڐi"k@)v?ƺHZ.!=xZy<>ϹqqgO̊7(W@? 
$(wQY4gmqШt_?̍!!N9`9Fj  uUa+Ϥ`  eV%Wd'7}v!̙Ux<R_cZ{FDrx ]o?#}߯ 
pN!o/eSc4Eq?18MtK   

[...]

I am not going to post the whole code for the sake of being a private document, but I believe it is already possible to identify which code it is.

Look for response.encoding Bring me 'utf-8'.

I need to consume that code, and serve in a Blob in front end with javascript.

I already searched and found the solution below:

   var file = new Blob([data], {type: 'application/pdf'});
   var fileURL = URL.createObjectURL(file);
   window.open(fileURL);

Where data would be the code of the pdf. Only that doing so generates me a totally blank pdf, despite having the correct number of pages. I searched, and this happens because I need to pass the object data as a arraybuffer instead of a string that is what I get from python, and the only way I thought of doing that would be by doing an ajax and setting the responseType to arraybuffer. I take this code from inside the template, however, and not via ajax, and therefore cannot in any way convert the string to arraybuffer or a valid Blob in any way to serve the client.

Could you help me with that? I’ve been searching for a long time and I haven’t found a solution yet.

Note: I am using Python 2.

1 answer

0


I managed to solve the problem, as follows:

In python file, return the return so:

return bytearray(response.content)

and, when consuming, instead of using jquery’s fetch or ajax, do it archaically:

new Promise((resolve, reject) => {
    var xhr = new XMLHttpRequest();
    xhr.open('GET', `${PORTAL_API_URL}/solicitacoes/${solicitacao}/imprimir`, true);
    xhr.setRequestHeader('Accept', 'application/pdf');
    xhr.responseType = 'arraybuffer';
    xhr.onload = function(e) {
        if (this.status >= 200 || this.status <= 400) {
            resolve(this.response)
        }
    }
    xhr.send();
});

Ai, receiving the return of this request, I can play inside the Blob and generate the correct pdf.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.