pygobject / pycairo

Python bindings for cairo
https://pycairo.readthedocs.io
Other
611 stars 83 forks source link

Using PDF surface in a loop, PDFs are combined together? #363

Closed ornamentist closed 4 months ago

ornamentist commented 4 months ago

I'm using pycairo to render PDF's with the PDFSurface in a function called in a loop. Something like:

def render_PDF(args):
   with cairo.PDFSurface(path, width, height) as surface:
    context = cairo.Context(surface)

    # call Cairo rendering methods
    # ...
    context.show_page()

In main.py:

for args in something:
  render_PDF(args)

This is running a much larger application where the args and other settings are coming from. For some reason the generated PDF's are combined together, with the first PDF with one page, the second with two overlaid pages, the third with three overlaid pages etc.

It doesn't seem to matter whether I use the Python "with" approach or manually flush and finish the surface, the PDF's are still combined.

This must be something I'm doing wrong or not appreciating about Python scopes?

ornamentist commented 4 months ago

I can't reproduce this in other language bindings so assuming it's my use of Python is causing it.