[reportlab-users] Run time error with version 3.4

Robin Becker robin at reportlab.com
Fri May 19 04:51:37 EDT 2017


Hi,

I don't know where the idea arose that you can repeatedly save a canvas object, 
but more than one person has tried it see

https://bitbucket.org/rptlab/reportlab/issues/107/runtimeerror-typo-in-pdfdocument



The Canvas contains many structured objects including a PDFDocument instance. 
That is the class that causes your error. When you save the Canvas lots of 
structures get changed. That means the Canvas is no longer valid and should be 
thrown away.

To save individual pages the showPage method should be called.

So  like

canv = Canvas(...)
for info in mystuff:
     make_a_page(info,canv)
     canv.save()

should really be

canv = Canvas(...)
for info in mystuff:
     make_a_page(info,canv)
     canv.showPage()
canv.save()


this may or may not suit if the number of pages is high, but we don't have a 
method which incrementally writes the document out. It might have worked in the 
past if you were lucky, but it was never intended to be so.

On 18/05/2017 20:30, lizhe Xu wrote:
> I called my colleague who developed the Python code and he is using the script daily without any error. The only difference between his and mine is I'm using the newer vision of reportLab.
> Yes, the script tries to save a canvas for each loop by adding a new page. Attached two files are the results of running the script from different data sets: WithSave.pdf was generated by my colleague with the original script and old version of reportLab; WithoutSave.pdf was generated from my computer by commenting out the line 546 c.save().
> So, I guess the implementation of the new SaveToFile may differ in the different versions. But my goal is to run the script on my computer without bothering my colleague by emailing data to him anymore. So I wonder if there is a method other than save() will do the magic for me.
> Thanks.
>
.........


-- 
Robin Becker


More information about the reportlab-users mailing list