Ir al contenido principal

Ralsina.Me — El sitio web de Roberto Alsina

Using rst2pdf in Different Ways

This was an idea by Di­nu Gher­man: you can use rs­t2­pdf as a flo­wa­ble ge­ne­ra­tor for re­por­tlab. Su­ppo­se you want to crea­te, in a re­por­tlab "s­to­r­y", a bun­ch of pa­ra­gra­phs, wi­th em­pha­sis, li­nks, etc, and perhaps a ta­ble.

Using res­truc­tu­red tex­t, it's so­me­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 cour­se, per­fec­tly po­s­si­ble to ge­ne­ra­te a bun­ch of re­por­tlab (or ra­ther pla­ty­pus) flo­wa­bles to re­pre­sent all this. It wi­ll just mean so­me 75 li­nes of co­de. And if you chan­ge an­y­thin­g, then you ha­ve to edit co­de!

Or you can take ad­van­ta­ge of rs­t2­pdf 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­du­ces this pdf. And of cour­se edi­ting it is ra­ther ea­sier than edi­ting co­de. Sin­ce you are not using rs­t2­pdf to do the fi­nal PDF ge­ne­ra­tio­n, you can use the­se flo­wa­bles in your own do­cu­men­ts.

The bad news

So­me things wi­ll not wo­rk, like hea­dings, sin­ce rs­t2­pdf crea­tes flo­wa­bles that do a ton of things like adding the­msel­ves on in­dexes and su­ch. If you want a hea­din­g-­like thing you can use cla­sses:

.. class:: heading1

This will look like a heading

This is a regular paragraph.

Other ran­dom res­truc­tu­red text fea­tu­res may or may not wo­rk, like footno­tes or ci­ta­tion­s.


Contents © 2000-2024 Roberto Alsina