Skip to main content

Ralsina.Me — Roberto Alsina's website

Posts about rst2pdf (old posts, page 1)

Giving rst2pdf some love

Be­cause of a thread in the PyAr list about gen­er­at­ing re­ports from Python, I sug­gest­ed us­ing ReST and my rst2pdf scrip­t.

This caused a few things:

  1. I de­­cid­ed it's a pret­­ty de­­cent piece of code, and it de­serves a re­lease. Mak­ing a re­lease means I need­ed to fix the most em­bar­ras­ing pieces of it. So...

  2. Im­­ple­­men­t­ed the class di­rec­­tive, so it can have cus­­tom para­­graph styles with very lit­­tle ef­­fort.

  3. Did prop­er com­­mand line pars­ing.

  4. Did prop­er se­­tup­­tools script

  5. Up­­load­­ed to PyPI

  6. Cre­at­ed a re­lease in Google Code.

So, if you want the sim­plest way to gen­er­ate PDF files from a pro­gram in the en­tire python­ic uni­verse... give it a look.

rst2pdf: New and improved

My rst2pdf script has had sev­er­al things hap­pen to it.

  1. It got an­oth­er guy work­ing on it: Christoph Zw­er­schke

  2. It's on google­­code now: http://rst2pdf.­­google­­code.org

  3. Christoph made a num­ber of im­prove­­ments:

    • bul­let­ed and enu­mer­at­ed list sim­­­pli­­­fied, use same font as text for bul­lets and num­bers

    • links in ta­ble of con­­­tents work

    • com­­­press lit­er­al sec­­­tions hor­i­­­zon­­­tal­­­ly so that they al­ways fit on the page

  4. I have in­­te­­grat­ed hy­phen­a­tion us­ing wor­­daxe (works on­­ly with re­­port­lab 2.1)

The out­put us­ing hy­phen­ation is re­al­ly ug­ly right now (for ex­am­ple, I get a black square in­stead of an hy­phen) but it's a small step for­ward.

rst2pdf again

I did a lit­tle (very lit­tle) more work on rst2pdf [1]

Bet­ter:

  • Head­­ers

  • Foot­ers

  • Foot­notes (as end­notes, re­al foot­notes are too much work be­­cause you have to re­flow the tex­t)

  • Ex­ter­­nal links (http, email)

  • Some styling im­prove­­ments (mean­ing: the out­­put is not so painful in the eye­­s)

  • Pa­per size sup­­port

  • Re­al sep­a­ra­­tors

Then there are some things you just get be­cause it's done us­ing re­port­lab:

  • True­­Type font em­bed­d­ing (check the ex­am­­ple be­low, and look at the monospaced font)

  • No hy­phen­a­tion (although there is a project on the web that claims to have done it, I should check it out)

Still bro­ken:

  • No way to put things like page num­bers or sec­­tion names in head­­er­s/­­foot­ers

  • The foot­notes are not linked to their con­­tents and vicev­er­sa

  • In fac­t, no in­­ter­­nal links work, in­­­clud­ing ti­­tle re­f­er­ences

  • Ta­bles are pret­­ty bro­ken

And here is the usu­al ver­sion of the rst de­mo show­ing im­prove­ments.

Done with rst2pdf for now

It works much bet­ter than it did last night.

The main miss­ing/bro­ken things are:

  • Ta­bles

  • Links

  • Foot­notes/c­i­­ta­­tions

  • Head­­er­s/­­Foot­ers

  • Re­f­er­ences

  • Ta­ble HEad­­er­s/S­­pan­n­ing cells

  • Lists that don't start at 1

The rest seems to be in work­ing or­der and pro­duc­ing de­cent out­put al­ready. It can process the ReST de­mo and it does­n't look bad: check it out

I say, good for half a day of hack­ing, and will now stop, be­cause it's a week­end and I have a fam­i­ly :-)

But that does­n't mean you can't try it for your­self. Just run it thus:

python rst2pdf.py myrstfile.txt

And you will get (hopefully) a myrstfile.txt.pdf

Let me know how it work­s, and if it does­n't and it's not re­lat­ed to one of the things I men­tioned above as bro­ken, send me a test file!

Generating PDFs from Restructured text

This has al­ways been pos­si­ble, go­ing via La­TeX.

How­ev­er, La­TeX be­ing what it is, you ei­ther need to learn it, or you end up with rather plain-look­ing doc­u­ments.

While that's ok for a man­u­al, I want to use Re­struc­tured Text for ev­ery­thing in­volv­ing doc­u­ments.

So, I looked for an­oth­er so­lu­tion. Sad­ly, I could not make the ex­ist­ing rlpdf writ­er work, sooooo I de­cid­ed to write my own tool.

Since I in­tense­ly dis­like the Vis­i­tor pat­tern in­volved in writ­ing a reg­u­lar do­cu­tils writer, I adapt­ed my old rst2rst.py and end­ed with rst2pdf.py which just tra­vers­es the tree re­cur­sive­ly and writes the PDF us­ing Re­port­Lab.

And it took me about 3 hours to make it work:

  • For a lim­it­ed sub­­set of RST (no foot­notes, no links, no dec­o­ra­­tion)

  • For some sub­­set of ta­bles (no col/row span­n­ing)

  • With lim­it­ed "styling" (it's most­­ly there, but I need to write a lot of Re­­port­Lab styles.

How well does it work... rather well.

Here's a gen­er­at­ed PDF of The Re­struc­tured­Text Primer

Ig­nore aes­thet­ic­s, and con­sid­er func­tion, it's pret­ty good.


Contents © 2000-2023 Roberto Alsina