Skip to main content

Ralsina.Me — Roberto Alsina's website

Using rst2pdf in Different Ways

This was an idea by Dinu Gher­man: you can use rst2pdf as a flow­able gen­er­a­tor for re­port­lab. Sup­pose you want to cre­ate, in a re­port­lab "s­to­ry", a bunch of para­graph­s, with em­pha­sis, links, etc, and per­haps a ta­ble.

Us­ing re­struc­tured tex­t, it's some­thing 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, per­fect­ly pos­si­ble to gen­er­ate a bunch of re­port­lab (or rather platy­pus) flow­ables to rep­re­sent all this. It will just mean some 75 lines of code. And if you change any­thing, then you have to ed­it code!

Or you can take ad­van­tage 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 pro­duces this pdf. And of course edit­ing it is rather eas­i­er than edit­ing code. Since you are not us­ing rst2pdf to do the fi­nal PDF gen­er­a­tion, you can use these flow­ables in your own doc­u­ments.

The bad news

Some things will not work, like head­ings, since rst2pdf cre­ates flow­ables that do a ton of things like adding them­selves on in­dex­es and such. If you want a head­ing-­like thing you can use class­es:

.. class:: heading1

This will look like a heading

This is a regular paragraph.

Oth­er ran­dom re­struc­tured text fea­tures may or may not work, like foot­notes or ci­ta­tion­s.


Contents © 2000-2024 Roberto Alsina