summaryrefslogtreecommitdiff
path: root/examples/poster.py
diff options
context:
space:
mode:
Diffstat (limited to 'examples/poster.py')
-rwxr-xr-xexamples/poster.py70
1 files changed, 28 insertions, 42 deletions
diff --git a/examples/poster.py b/examples/poster.py
index ee58282..7f1c1c2 100755
--- a/examples/poster.py
+++ b/examples/poster.py
@@ -7,51 +7,37 @@ Shows how to change the size on a PDF.
Motivation:
-My daughter needed to create a 48" x 36" poster, but her Mac version of Powerpoint
-only wanted to output 8.5" x 11" for some reason.
+My daughter needed to create a 48" x 36" poster, but her Mac
+version of Powerpoint only wanted to output 8.5" x 11" for
+some reason.
+
+So she did an 8.5x11" output with 0.5" margin all around
+(actual size of useful area 7.5x10") and we scaled it
+up by 4.8.
+
+We also copy the Info dict to the new PDF.
'''
import sys
import os
-import find_pdfrw
-from pdfrw import PdfReader, PdfWriter, PdfDict, PdfName, PdfArray, IndirectPdfDict
-from pdfrw.buildxobj import pagexobj
-
-def adjust(page):
- page = pagexobj(page)
- assert page.BBox == [0, 0, 11 * 72, int(8.5 * 72)], page.BBox
- margin = 72 // 2
- old_x, old_y = page.BBox[2] - 2 * margin, page.BBox[3] - 2 * margin
-
- new_x, new_y = 48 * 72, 36 * 72
- ratio = 1.0 * new_x / old_x
- assert ratio == 1.0 * new_y / old_y
-
- index = '/BasePage'
- x = -margin * ratio
- y = -margin * ratio
- stream = 'q %0.2f 0 0 %0.2f %s %s cm %s Do Q\n' % (ratio, ratio, x, y, index)
- xobjdict = PdfDict()
- xobjdict[index] = page
-
- return PdfDict(
- Type = PdfName.Page,
- Contents = PdfDict(stream=stream),
- MediaBox = PdfArray([0, 0, new_x, new_y]),
- Resources = PdfDict(XObject = xobjdict),
- )
-
-def go(inpfn, outfn):
- reader = PdfReader(inpfn)
- page, = reader.pages
- writer = PdfWriter()
- writer.addpage(adjust(page))
- writer.trailer.Info = IndirectPdfDict(reader.Info)
- writer.write(outfn)
-
-if __name__ == '__main__':
- inpfn, = sys.argv[1:]
- outfn = 'poster.' + os.path.basename(inpfn)
- go(inpfn, outfn)
+from pdfrw import PdfReader, PdfWriter, PageMerge, IndirectPdfDict
+
+
+def adjust(page, margin=36, scale=4.8):
+ info = PageMerge().add(page)
+ x1, y1, x2, y2 = info.xobj_box
+ viewrect = (margin, margin, x2 - x1 - 2 * margin, y2 - y1 - 2 * margin)
+ page = PageMerge().add(page, viewrect=viewrect)
+ page[0].scale(scale)
+ return page.render()
+
+
+inpfn, = sys.argv[1:]
+outfn = 'poster.' + os.path.basename(inpfn)
+reader = PdfReader(inpfn)
+writer = PdfWriter()
+writer.addpage(adjust(reader.pages[0]))
+writer.trailer.Info = IndirectPdfDict(reader.Info or {})
+writer.write(outfn)