By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,677 Members | 1,177 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,677 IT Pros & Developers. It's quick & easy.

Create a Multilanguage PDF in Python

P: n/a
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

Anyone can help me, please?
Thanks
Perseo

Aug 20 '06 #1
Share this Question
Share on Google+
12 Replies


P: n/a
"Perseo" <mt******@gmail.comwrites:
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
Aug 20 '06 #2

P: n/a
Hi Rob,

thank you for your answer, but I'm a newbie in Web application written
in Python. Now I try your suggestion ...

Thanks
Perseo
Rob Wolfe wrote:
"Perseo" <mt******@gmail.comwrites:
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
Aug 20 '06 #3

P: n/a
Hi again,

WORKS!!! I download all I need as python + reportlab. Only 2 questions:

1. I need all of this package? because it is 6Mb!
2. How can I connect my software with MySql. In my Hosting is present
the Python support but I don't thing that the MySQLdb is present. How
can I solve this little problem?

Thanks
Perseo
Rob Wolfe wrote:
"Perseo" <mt******@gmail.comwrites:
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
Aug 20 '06 #4

P: n/a

Perseo wrote:
Hi again,

WORKS!!! I download all I need as python + reportlab. Only 2 questions:

1. I need all of this package? because it is 6Mb!
I'm afraid you need all of it.
BTW My reportlab package is only 3MB... hmm strange.
2. How can I connect my software with MySql. In my Hosting is present
the Python support but I don't thing that the MySQLdb is present. How
can I solve this little problem?
You can install MySQLdb wherever you want. You need only to make
sure the module is in your PYTHONPATH.

HTH,
Rob

Aug 21 '06 #5

P: n/a
I can't upload in the PYTHONPATH but in a normal folder of our site.
Exist another way to do it?
Thanks

Rob Wolfe wrote:
Perseo wrote:
Hi again,

WORKS!!! I download all I need as python + reportlab. Only 2 questions:

1. I need all of this package? because it is 6Mb!

I'm afraid you need all of it.
BTW My reportlab package is only 3MB... hmm strange.
2. How can I connect my software with MySql. In my Hosting is present
the Python support but I don't thing that the MySQLdb is present. How
can I solve this little problem?

You can install MySQLdb wherever you want. You need only to make
sure the module is in your PYTHONPATH.

HTH,
Rob
Aug 21 '06 #6

P: n/a
Perseo wrote:
I can't upload in the PYTHONPATH but in a normal folder of our site.
Exist another way to do it?
PYTHONPATH is an environment variable. You can set it to arbitrary paths,
and python will look for module there, too. Or you modify sys.path before
you try the import.

Diez
Aug 21 '06 #7

P: n/a
Thanks I will try it.

Diez B. Roggisch wrote:
Perseo wrote:
I can't upload in the PYTHONPATH but in a normal folder of our site.
Exist another way to do it?

PYTHONPATH is an environment variable. You can set it to arbitrary paths,
and python will look for module there, too. Or you modify sys.path before
you try the import.

Diez
Aug 22 '06 #8

P: n/a
Nothing to do!
I enable test2.py and the folder with 777 permission and I write at the
top of the file the PYTHONPATH "#!/usr/bin/python" as you told me but
it doesn't works as well.

this is my organization:
www.domain.com/python/reportlab/ [all files]
www.domain.com/python/test2.py

<code>
#!/usr/bin/python

print "Content-Type: text/html\n\n"
print "Hello World"

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>

The verdana font doesn't exist in the reportlab fonts folder. I try to
delete the rows where it is called like (registerFont, setFont) but
nothing to do.

any suggestion?!

Diez B. Roggisch wrote:
Perseo wrote:
I can't upload in the PYTHONPATH but in a normal folder of our site.
Exist another way to do it?

PYTHONPATH is an environment variable. You can set it to arbitrary paths,
and python will look for module there, too. Or you modify sys.path before
you try the import.

Diez
Aug 22 '06 #9

P: n/a

Perseo wrote:
Nothing to do!
I enable test2.py and the folder with 777 permission and I write at the
top of the file the PYTHONPATH "#!/usr/bin/python" as you told me but
it doesn't works as well.
#!/usr/bin/python is not PYTHONPATH. I think you should
read this:
http://docs.python.org/tut/node4.htm...00000000000000
The verdana font doesn't exist in the reportlab fonts folder. I try to
delete the rows where it is called like (registerFont, setFont) but
nothing to do.
Fonts are not part of reportlab package. You should find it
in your OS or in the net. You need to know what font do you want to
use.
any suggestion?!
Install reportlab package in your home directory, for example
/home/perseo/python/lib

and then in the shell write this:

export PYTHONPATH=/home/perseo/python/lib

and look here:
http://docs.python.org/tut/node8.htm...00000000000000

HTH,
Rob

Aug 23 '06 #10

P: n/a
PERFECT! Done! Thanks
Now I create a little file in pdf format but the data are in the MySql
database! :(
How can I connect to it?

Thanks for all suggestions
Perseo
Rob Wolfe wrote:
Perseo wrote:
Nothing to do!
I enable test2.py and the folder with 777 permission and I write at the
top of the file the PYTHONPATH "#!/usr/bin/python" as you told me but
it doesn't works as well.

#!/usr/bin/python is not PYTHONPATH. I think you should
read this:
http://docs.python.org/tut/node4.htm...00000000000000
The verdana font doesn't exist in the reportlab fonts folder. I try to
delete the rows where it is called like (registerFont, setFont) but
nothing to do.

Fonts are not part of reportlab package. You should find it
in your OS or in the net. You need to know what font do you want to
use.
any suggestion?!

Install reportlab package in your home directory, for example
/home/perseo/python/lib

and then in the shell write this:

export PYTHONPATH=/home/perseo/python/lib

and look here:
http://docs.python.org/tut/node8.htm...00000000000000

HTH,
Rob
Aug 23 '06 #11

P: n/a
It's too difficult for me, anyone can help me contact me thru chat or
something else, please. It drives me crazy!

Perseo wrote:
PERFECT! Done! Thanks
Now I create a little file in pdf format but the data are in the MySql
database! :(
How can I connect to it?

Thanks for all suggestions
Perseo
Rob Wolfe wrote:
Perseo wrote:
Nothing to do!
I enable test2.py and the folder with 777 permission and I write at the
top of the file the PYTHONPATH "#!/usr/bin/python" as you told me but
it doesn't works as well.
#!/usr/bin/python is not PYTHONPATH. I think you should
read this:
http://docs.python.org/tut/node4.htm...00000000000000
The verdana font doesn't exist in the reportlab fonts folder. I try to
delete the rows where it is called like (registerFont, setFont) but
nothing to do.
Fonts are not part of reportlab package. You should find it
in your OS or in the net. You need to know what font do you want to
use.
any suggestion?!
Install reportlab package in your home directory, for example
/home/perseo/python/lib

and then in the shell write this:

export PYTHONPATH=/home/perseo/python/lib

and look here:
http://docs.python.org/tut/node8.htm...00000000000000

HTH,
Rob
Aug 23 '06 #12

P: n/a
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
Διεύθυνση| jo****@otenet.gr
ηλεκτρονικού |
ταχυδρομείου |
Κινητό τηλ*φωνο | 00345353453453
Τόπος γ*ννησης | Thessaloniki
Χώρα | Greece
Υπηκοότητα | Greek
Ημερομηνία |
γ*ννησης |
Μητρική γλώσσα | Greek
Rob Wolfe wrote:
"Perseo" <mt******@gmail.comwrites:
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
Aug 23 '06 #13

This discussion thread is closed

Replies have been disabled for this discussion.