[reportlab-users] [PyPDF] I would like merge two A4 PDF documents in PDF document with A4 size + 10 mm (width + 10mm, height + 10mm)
KLEIN Stéphane
stephane at harobed.org
Fri Mar 27 06:10:34 EDT 2009
Le Thu, 26 Mar 2009 15:51:02 +0000, KLEIN Stéphane a écrit :
> Hi,
>
> To summarize, with PyPDF package, I would like merge two A4 PDF
> documents in PDF document with A4 size + 10 mm (width + 10mm, height +
> 10mm).
>
> First, I merge two PDF pages with this code :
>
> ::
>
> outputStream = file("output.pdf", "w") output = PdfFileWriter()
>
> background_pages = PdfFileReader(file("background.pdf", "rb"))
> foreground_pages = PdfFileReader(file("foreground.pdf", "rb"))
>
> bg_page0 = background_pages.getPage(0) fg_page0 =
> foreground_pages.getPage(0)
>
> bg_page0.mergePage( fg_page0 )
>
> output.addPage(page0)
>
> output.write(outputStream)
> outputStream.close()
>
> This works greatly.
>
> Background and foreground PDF documents size are A4 (paper size).
>
> From this point, I would like :
>
> 1. create an empty page with this paper size : A4 width + 10 mm, A4
> height + 10 mm
> 2. merge and center background page in current page (page create at step
> 1)
> 3. merge and center foreground page
>
> I don't know :
>
> * how to change page size ?
> * how to merge and center page ?
I found a solution to "translate" a page :
::
from reportlab.lib.units import mm
import pyPdf
def translate_pdf_page(page, delta_x, delta_y):
content = pyPdf.pdf.ContentStream(page["/Contents"].getObject(),
page.pdf)
content.operations.insert(0, [[], '1 0 0 1 %s %s cm' %
(delta_x,delta_y)] )
page[pyPdf.generic.NameObject('/Contents')] = content
outputStream = file("output.pdf", "w")
output = pyPdf.pdf.PdfFileWriter()
background_pages = pyPdf.pdf.PdfFileReader(file("background.pdf",
"rb"))
foreground_pages = pyPdf.pdf.PdfFileReader(file("foreground.pdf",
"rb"))
bg_page0 = background_pages.getPage(0)
fg_page0 = foreground_pages.getPage(0)
translate_pdf_page(fg_page0, 5 * mm, 5 * mm)
bg_page0.mergePage( fg_page0 )
output.addPage(bg_page0)
output.write(outputStream)
outputStream.close()
Regards,
Stephane
More information about the reportlab-users
mailing list