Skip to main content
Archived

This project has been archived by its maintainers, and is no longer receiving any updates.

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

Release files for z3c.pdftemplate 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for z3c.pdftemplate 0.2.0
File Size Uploaded
z3c.pdftemplate-0.2.0.tar.gz 363.5 kB Details

Release files / z3c.pdftemplate-0.2.0.tar.gz

Download URL z3c.pdftemplate-0.2.0.tar.gz
Size 363.5 kB
Tags Source
SHA-256 checksum
How to use checksums
80560e7f0588d46eb8a5622201d6de9139bb798eeefee711844b9513499fe732
BLAKE2b-256 checksum
How to use checksums
e247b8c20c8fa1e6268de3e684243cf5dfab284fbfc984a637e5b0c487ff0f36
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.2.0 This release

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page