I have the following script that generates several circles in a field on top of a larger circle. The output file is a PDF file that I would like to have a tightly limited amount of ink.
#!/usr/bin/env python import math import cairocffi as cairo import random def DrawFilledCircle(x,y,radius,rgba): ctx.set_source_rgba(*rgba) ctx.arc(x,y,radius,0,2*math.pi) ctx.fill() def DrawCircle(x,y,radius,rgba=(0,0,0,1)): ctx.set_source_rgba(*rgba) ctx.arc(x,y,radius,0,2*math.pi) ctx.stroke() surface = cairo.RecordingSurface(cairo.CONTENT_COLOR_ALPHA, None) ctx = cairo.Context (surface) DrawCircle(200,200,150,(0,0,0,1)) for i in range(1000): DrawFilledCircle(200+(150-300*random.random()), 200+(150-300*random.random()), 4, (0,0,0,0.5))
Running the script calls the following.

As you can see, the width and height are correct, but the origin needs to be shifted. Accordingly, I tried this:
pdfout = cairo.PDFSurface ("circle.pdf", extents[2], extents[3]) pdfctx = cairo.Context (pdfout) pdfctx.set_source_surface(surface,extents[0],extents[1])
It just moved farther to the right.

This suggested using negative coordinates to switch things in another way:
pdfout = cairo.PDFSurface ("circle.pdf", extents[2], extents[3]) pdfctx = cairo.Context (pdfout) pdfctx.set_source_surface(surface,-extents[0],-extents[1])
But that didn't work either:

As a backup, I could call the pdfcrop shell pdfcrop , but this seems like a ridiculous workaround.
What can I do to achieve what I'm trying to do? Where am I going wrong? How can a lasting peace be achieved in Cairo?