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 <a href="http://www.java2s.com/Open-Source/Python/PDF/ReportLab/ReportLab_2_4/src/reportlab/pdfgen/pycanvas.py.htm" rel="nofollow">(source)</a> (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.Answer1:
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)Answer2:
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.