Create a Multilanguage PDF in Python

Perseo mturillo at gmail.com
Wed Aug 23 13:23:20 EDT 2006


Hi Rob this is my code:

<code>
#!/usr/bin/python

import time, os, sys
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.pdfgen import canvas
from reportlab.lib.units import inch, cm
from reportlab.lib.pagesizes import A4

#precalculate some basics
top_margin = A4[1] - inch
bottom_margin = inch
left_margin = inch
right_margin = A4[0] - inch
frame_width = right_margin - left_margin

pdfmetrics.registerFont(TTFont('Verdana', 'verdana.ttf'))
canv = canvas.Canvas("test.pdf")

def drawPageFrame(mycanv):
  mycanv.line(left_margin, top_margin, right_margin, top_margin)
  mycanv.setFont('Verdana',12)
  mycanv.drawString(left_margin, top_margin + 2, "Pdf Test")
  mycanv.line(left_margin, top_margin, right_margin, top_margin)

  mycanv.line(left_margin, bottom_margin, right_margin, bottom_margin)
  mycanv.drawCentredString(0.5*A4[0], 0.5 * inch,
  "Page %d" % mycanv.getPageNumber())

canv.setPageCompression(1)
drawPageFrame(canv)

#do some title page stuff
canv.setFont("Verdana", 36)
canv.drawCentredString(0.5 * A4[0], 7 * inch, "Pdf Test")

canv.setFont("Verdana", 18)
canv.drawCentredString(0.5 * A4[0], 5 * inch, "Test Staff")

canv.setFont("Verdana", 12)
tx = canv.beginText(left_margin, 3 * inch)
tx.textLine("This is a test to a PDF Exporting Tool")
canv.drawText(tx)
canv.showPage()

canv.save()

print "Content-Type: text/html\n\n"
print "<a href=\"test.pdf\">PDF Test</a>"

</code>


I would like to create a simple pdf splitted in two column with a
vertical row.

                               |
                Όνομα, Επώνυμο | John, Malkovic
                 Διεύθυνση (1) | 11 Main Street, Athens 54640
                               | Thessaloniki Greece
                 Διεύθυνση (2) |
                      Τηλέφωνο | 00302310886995
                     Διεύθυνση | john.m at otenet.gr
                  ηλεκτρονικού |
                 ταχυδρομείου  |
               Κινητό τηλέφωνο | 00345353453453
                Τόπος γέννησης | Thessaloniki
                          Χώρα | Greece
                    Υπηκοότητα | Greek
                    Ημερομηνία |
                      γέννησης |
                Μητρική γλώσσα | Greek
Rob Wolfe wrote:
> "Perseo" <mturillo at gmail.com> writes:
>
> > Hi guys,
> >
> > I'm disprate with the Pdf Unicode. I try to create a class using ufpdf
> > but some chars are not correct and now I would like try Python because
> > a friend tolds me that it's very powerful.
> > I need a simple script in Python that grab all Records from a MySql
> > table and print in the pdf file.
> >
> > The languages stored in my db are about 25 and they are:
> > Greek English French Hungarian Italian Lithuanian Dutch Portuguese
> > Albanian
> > Czech Danish German Spanish Estonian Finnish Irish Latvian Maltese
> > Polish Romanian
> > Russian Slovene Slovak Swedish
>
> You can give reportlab [1] a try. It has support for TrueType fonts
> and unicode translation using UTF-8. I used to use it for pdf files
> with polish chars.
>
> Some example code:
>
> <code>
> from reportlab.pdfbase import pdfmetrics
> from reportlab.pdfbase.ttfonts import TTFont
> from reportlab.pdfgen import canvas
>
> pdfmetrics.registerFont(TTFont('Verdana', 'Verdana.ttf'))
> c = canvas.Canvas("pl.pdf")
> c.setFont("Verdana", 12)
> c.drawString(100, 600, "Witaj, ¶wiecie!".decode("iso-8859-2").encode("utf-8"))
> c.showPage()
> c.save()
> </code>
> 
> 
> [1] http://www.reportlab.org/
> 
> -- 
> HTH,
> Rob




More information about the Python-list mailing list