0

I'd like to use the pdf.py script here to create PDFs of a specific size, much the way img2pdf does. I'd be happy for a few standard sizes, like US letter or A4, and have been able to modify the script so that it creates a page of the correct size, but I'm afraid my knowledge of what's going on in the script is not sufficient to center the image on the page.

I'm pretty sure it's here somewhere where the media box is created, but I'm not sure how to alter the position:

    xobj = Obj({'Type': '/XObject', 'Subtype': '/Image', 'Width':
        str(width), 'Height': str(height), 'ColorSpace': '/DeviceGray',
        'BitsPerComponent': '1', 'Filter': '/JBIG2Decode', 'DecodeParms':
        ' << /JBIG2Globals %d 0 R >>' % symd.id}, contents)
    contents = Obj({}, 'q %f 0 0 %f 0 0 cm /Im1 Do Q' % (float(width * 72) / xres, float(height * 72) / yres))
    resources = Obj({'ProcSet': '[/PDF /ImageB]',
        'XObject': '<< /Im1 %d 0 R >>' % xobj.id})
    page = Obj({'Type': '/Page', 'Parent': '3 0 R',
        'MediaBox': '[ 0 0 %f %f ]' % (pw, ph),
        'Contents': ref(contents.id),
        'Resources': ref(resources.id)})

Any help appreciated.

(Borders would be nice too, but one thing at a time.)

Progman
  • 16,827
  • 6
  • 33
  • 48
Eponymous
  • 109
  • 8
  • Thanks. I was hoping there’d be a way to directly center it, but will do the math. – Eponymous Feb 22 '22 at 23:48
  • Yeah, I'm not worried about the math, just was hoping that there was a built-in way to do it automatically. Thanks for the help. – Eponymous Feb 24 '22 at 19:55

0 Answers0