summaryrefslogtreecommitdiff
path: root/examples/rl1/platypus_pdf_template.py
diff options
context:
space:
mode:
Diffstat (limited to 'examples/rl1/platypus_pdf_template.py')
-rwxr-xr-xexamples/rl1/platypus_pdf_template.py30
1 files changed, 16 insertions, 14 deletions
diff --git a/examples/rl1/platypus_pdf_template.py b/examples/rl1/platypus_pdf_template.py
index 8c85794..7e4769a 100755
--- a/examples/rl1/platypus_pdf_template.py
+++ b/examples/rl1/platypus_pdf_template.py
@@ -1,17 +1,18 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
-usage: platypus_pdf_template.py output.pdf pdf_file_to_use_as_template.pdf
+usage: platypus_pdf_template.py source.pdf
-Example of using pdfrw to use a pdf (page one) as the background for all
-other pages together with platypus.
+Creates platypus.source.pdf
-There is a table of contents in this example for completeness sake.
+Example of using pdfrw to use page 1 of a source PDF as the background
+for other pages programmatically generated with Platypus.
Contributed by user asannes
"""
import sys
+import os
from reportlab.platypus import PageTemplate, BaseDocTemplate, Frame
from reportlab.platypus import NextPageTemplate, Paragraph, PageBreak
@@ -21,7 +22,6 @@ from reportlab.rl_config import defaultPageSize
from reportlab.lib.units import inch
from reportlab.graphics import renderPDF
-import find_pdfrw
from pdfrw import PdfReader
from pdfrw.buildxobj import pagexobj
from pdfrw.toreportlab import makerl
@@ -29,6 +29,7 @@ from pdfrw.toreportlab import makerl
PAGE_WIDTH = defaultPageSize[0]
PAGE_HEIGHT = defaultPageSize[1]
+
class MyTemplate(PageTemplate):
"""The kernel of this example, where we use pdfrw to fill in the
background of a page before writing to it. This could be used to fill
@@ -57,6 +58,7 @@ class MyTemplate(PageTemplate):
canvas.doForm(rl_obj)
canvas.restoreState()
+
class MyDocTemplate(BaseDocTemplate):
"""Used to apply heading to table of contents."""
@@ -70,20 +72,22 @@ class MyDocTemplate(BaseDocTemplate):
self.canv.bookmarkPage(key)
self.notify('TOCEntry', [1, text, self.page, key])
+
def create_toc():
"""Creates the table of contents"""
table_of_contents = TableOfContents()
table_of_contents.dotsMinLevel = 0
- header1 = ParagraphStyle(name = 'Heading1', fontSize = 16, leading = 16)
- header2 = ParagraphStyle(name = 'Heading2', fontSize = 14, leading = 14)
+ header1 = ParagraphStyle(name='Heading1', fontSize=16, leading=16)
+ header2 = ParagraphStyle(name='Heading2', fontSize=14, leading=14)
table_of_contents.levelStyles = [header1, header2]
return [table_of_contents, PageBreak()]
+
def create_pdf(filename, pdf_template_filename):
"""Create the pdf, with all the contents"""
- pdf_report = open(filename, "w")
+ pdf_report = open(filename, "wb")
document = MyDocTemplate(pdf_report)
- templates = [ MyTemplate(pdf_template_filename, name='background') ]
+ templates = [MyTemplate(pdf_template_filename, name='background')]
document.addPageTemplates(templates)
styles = getSampleStyleSheet()
@@ -99,8 +103,6 @@ def create_pdf(filename, pdf_template_filename):
if __name__ == '__main__':
- try:
- output, template = sys.argv[1:]
- create_pdf(output, template)
- except ValueError:
- print "Usage: %s <output> <template>" % (sys.argv[0])
+ template, = sys.argv[1:]
+ output = 'platypus_pdf_template.' + os.path.basename(template)
+ create_pdf(output, template)