I am connecting to an external service that returns a bytearray with a PDF file in bytes. The version of the PDF is 1.7.
My problem is that this response I have to package it somehow and return it to the user with HTTPResponse
.
The code I have right now is this:
with open("etiqueta-envio.pdf", "wb") as f:
f.write(datos_pdf)
response = HttpResponse(content_type='application/pdf')
response['Content-Disposition'] = 'attachment; filename="etiqueta-envio.pdf"'
response.write(f)
return response
This returns me a file and adobe says it's corrupt.
Don't dump it to an intermediate file. It simply returns the bytes you have in
datos_pdf
, with thecontent_type
appropriate one:Update . This answer assumes that it
datos_pdf
is a byte string. If you receive what comes from the external server as abytearray()
, you must convert it to a byte string like this: