py-pdf / fpdf2

Simple PDF generation for Python
https://py-pdf.github.io/fpdf2/
GNU Lesser General Public License v3.0
1.12k stars 253 forks source link

writehtml Turkish Character Error #1281

Closed mglnr closed 3 weeks ago

mglnr commented 1 month ago

Hello,

I'm testing with writehtml at https://py-pdf.github.io/fpdf2/HTML.html. Unfortunately, I could not run the application when I used Turkish characters in my test. The application does not open when you type the characters "ğĞİÖÖüÜ". I added add_font with external Turkish character support, but the problem persists.

Lucas-C commented 1 month ago

Hi @mglnr

I'd be happy to try to help you, but the information you provided is not precise enough to be able to solve your problem.

Could you please provide some minimal code reproducing your problem: https://stackoverflow.com/help/minimal-reproducible-example

Also, what do you mean by "The application does not open"?

Do you get an error when running your Python script?

Or is it that you cannot open the PDF file produced?

At this point, I can only guess that the font file you provided (the "external Turkish character support") simply does not render the characters ğĞİÖÖüÜ

mglnr commented 1 month ago

Thank you for your interest. When I type one of the characters "ğĞİÖÖÜÜ" and run my test application, the application closes without creating the pdf. When I use only the characters I specified, the situation I specified occurs.

Lucas-C commented 1 month ago

When I type one of the characters "ğĞİÖÖÜÜ" and run my test application, the application closes without creating the pdf.

Do you have any error produced?

If the call to .output() fails to create a file, you should probably have an error raised.

If so, could you please share the stacktrace?

Otherwise there is really not much I can do to help you...

andersonhc commented 1 month ago

@mglnr Here is a small test I did showing fpdf2 can handle the characters if you are using a compatible font:

from fpdf import FPDF

pdf = FPDF()
pdf.add_page()
pdf.add_font(family="Noto", fname="NotoSans-Regular.ttf")
pdf.set_font("Noto", "", 16)
pdf.write_html("""
  <h1>ğĞİÖÖÜÜ</h1>
""")
pdf.output("issue1281.pdf")

I really don't know how to help you any further without specific information about your code and the error you are getting.

mglnr commented 1 month ago

"ğĞİÖÖÜÜ" karakterlerinden birini yazıp test uygulamamı çalıştırdığımda pdf oluşturulmadan uygulama kapanıyor.

Herhangi bir hata oluştu mu?

Eğer çağrı .output()dosya oluşturmada başarısız olursa, muhtemelen bir hatayla karşılaşırsınız.

Eğer öyleyse, lütfen stacktrace'i paylaşabilir misiniz?

Aksi takdirde sana yardımcı olabileceğim pek bir şey yok...

I am sharing my test code below. I encounter an error when I use the characters ĞĞİÖÖÜÜ in the 14th line. My Test Code:

#

from fpdf import FPDF
pdf = FPDF()
pdf.add_page()
pdf.add_font("Times New Roman", "", "times.ttf", uni=True)  # Normal font
pdf.add_font("Times New Roman", "B", "timesbd.ttf", uni=True)  # Kalın font
pdf.write_html("""
  <dl>
      <dt>Description title</dt>
      <dd>Description Detail</dd>
  </dl>
  <h1>Big title</h1>
    <p align="justify"><b>deneme</b>
      ğĞİÖÖüÜ deneme deneme deneme deneme deneme deneme deneme deneme deneme deneme deneme deneme deneme deneme deneme deneme 
      deneme deneme deneme deneme deneme deneme deneme deneme 
      deneme deneme deneme deneme deneme deneme deneme deneme deneme deneme deneme deneme deneme deneme deneme deneme deneme deneme deneme 
      deneme deneme deneme deneme deneme deneme deneme deneme deneme deneme deneme 
    </p>
    <br>
    <pre>i am preformatted text.</pre>
    <br>
    <blockquote>hello blockquote</blockquote>
    <table width="50%" border="1">
      <thead>
        <tr>
          <th width="30%">ID</th>
          <th width="70%">Name</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>1</td>
          <td>Alice</td>
        </tr>
        <tr>
          <td>2</td>
          <td>Bob</td>
        </tr>
      </tbody>
    </table>
""")
pdf.output("html.pdf")
input("pencereyi kapatma") 

``

mglnr commented 1 month ago

@mglnr Here is a small test I did showing fpdf2 can handle the characters if you are using a compatible font:

from fpdf import FPDF

pdf = FPDF()
pdf.add_page()
pdf.add_font(family="Noto", fname="NotoSans-Regular.ttf")
pdf.set_font("Noto", "", 16)
pdf.write_html("""
  <h1>ğĞİÖÖÜÜ</h1>
""")
pdf.output("issue1281.pdf")

I really don't know how to help you any further without specific information about your code and the error you are getting.

Thank you for your interest. When I changed the family to times-roman in the code you gave, the Turkish characters worked, but when I wrote the html table code below it, it did not work. Sample code:

from fpdf import FPDF

pdf = FPDF()
pdf.add_page()
pdf.add_font(family="Times-Roman", fname="times.ttf")
pdf.set_font("Times-Roman", "", 12)
pdf.write_html("""
  <p align="justify"> deneme deneme </p>
    <table width="50%" border="1">
      <thead>
        <tr>
          <th width="30%">ID</th>
          <th width="70%">Name</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>1</td>
          <td>Alice</td>
        </tr>
        <tr>
          <td>2</td>
          <td>Bob</td>
        </tr>
      </tbody>
    </table>
""")
pdf.output("html.pdf")

input("Pencereyi kapatma.")
andersonhc commented 1 month ago

The default is table header being in bold and your code is failing because you didn't add a bold version of the font.

Add this line and your code should run OK:

pdf.add_font(family="Times-Roman", style="B", fname="timesbd.ttf")
mglnr commented 1 month ago

The default is table header being in bold and your code is failing because you didn't add a bold version of the font.

Add this line and your code should run OK:

pdf.add_font(family="Times-Roman", style="B", fname="timesbd.ttf")

Thank you. I missed that detail.

Lucas-C commented 3 weeks ago

Do you have any more questions, or can this issue be closed? 🙂