views:

75

answers:

2

Hi,

I'm using Reportlab to create PDFs. I'm creating two PDFs which I want to merge after I created them. Reportlab provides a way to save a pycanvas (source) (which is basically my pdf file in memory) as a python file, and calling the method doIt(filename) on that python file, will recreate the pdf file. This is great, since you can combine two PDFs on source code basis and create one merge pdf.

This is done like this:

from reportlab.pdfgen import canvas, pycanvas
#create your canvas
p = pycanvas.Canvas(buffer,pagesize=PAGESIZE)
#...instantiate your pdf...

# after that, close the PDF object cleanly.
p.showPage()
p.save()

#now create the string equivalent of your canvas
source_code_equiv = str(p)
source_code_equiv2 = str(p)

#merge the two files on str. basis
#not shown how it is exactly done, to make it more easy to read the source
#actually one just have to take the middle part of source_code_equiv2 and add it into source_code_equiv
final_pdf = source_code_equiv_part1 + source_code_equiv2_center_part + source_code_equiv_part2

#write the source-code equivalent of the pdf
open("n2.py","w").write(final_pdf)
from myproject import n2
p = n2.doIt(buffer)

# Get the value of the StringIO buffer and write it to the response.
pdf = buffer.getvalue()
buffer.close()
response.write(pdf)
return response  

This works fine, but I want to skip the step that I save the n2.py to the disk. Thus I'm looking for a way to instantiate from the final_pdf string the corresponding python class and use it directly in the source. Is this possible?

It should work somehow like this..

n2 = instantiate_python_class_from_source(final_pdf)
p = n2.doIt(buffer)

The reason for this is mainly that there is not really a need to save the source to the disk, and secondly that it is absolutely not thread save. I could name the created file at run time, but then I do not know what to import!? If there is no way to prevent the file saving, is there a way to define the import based on the name of the file, which is defined at runtime!?

One might ask why I do not create one pdf in advance, but this is not possible, since they are coming from different applications.

A: 

Ok, I guess you could use code module which provides standard interpreter’s interactive mode. The following would execute function doIt.

import code
import string
coded_data = """
def doIt():
    print "XXXXX"
"""
script = coded_data + "\ndoIt()\n" 
co = code.compile_command(script, "<stdin>", "exec")
if co:
    exec co

Let me know, if this helped.

pyfunc
Thanks, but I'm not yet sure whether it helps ;-) I would need the return value of the method call doIt(buffer) assigned to p, as I do with this statement: p = n2.doIt(buffer) .. is this possible?
Tom Tom
+1  A: 

This seems like a really long way around to what you want. Doesn't Reportlab have a Canvas class from which you can pull the PDF document? I don't see why generated Python source code should be involved here.

But if for some reason it is necessary, then you can use StringIO to "write" the source to a string, then exec to execute it:

from cStringIO import StringIO

source_code = StringIO()
source_code.write(final_pdf)
exec(source_code)
p = doIt(buffer)
Ned Batchelder
hmm.. Just calling doIt(buffer) will not work, since doIt() is a method of the serialized pycanvas.
Tom Tom
I guess we'll need to see a sample of this generated source to know the right syntax. If you can write it to n2.py, then import n2, then call n2.doIt(), then my code should also work.
Ned Batchelder
I would triple-check that you have to involve generate Python code at all though. It definitely seems like there's a simpler way to do it.
Ned Batchelder
You are right. I went away from this idea of merging and was restructuring the whole system to be able to create this merged pdf on the fly.
Tom Tom