[reportlab-users] script conveting text to pdf

Paul Tremblay phthenry at iglou.com
Tue Mar 13 21:32:07 EDT 2007

I have written a simple script to convert text to PDF. My script seeks
to imitate using a typewritter. It breaks lines where lines are broken
in the text file. It does not change font or font face. Except for the
two exceptions I list below, it prints exactly what the txt file looks
like, including blank lines.

The only features the script provides that text does not are
underlining and superscript, two features you can easily do on a type

In order to write a string of text, I use this code:

# this object is actually set up earlier in the script
self.__pdf = Canvas(write_file)

# simply draw the string, using the cooridinates of where you are
# in the file
self.__pdf.drawString(self.__current_x, self.__current_y,

# get text length
text_len = len(the_string)

# reset the current x value by moving it to the current position
# plus the font size times the font width, which I set at .6
# times the text length. So to change the x position of "Brown,"
# with a font size of 10, the equation looks like
# x = x + 10 * .6 * 5
self.__current_x = self.__current_x + self.__font_size*
self.__font_width *text_len

Of course, this only works with monspace font like Courier, but that
suits my purpose. I have tested this code on many lines, and it seems
to work perfect.

To write underine text, I first write the string using the code above
reset the x value *back* to whree it was, and then use this code:

x1 = self.__current_x
x2 = self.__current_x + self.__font_width * self.__font_size *
# self.__underline_below is currently set to 2pts
y1 = self.__current_y - self.__underline_below
y2 = self.__current_y - self.__underline_below
self.__pdf.line(x1, y1, x2, y2)
text_len = len(underlined_text)

# reset x value
self.__current_x = self.__current_x + self.__font_size*
self.__font_size *text_len

To do superscript, I simply raise the text one half of the font size
above the current y value.

After I write each line nof text, I move the y value:

self.__current_y -= self.__font_size * self.__leading

My script also has a mode called simple, in which it simply writes the
lines of txt to PDF without doing any underlining or superscript. The
code for this is very simple:

self.__line_obj = self.__pdf.beginText(mm * self.__left_margin, mm *
(292.10 - self.__top_margin))
self.__line_obj.setFont(self.__font, self.__font_size,
self.__leading * self.__font_size)

I am new to reportlab, so I don't know if any of this code will cause
unforseen problems, or if there are simpler ways to do what I want.

I am thinking of posting this script on sourceforge, because I don't
see any scripts that convert txt directly to PDF. The only script I
see I have come across is one called txt2pdf.pl. Although the code is
available, the script is shareware, not open source. Also, the script
cannot handlt unicode; it is limited to a few encodings.



More information about the reportlab-users mailing list