0

I am trying to save a rendered html page in flask as pdf. I tried the following things:

pdf=render_template('exp1_post.html',some_data=some_data)
filename = "simplePrint.pdf"        
pisa.CreatePDF(pdf, file(filename, "w"))

and

pdf=render_template('exp1_post.html',some_data=some_data)
filename = "simplePrint.pdf"        
pisa.CreatePDF(pdf.encode("ISO-8859-1"), file(filename, "w"))

but generated pdf file is perfectly visible in Google Chrome (opened directly by going to the location on disk), but i get blank page in Adobe acrobat reader.

Similar problem that i can find: PDF text show in Google Chrome but not in Adobe Acrobat

but i am not sure how to implement above solution in python Chrome Version 41.0.2272.118 m Adobe Reader XI

2
  • 1
    Can you post an example PDF document that exhibits this problem? That may help someone to see what actually the problem is and might help with you solving it. Apr 4, 2015 at 17:31
  • Sure. Here is one such pdf, 2.73 kb in size. Google Drive link (any other way i should share it ? like pastebin kind of service that am not aware of?). You shall see tables etc for a blank form for data submission of titration data. file
    – ipcamit
    Apr 4, 2015 at 17:37

1 Answer 1

1

I'm not a Python expert (I know the name, that's about it) so my solution may not be correct. But I do think I know what the problem is and that should help you find the solution you're looking for.

When you open your file with a binary editor (or a good text editor showing invisible characters) you'll see that each line ending consists of three line-ending characters:

x0D x0D x0A

Or otherwise said

Carriage Return, Carriage Return, Line feed

This is wrong. From the PDF specification:

"As a matter of convention, the tokens in a PDF file are arranged into lines; see 7.2, "Lexical Conventions."Each line shall be terminated by an end-of-line (EOL) marker, which may be a CARRIAGE RETURN (0Dh), a LINE FEED (0Ah), or both. PDF files with binary data may have arbitrarily long lines."

I think this is what breaks Adobe Reader. It's strange that Adobe Reader and Acrobat throw a fit on this file, while many other (worse) PDF readers (such as Mac OS X Preview) seem to show it without any problem.

All of this said, you seem to have a problem with line endings. Given my limited knowledge of Python all I might point you to is the line:

file(filename, "w")

I read in Python documentation that on some platforms this can treat files as ASCII files and ruin binary files. As a PDF is definitely a binary file, I would change that to:

file(filename, "wb")

and see what happens.

I can tell you that for a far as I could see the rest of the file structure seems correct. So I think you have all necessary objects etc to show the file correct (as proven by Chrome and Mac Preview), so I really think the line ending problem is the one you need to solve.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.