1

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...

PerryW
  • 1,426
  • 1
  • 15
  • 25

1 Answers1

6

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

print "Content-type: application/pdf\n"
print pdf
Pablo Reyes
  • 3,073
  • 1
  • 20
  • 30
  • Also, if you create the xxx.pdf file in your python code it will probably have owner:group www-data:www-data. with read permissions. Otherwise apache will not be able to read it. – Pablo Reyes Nov 01 '13 at 01:08
  • Oh for $$%%%$$%'s sake!!! :) Thanks! That has to be the easiest StackOverflow points ever! Yup adding the \n makes it work perfectly – PerryW Nov 01 '13 at 01:09
  • 1
    @PerryW: Yes, in HTTP, headers are separated from body by a blank line, so you're sending the whole PDF as a bunch of improperly-formatted headers, and then no body… – abarnert Nov 01 '13 at 01:11
  • It does not display the contents in the browser for me. Can you revert with complete code for me please to display the pdf contents in the firefox browser for pd, png, jpg files. I have tried several cgi scripts but they don't render the images nor the pdf as o/p http://stackoverflow.com/users/1608452/perryw. Thank you in advance – user956424 Jul 08 '14 at 13:58