diogenes
diogenes

Reputation: 2129

Docraptor API Django get PDF to download

I am trying to use Docraptor in Django 2.0 and Python 3.

I can get the PDF to generate to their website but not download to my system.

Here is their code from https://github.com/DocRaptor/docraptor-python

What needs to change to download the pdf file?

doc_api = docraptor.DocApi()

try:

  create_response = doc_api.create_doc({
    "test": True,       
    "document_content": "<html><body>Hello World</body></html>",
    "name": "docraptor-python.pdf", 
    "document_type": "pdf",      

  })
  file = open("/tmp/docraptor-python.pdf", "wb")
  file.write(create_response)
  file.close()
  print("Wrote PDF to /tmp/docraptor-python.pdf")

except docraptor.rest.ApiException as error:
  print(error)
  print(error.message)
  print(error.code)
  print(error.response_body)

I am guessing there is some sort of HttpResponse missing?

This seems like a great way to produce PDF files once I overcome this issue. Their tech support is great but documentation a bit short.

Thanks.

Upvotes: 0

Views: 281

Answers (1)

Borut
Borut

Reputation: 3364

Yes, response is missing. In general it's something like this:

response = HttpResponse()
response['Content-Type'] = 'application/pdf'
response['Content-Disposition'] = 'attachment; filename="my_pdf.pdf"'
response.write(creeate_response)
return response

Upvotes: 3

Related Questions