Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I cannot render Khmer Unicode Properly in PDF file. #700

Closed
kuth-chi opened this issue Feb 20, 2023 · 15 comments
Closed

I cannot render Khmer Unicode Properly in PDF file. #700

kuth-chi opened this issue Feb 20, 2023 · 15 comments

Comments

@kuth-chi
Copy link

kuth-chi commented Feb 20, 2023

I have a problem to render PDF in Khmer language (Unicode font).

Error details
My project is work Cambodian so I want to generate PDF in Khmer language by using font Battambang (I downloaded from here: https://fonts.google.com/specimen/Battambang?subset=khmer&noto.script=Khmr). I tested the font in text editor is "Notepad" it is working properly.

Screenshot 2023-02-20 195146

But when I render from FPDF2 with Python. the position of characters is not properly. Something is not in order.

Screenshot 2023-02-18 201948

Minimal code
Here is my code to generate PDF:

from fpdf import FPDF

Code ....

    def chapter_body(self, name):
        # Reading text file:
        with open(name, "rb") as fh:
            txt = fh.read().decode("utf-8")
        # Setting font: Times 12
        self.add_font('Battambang', fname='font/Battambang-Regular.ttf')
        self.set_font("Battambang", size=12)
       
Code ....

pdf = PDF()
pdf.set_title("Book Title")
pdf.set_author("Author Name")
pdf.print_chapter(1, "Chapter 1 Title", "chapter1.txt")
pdf.print_chapter(2, "Chapter 2 Title", "chapter2.txt")
pdf.output("output.pdf")

I have downloaded the font and place it in a directory named font in the project.
I created two files in the project directory are "chapter1.txt" is in Khmer and another one is "chapter2.txt" is in English find with attachment files.
chapter1.txt
chapter2.txt

Environment

  • OS (Windows 11)
  • Python version: 3.11.1
  • fpdf2 version: 2.6.1

Result of generated PDF (output
output.pdf
.pdf)

@Lucas-C
Copy link
Member

Lucas-C commented Feb 20, 2023

Hi @ezzhop
Thank you for reporting your problem here, for providing some code reproducing your issue,
and for mentioning information about your environment & Python version.

From reingart#206:

Please take care to provide a minimal reproducing example: https://stackoverflow.com/help/minimal-reproducible-example

The code you provided is 84 lines long, it's not "minimal".
Providing minimal bug-reproducing code will help you, and people willing to help you with your issue,
to figure where the problem is, and eventually to find a solution.
This is a lot easier if you take the time to craft an example that is less than 10 lines long.
Also, if a patch is made on fpdf2 to fix the problem, this minimal code will also make for a good unit test.

Please mention of where to find the TTF font files used

I think you missed to include the link in your issue description.

Also, please understand that neither me nor other fpdf2 contributors know khmer language.
Hence, could you try to be very specific on the issue you are facing please?
I understand that some characters are not rendered correctly, but could you please answer those questions:

@eroux
Copy link

eroux commented Feb 22, 2023

this is likely a duplicate of #679 / #549 / #540 / #365

@kuth-chi
Copy link
Author

this is likely a duplicate of #679 / #549 / #540 / #365

How can I fix them?

@eroux
Copy link

eroux commented Feb 24, 2023

there seems to be a solution under development on https://github.com/andersonhc/fpdf2/tree/harfbuzz

@Lucas-C
Copy link
Member

Lucas-C commented Feb 24, 2023

@allcontributors please add @eroux for question

@allcontributors
Copy link

@Lucas-C

I've put up a pull request to add @eroux! 🎉

@kuth-chi
Copy link
Author

there seems to be a solution under development on https://github.com/andersonhc/fpdf2/tree/harfbuzz

I am trying to update library but still be a problem

@mrchoke
Copy link

mrchoke commented Mar 10, 2023

I am trying to update library but still be a problem

harfbuzz just a proof of concept.

pdf = FPDF()

pdf.add_page()
pdf.add_font('Battambang', fname='Battambang/Battambang-Regular.ttf')
pdf.set_font("Battambang", size=12)


pdf.harfbuzz_text(
    x=15,
    y=15,
    txt='មនុស្សទាំងអស់រស់នៅលើផែនដីមិនមែនគ្រាន់តែជាការសប្បាយនោះទេ តែវាជាការចូលរួមចំណែកនូវសោកនាថកម្មដ៏អស្ចារ្យ។'
)
pdf.harfbuzz_text(
    x=15,
    y=30,
    txt='ប្រុសឬ​ ស្រីកើតមកមានសេចក្តីស្រលាញ់តែមួយគត់ក្នុងពេលវេលាដូចគ្នា។'
)
pdf.harfbuzz_text(
    x=15,
    y=45,
    txt='ដោយយល់ឃើញថា ជាការចាំបាច់មិនអោយមានការដាច់ពូជមនុស្ស មនុស្សត្រូវមានមនោសញ្ចេតនា។'
)

pdf.output('khr.pdf')

Screenshot 2023-03-11 at 02 49 52

@kuth-chi
Copy link
Author

Where to define pdf.harfbuzz_text(
x=15,
y=15,
txt='មនុស្សទាំងអស់រស់នៅលើផែនដីមិនមែនគ្រាន់តែជាការសប្បាយនោះទេ តែវាជាការចូលរួមចំណែកនូវសោកនាថកម្មដ៏អស្ចារ្យ។'

The library has no attribute harfbuzz_text. 
I am so sorry about this question, because I am beginner of using this.

@mrchoke
Copy link

mrchoke commented Mar 11, 2023

Where to define pdf.harfbuzz_text(

x=15,

y=15,

txt='មនុស្សទាំងអស់រស់នៅលើផែនដីមិនមែនគ្រាន់តែជាការសប្បាយនោះទេ តែវាជាការចូលរួមចំណែកនូវសោកនាថកម្មដ៏អស្ចារ្យ។'



The library has no attribute harfbuzz_text. 

I am so sorry about this question, because I am beginner of using this.

pip install git+https://github.com/andersonhc/fpdf2.git@harfbuzz

pip install uharfbuzz

@kuth-chi
Copy link
Author

Where to define pdf.harfbuzz_text(

x=15,

y=15,

txt='មនុស្សទាំងអស់រស់នៅលើផែនដីមិនមែនគ្រាន់តែជាការសប្បាយនោះទេ តែវាជាការចូលរួមចំណែកនូវសោកនាថកម្មដ៏អស្ចារ្យ។'



The library has no attribute harfbuzz_text. 

I am so sorry about this question, because I am beginner of using this.

pip install git+https://github.com/andersonhc/fpdf2.git@harfbuzz

pip install uharfbuzz

Okay, Thank you very much. Now it is working,

@andersonhc
Copy link
Collaborator

I don't recommend using this fork in your projects unless you have a very urgent need.
I is just a test/proof of concept version and there are some problems with it. As an example, if you copy the text from the PDF and paste on an text editor it will give you invalid characters.

The definitive solution into the main pyfpdf2 library is being worked on and will be available in a few weeks.

@Lucas-C
Copy link
Member

Lucas-C commented Aug 2, 2023

@andersonhc PR #820 has been merged today.

Could you test if that solved your issue @kuth-chi?

You can install fpdf2 directly from the master branch of this repo with this command:

pip install git+https://github.com/PyFPDF/fpdf2.git@master

@kuth-chi
Copy link
Author

kuth-chi commented Aug 2, 2023

It's fixed

@Lucas-C
Copy link
Member

Lucas-C commented Aug 2, 2023

Perfect!

Thank you @kuth-chi

Closing this now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants