Generating a PDF file on Linux (try using xtopdf)

vasudevram vasudevram at gmail.com
Fri Sep 29 07:12:08 EDT 2006


George Adams wrote:
> Sorry for what is probably a very basic question...
>
> I have a database of contact info data (names, addresses, phone, etc.)
> I want to take that data and generate a printable booklet-form directory
> that can be handed out to people.
>
> So the database may look something like this:
>
>    FNAME,LNAME,ADDRESS,HOMEPHONE,WORKPHONE,HOMEEMAIL,WORKEMAIL
>
>    "George","Adams","123 Faketreet",
>    NULL,"555-1212",NULL,"george at example.net"
>
>    "John","Doe","456 Nowhere Lane",
>    "555-1111","555-2222","johnhome at example.net","johnwork at example.net"
>
>
> and I want the printable output to "look" something like this:
>
>    <red><bold>Adams</bold></red>, George
>    123 Fake Street
>    Work phone: 555-1212
>    Work e-mail: george at example.net
>
>    <red><bold>Doe</bold></red>, John
>    456 Nowhere Lane
>    Home phone: 555-1111
>    Work phone: 555-2222
>    Home e-mail: johnhome at example.net
>    Work e-mail: johnwork at example.net
>
> I can use a scripting language like Perl to handle any programming logic
> I may need (such as not printing out a line for any fields that are
> empty in the database).  What I don't know is how to generate a nicely
> formatted PDF file containing the line breaks, red text, bold text, etc.
> that's ready for printing.
>
> Can anyone point me to the tool I need to make this happen?  (maybe it's
> a Perl module?  Maybe a standalone tool that takes my pseudocode
> <red><bold> and turns it into valid PDF code?)
>
> Thanks to anyone who can help!

My xtopdf toolkit may be of help. It has both end-user tools (some
command-line, some GUI (using wxPython (v 2.6 or higher needed)), and
an API for developers. Both the tools and the API are quite easy to
use.

Check the links here:

http://www.dancingbison.com
This page has a link to an article I wrote for Packt Publishing
(http://www.packtpub.com), on how to use xtopdf to generate PDF from
CSV data - which is the kind you have. You can use the article and the
accompanying code (links available in the article) as a model for your
needs.

http://www.dancingbison.com/products.html
This page has links to get xtopdf, a guide to installing and using it
(on Windows), etc.
xtopdf is cross-platform, works on both Linux and Windows. I haven't
yet written a Linux-specific install and use guide, but the generic
instructions in the README should suffice if you know Linux some, how
to install software on it, and how to set up additional Python
libraries on it. You can also post here if you have problems making
xtopdf work on Linux, and I'll try to help. (I developed and tested it
on Linux, then tested it on Windows, so it should work ok on Linux).

xtopdf is written in Python (http://www.python.org, needs v2.2 or
higher) and uses the open source version of the ReportLab toolkit
(http://www.reportlab.org, needs v1.17 or higher, but use the 1.x
series, its not tested with Reportlab v2.x, though it might work with
it).

IMO, Python is easier to learn and develop in than Perl, so if you say
you can do it in Perl, you should be able to do it in Python.

To get the line breaks you can write code to read each cell of each row
of the CSV file, and put the line breaks wherever you want to.

For color changes, its a little more difficult - though the ReportLab
toolkit supports colors, I didn't expose that support in the xtopdf
API, though I may in future. (I did expose the ability to set the font
for any chunk of text, though). If you don't mind digging into the
source, you can try making the changes to allow colors to be specified
for arbitrary chunks of text, as per your need. It shouldn't be too
difficult, since the code is written for understandability, and there
are enough comments.

The CSV and other new formats support (TDV, XLS) is alpha and may have
some bugs. Also, CSV is know to be a variable format - different
products like different spreadsheets, generate various "dialects" of
it. I've not (yet) tested the CSV support with many differing CSV
dialects, only with the MS Excel format of Office 2000.

Feel free to let me know if you find any bugs.

If you make the changes for color support, do consider sending them to
me, to include in the next release of xtopdf. Otherwise, I'll probably
add it myself.

HTH
Vasudev Ram
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Software training and consulting
http://www.dancingbison.com
10.times say "Truly rural"
~~~~~~~~~~~~~~~~~~~~~~~~~~~~




More information about the Python-list mailing list