Welcome, guest | Sign In | My Account | Store | Cart

Notice! PyPM is being replaced with the ActiveState Platform, which enhances PyPM’s build and deploy capabilities. Create your free Platform account to download ActivePython or customize Python with the packages you require and get automatic updates.

Download
ActivePython
INSTALL>
pypm install z3c.pdftemplate

How to install z3c.pdftemplate

  1. Download and install ActivePython
  2. Open Command Prompt
  3. Type pypm install z3c.pdftemplate
 Python 2.7Python 3.2Python 3.3
Windows (32-bit)
0.2.0 Available View build log
0.1.0 Available View build log
Windows (64-bit)
0.2.0 Available View build log
0.1.0 Available View build log
Mac OS X (10.5+)
0.2.0 Available View build log
0.1.0 Available View build log
Linux (32-bit)
0.2.0 Available View build log
0.1.0 Available View build log
Linux (64-bit)
0.2.0 Available View build log
0.1.0 Available View build log
 
License
ZPL 2.1
Imports
Lastest release
version 0.2.0 on Jan 5th, 2011

Quickly generate PDF files using page templates and RML.

Using ReportLab to generate PDF Views

This package,

>>> import z3c.pdftemplate

provides the functionality of creating browser views that generate PDFs instead of HTML using reportlab's PDF writer technology. There are several ways to use the features in this package, which are demonstrated in the text below.

But first we have to load the directives' meta configuration:

>>> import zope.component
>>> from zope.publisher.browser import TestRequest
>>> from zope.configuration import xmlconfig
>>> context = xmlconfig.file('meta.zcml', package=z3c.pdftemplate)
Using z3c.rml and ReportLab to generate PDF Views

See DEPENDENCIES.cfg, this lib depends on some 3rd party libraries.

Purpose: PDF-Generration with the help of z3c.rml (an open source implementation of RML) and Reportlab. The z3c.rml is really a dialect of the official RML and supports many more features, such as charting while still remaining compatible with the commercial version of RML as much as possible.

This package provides the functionality of creating browser views that generate PDFs instead of HTML using reportlab's PDF writer technology.

But first we have to load the directives' meta configuration:

>>> from zope.configuration import xmlconfig
>>> context = xmlconfig.file('meta.zcml', package=z3c.pdftemplate)

RML, an XML-dialect developed by Reportlab.org, is much like HTML in that it lets you define the structure of a PDF document. The RML is dynamically generated using page templates and then used to generate a PDF file.

Let's say we want to create a PDF that shows the contents of a folder. The first step is to create a rml-document that contains the structure of the PDF. The following folder contents document template is available in rml_contents.pt

<?xml version="1.0" encoding="iso-8859-1" standalone="no" ?>
<!DOCTYPE document SYSTEM "rml_1_0.dtd">
<document
    xmlns:tal="http://xml.zope.org/namespaces/tal"
    xmlns:metal="http://xml.zope.org/namespaces/metal"
    filename="contents.pdf">
<content>

<para style="FolderName">
  Folder Name:
  <tal:block
     condition="context/__name__"
     replace="context/__name__|default" />
  <tal:block condition="not:context/__name__">&lt;no name&gt;</tal:block>
</para>

<spacer height="30" />

<table splitbyrow="1" repeatrows="0" repeatcols="0" style="ContentTable">
  <tr>
    <td>Name</td>
    <td>Title</td>
    <td>Size</td>
    <td>Created</td>
    <td>Modified</td>
  </tr>
  <tr tal:repeat="item view/listContentInfo">
    <td tal:content="item/id">me.png</td>
    <td tal:content="item/title|default">&lt;no title&gt;</td>
    <td tal:content="item/size/sizeForDisplay|nothing">34.5 kB</td>
    <td tal:content="item/created|default"></td>
    <td tal:content="item/modified|default"></td>
  </tr>
</table>

<action name="frameEnd" />

</content>
</document>

Pretty easy isn't it? Fortunately, we can simply reuse the Contents view class for the HTML contents view.

Now that we have the template and the document, we can simply register the view:

>>> context = xmlconfig.string("""
...     <configure xmlns:browser="http://namespaces.zope.org/browser">
...       <browser:rml2pdf
...           name="rmlsample.pdf"
...           for="zope.app.folder.interfaces.IFolder"
...           template="sample/rml_contents.pt"
...           class="zope.app.container.browser.contents.Contents"
...           permission="zope.Public"
...           />
...     </configure>
...     """, context)

Once we have created a folder instance:

>>> from zope.app.folder.folder import Folder
>>> folder = Folder()
>>> folder.__name__ = 'my folder'
>>> folder['subFolder'] = Folder()

we can can look up the view

>>> class Principal:
...   id = 'bob'
>>> request = TestRequest()
>>> request.setPrincipal(Principal())
>>> contents = zope.component.getMultiAdapter((folder, request),
...                                           name="rmlsample.pdf")

and create the PDF:

>>> contents() #doctest: +ELLIPSIS
'%PDF-1.4...'

CHANGES

0.2.0 (2010-12-12)
  • Added missing install dependencies and test dependencies.
  • Fixed tests to run with current package versions.
  • Adapted tests to reportlab 2.4+ which produces PDF 1.4, so requiring at least this reportlab version.
  • Using python's doctest module instead of the deprecated zope.testing.doctest.
  • Removed ZCML slugs and ZPKG ones.
  • Fixed REST of long_description.
0.1.0 (2008-02-15)
  • Initial Release

Subscribe to package updates

Last updated Jan 5th, 2011

Download Stats

Last month:1

What does the lock icon mean?

Builds marked with a lock icon are only available via PyPM to users with a current ActivePython Business Edition subscription.

Need custom builds or support?

ActivePython Enterprise Edition guarantees priority access to technical support, indemnification, expert consulting and quality-assured language builds.

Plan on re-distributing ActivePython?

Get re-distribution rights and eliminate legal risks with ActivePython OEM Edition.