--- author: '' category: '' date: 2013/04/05 21:54:26 description: A different way to use rst2pdf. link: '' priority: '' slug: using-rst2pdf-in-different-ways tags: python, rst2pdf title: Using rst2pdf in Different Ways type: text updated: 2013/04/05 21:54:26 url_type: '' --- 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: .. code:: python 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.