Create a Multilanguage PDF in Python

P

Perseo

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
 
R

Rob Wolfe

Perseo said:
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/
 
P

Perseo

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 said:
Perseo said:
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/
 
P

Perseo

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 said:
Perseo said:
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/
 
R

Rob Wolfe

Perseo said:
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
 
P

Perseo

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

Diez B. Roggisch

Perseo said:
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
 
P

Perseo

Thanks I will try 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
 
P

Perseo

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?!
 
R

Rob Wolfe

Perseo said:
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.html#SECTION004220000000000000000
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.html#SECTION008110000000000000000

HTH,
Rob
 
P

Perseo

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
 
P

Perseo

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

Perseo

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
ΔιεÏθυνση | (e-mail address removed)
ηλεκτÏÎ¿Î½Î¹ÎºÎ¿Ï |
ταχυδÏομείου |
Κινητό τηλέφωνο | 00345353453453
Τόπος γέννησης | Thessaloniki
ΧώÏα | Greece
Υπηκοότητα | Greek
ΗμεÏομηνία |
γέννησης |
ΜητÏική γλώσσα | Greek
Rob said:
Perseo said:
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/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top