Question

I have to work with a fairly basic Python 2.6 on a 'black box' appliance (so no Django or non-standard libraries).

I have to:

  1. Send a bunch of html from the browser to the Python script on the server
  2. Do some processing and convert to pdf using wkhtmltopdf
  3. Return the PDF to the browser

I use two Python scripts - makePDF and getPDF At the end of makePDF I have a valid /tmp/xxx.pdf sitting on the server - I can transfer it by SCP, it opens without issue in acrobat - no problem there (it should always be under 100k - 2mb in size btw).

My problem is in sending the file back to the browser

here's getPDF

#!/usr/bin/python
from tempfile import *
tempfile=gettempdir()+"/xxx.pdf"
f = open(tempfile, 'r')
pdf = f.read()
f.close()
print 'Content-Type: application/pdf'
print pdf

It looks like it should be working - if I watch the http conversation in dev tools I can see that 169k of content length is returned, but it shows no response data, if use my weapon of choice, the 'Advanced Rest Client' chrome extn I see a response that contains what looks like a kosher pdf file:

%PDF-1.4
1 0 obj
<<
/Title (��Briefing Pack)
/Creator (��)
/Producer (��wkhtmltopdf)
/CreationDate (D:20131101095256+10'30')
>>
... etc

The browser, shows a "Failed to Load PDF Document" Error

I think it's fairly obvious that I'm an occasional Python user rather than a regular, so I suspect I'm missing something fairly basic...

Was it helpful?

Solution

It's working for me after adding a '\n' after application/pdf:

print "Content-type: application/pdf\n"
print pdf
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top