Using rst2pdf in Different Ways

This was an idea by Dinu Gherman: you can use rst2pdf as a flowable generator for reportlab. Suppose you want to create, in a reportlab "story", a bunch of paragraphs, with emphasis, links, etc, and perhaps a table.

Using restructured text, it's something like this:

This is a paragraph. It has a link: http://rst2pdf.ralsina.me and then some random text.

+-------------+---------------------------+
| A table     | With cells                |
|             |                           |
|             |                           |
|             |                           |
|             |                           |
+-------------+---------------------------+
| And inside                              |
| it some                                 |
| more text                               |
|                                         |
|                                         |
+-----------------------------------------+

* And a list
* Just to make it harder

  + with a nested item here

It is, of course, perfectly possible to generate a bunch of reportlab (or rather platypus) flowables to represent all this. It will just mean some 75 lines of code. And if you change anything, then you have to edit code!

Or you can take advantage of rst2pdf and do this:

from docutils.core import publish_doctree
from rst2pdf.createpdf import RstToPdf
from reportlab.lib.units import cm
from reportlab.pdfgen.canvas import Canvas
from reportlab.platypus import Frame

rest_text = """
This is a paragraph. It has a link: http://rst2pdf.ralsina.me and then some random text.

+-------------+---------------------------+
| A table     | With cells                |
|             |                           |
|             |                           |
|             |                           |
|             |                           |
+-------------+---------------------------+
| And inside                              |
| it some                                 |
| more text                               |
|                                         |
|                                         |
+-----------------------------------------+

* And a list
* Just to make it harder

    + with a nested item here
"""
r2p = RstToPdf()
doctree = publish_doctree(rest_text)
story = r2p.gen_elements(doctree)
canv = Canvas("platypus-rest.pdf")
f = Frame(2 * cm, 2 * cm, 16 * cm, 18 * cm, showBoundary=True)
f.addFromList(story, canv)
canv.save()

This produces this pdf. And of course editing it is rather easier than editing code. Since you are not using rst2pdf to do the final PDF generation, you can use these flowables in your own documents.

The bad news

Some things will not work, like headings, since rst2pdf creates flowables that do a ton of things like adding themselves on indexes and such. If you want a heading-like thing you can use classes:

.. class:: heading1

This will look like a heading

This is a regular paragraph.

Other random restructured text features may or may not work, like footnotes or citations.

Comments

Comments powered by Disqus