[cairo] Putting PNGs into PDFs using pycairo?

Andrea Canciani ranma42 at gmail.com
Mon Oct 18 08:00:38 PDT 2010

On Mon, Oct 18, 2010 at 3:50 PM, Steve Schwarz <steve at agilitynerd.com> wrote:
> Hi,
> I'm pretty new to pycairo and I'd like to insert PNG images into PDFs and it
> looks like I'm not doing so correctly:
> import cairo
> def pngInPDF():
>     pdf_file = open("test.pdf", 'wb')
>     surface = cairo.PDFSurface(pdf_file, 595, 841)
>     context = cairo.Context(surface)
>     png_surface = cairo.ImageSurface.create_from_png('test.png')
>     context.set_source(png_surface)
> I get a traceback on the set_source() call:
>      context.set_source(png_surface)
> TypeError: Context.set_source() argument 1 must be cairo.Pattern, not
> cairo.ImageSurface
> I'd appreciate any pointers on the correct way of doing this.

You should probably use context.set_source_surface (png_surface, 0, 0)
or create a pattern from png_surface and use that as parameter
for set_source.

I don't know where the documentation of pycairo is, but knowing
the C API might be useful anyway:


More information about the cairo mailing list