Create a Multilanguage PDF in Python

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Perseo

    Create a Multilanguage PDF in Python

    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

  • Rob Wolfe

    #2
    Re: Create a Multilanguage PDF in Python

    "Perseo" <mturillo@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.pdfba se import pdfmetrics
    from reportlab.pdfba se.ttfonts import TTFont
    from reportlab.pdfge n import canvas

    pdfmetrics.regi sterFont(TTFont ('Verdana', 'Verdana.ttf'))
    c = canvas.Canvas(" pl.pdf")
    c.setFont("Verd ana", 12)
    c.drawString(10 0, 600, "Witaj, ¶wiecie!".decod e("iso-8859-2").encode(" utf-8"))
    c.showPage()
    c.save()
    </code>


    [1] http://www.reportlab.org/

    --
    HTH,
    Rob

    Comment

    • Perseo

      #3
      Re: Create a Multilanguage PDF in Python

      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" <mturillo@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.pdfba se import pdfmetrics
      from reportlab.pdfba se.ttfonts import TTFont
      from reportlab.pdfge n import canvas
      >
      pdfmetrics.regi sterFont(TTFont ('Verdana', 'Verdana.ttf'))
      c = canvas.Canvas(" pl.pdf")
      c.setFont("Verd ana", 12)
      c.drawString(10 0, 600, "Witaj, ¶wiecie!".decod e("iso-8859-2").encode(" utf-8"))
      c.showPage()
      c.save()
      </code>


      [1] http://www.reportlab.org/

      --
      HTH,
      Rob

      Comment

      • Perseo

        #4
        Re: Create a Multilanguage PDF in Python

        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" <mturillo@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.pdfba se import pdfmetrics
        from reportlab.pdfba se.ttfonts import TTFont
        from reportlab.pdfge n import canvas
        >
        pdfmetrics.regi sterFont(TTFont ('Verdana', 'Verdana.ttf'))
        c = canvas.Canvas(" pl.pdf")
        c.setFont("Verd ana", 12)
        c.drawString(10 0, 600, "Witaj, ¶wiecie!".decod e("iso-8859-2").encode(" utf-8"))
        c.showPage()
        c.save()
        </code>


        [1] http://www.reportlab.org/

        --
        HTH,
        Rob

        Comment

        • Rob Wolfe

          #5
          Re: Create a Multilanguage PDF in Python


          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

          Comment

          • Perseo

            #6
            Re: Create a Multilanguage PDF in Python

            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

            Comment

            • Diez B. Roggisch

              #7
              Re: Create a Multilanguage PDF in Python

              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

              Comment

              • Perseo

                #8
                Re: Create a Multilanguage PDF in Python

                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

                Comment

                • Perseo

                  #9
                  Re: Create a Multilanguage PDF in Python

                  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]


                  <code>
                  #!/usr/bin/python

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

                  from reportlab.pdfba se import pdfmetrics
                  from reportlab.pdfba se.ttfonts import TTFont
                  from reportlab.pdfge n import canvas

                  pdfmetrics.regi sterFont(TTFont ('Verdana', 'Verdana.ttf'))
                  c = canvas.Canvas(" pl.pdf")
                  c.setFont("Verd ana", 12)
                  c.drawString(10 0, 600, "Witaj,
                  ¶wiecie!".decod e("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

                  Comment

                  • Rob Wolfe

                    #10
                    Re: Create a Multilanguage PDF in Python


                    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:

                    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:


                    HTH,
                    Rob

                    Comment

                    • Perseo

                      #11
                      Re: Create a Multilanguage PDF in Python

                      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:

                      >
                      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:

                      >
                      HTH,
                      Rob

                      Comment

                      • Perseo

                        #12
                        Re: Create a Multilanguage PDF in Python

                        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:

                        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:


                        HTH,
                        Rob

                        Comment

                        • Perseo

                          #13
                          Re: Create a Multilanguage PDF in Python

                          Hi Rob this is my code:

                          <code>
                          #!/usr/bin/python

                          import time, os, sys
                          from reportlab.pdfba se import pdfmetrics
                          from reportlab.pdfba se.ttfonts import TTFont
                          from reportlab.pdfge n import canvas
                          from reportlab.lib.u nits import inch, cm
                          from reportlab.lib.p agesizes 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.regi sterFont(TTFont ('Verdana', 'verdana.ttf'))
                          canv = canvas.Canvas(" test.pdf")

                          def drawPageFrame(m ycanv):
                          mycanv.line(lef t_margin, top_margin, right_margin, top_margin)
                          mycanv.setFont( 'Verdana',12)
                          mycanv.drawStri ng(left_margin, top_margin + 2, "Pdf Test")
                          mycanv.line(lef t_margin, top_margin, right_margin, top_margin)

                          mycanv.line(lef t_margin, bottom_margin, right_margin, bottom_margin)
                          mycanv.drawCent redString(0.5*A 4[0], 0.5 * inch,
                          "Page %d" % mycanv.getPageN umber())

                          canv.setPageCom pression(1)
                          drawPageFrame(c anv)

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

                          canv.setFont("V erdana", 18)
                          canv.drawCentre dString(0.5 * A4[0], 5 * inch, "Test Staff")

                          canv.setFont("V erdana", 12)
                          tx = canv.beginText( left_margin, 3 * inch)
                          tx.textLine("Th is is a test to a PDF Exporting Tool")
                          canv.drawText(t x)
                          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@otenet.g r
                          ηλεκτροΠ½Î¹ÎºÎ¿Ï |
                          ταχυδροΠ¼ÎµÎ¯Î¿Ï… |
                          Κινητό τηλέφωνΠ¿ | 00345353453453
                          Τόπος Î³Î­Î½Î½Î·ÏƒÎ·Ï ‚ | Thessaloniki
                          Χώρα | Greece
                          ΥπηκοότΠ·Ï„α | Greek
                          ΗμερομηΠ½Î¯Î± |
                          Î³Î­Î½Î½Î·ÏƒÎ·Ï ‚ |
                          Μητρική γλώσσα | Greek
                          Rob Wolfe wrote:
                          "Perseo" <mturillo@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.pdfba se import pdfmetrics
                          from reportlab.pdfba se.ttfonts import TTFont
                          from reportlab.pdfge n import canvas
                          >
                          pdfmetrics.regi sterFont(TTFont ('Verdana', 'Verdana.ttf'))
                          c = canvas.Canvas(" pl.pdf")
                          c.setFont("Verd ana", 12)
                          c.drawString(10 0, 600, "Witaj, ¶wiecie!".deco de("iso-8859-2").encode(" utf-8"))
                          c.showPage()
                          c.save()
                          </code>


                          [1] http://www.reportlab.org/

                          --
                          HTH,
                          Rob

                          Comment

                          Working...