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

How to convert HTML style tags inside fpdf paragraphs? #498

Closed
me-suzy opened this issue Aug 20, 2022 · 12 comments
Closed

How to convert HTML style tags inside fpdf paragraphs? #498

me-suzy opened this issue Aug 20, 2022 · 12 comments
Assignees

Comments

@me-suzy
Copy link

me-suzy commented Aug 20, 2022

Hi!

I have several HTML tags inside the paragraphs I'm writing to the pdf file.
Those HTML tags contain some style I want to apply to the text I am writing.
Let's consider this example:

My name is Emily. <em>Hello world!<em> My name is Jon. (in pdf file)

Normally, on the HTML page this text is displayed like this (<em> tags transform the text into italic):

My name is Emily. Hello world! My name is Jon. (on HTML page)

Is there any way to annotate the text, such that it will be displayed italic in the pdf, instead of ?

image

So, basicaly, I need to change the second style contained in a HTML tag. For example:

<p class="soft">I love design very much because it represent <em>the month december</em> that its winter time.</p>

must be visible in adobe as:

I love design very much because it represent the month december that its winter time.

@Lucas-C
Copy link
Member

Lucas-C commented Aug 22, 2022

Hi @me-suzy
You have spotted an actual bug of fpdf2, thank you for reporting it.
I'm working on a fix

@Lucas-C
Copy link
Member

Lucas-C commented Aug 22, 2022

@all-contributors please add @me-suzy for bug

@allcontributors
Copy link

@Lucas-C

I've put up a pull request to add @me-suzy! 🎉

@Lucas-C
Copy link
Member

Lucas-C commented Aug 22, 2022

PR #499 fixed this

You can test it by installing the latest fpdf2 version from git:

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

@Lucas-C
Copy link
Member

Lucas-C commented Aug 22, 2022

This has been broken since at least v2.2.0 of fpdf2:
https://github.com/PyFPDF/fpdf2/blob/2.2.0/test/html/test_html_features.pdf
(you can compare the 3rd line of the PDF with the latest reference file: https://github.com/PyFPDF/fpdf2/blob/master/test/html/html_features.pdf - Python test source)

@me-suzy
Copy link
Author

me-suzy commented Aug 23, 2022

Also, I have already installed GIT:

pip install github.py

But when I try to install your extension , and I got this error:

image

@Lucas-C
Copy link
Member

Lucas-C commented Aug 23, 2022

@me-suzy: no need to install github.py! This is a totally unrelated Pypi package: https://pypi.org/project/github.py/

You need to have the git command available, that is correct, but then just run this:

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

This is the syntax to install a Python package from a git repository.
Full documentation is available here: https://pip.pypa.io/en/stable/topics/vcs-support/

@me-suzy
Copy link
Author

me-suzy commented Aug 25, 2022

In the error it says that you have not installed git.
You need to install git.

https://phoenixnap.com/kb/how-to-install-git-windows

You do these steps to Launch Git GUI (exclusive).

Open Git Bash.

Then go to the Configure GitHub Credentials step and run the 2 commands with your GitHub account data.

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

After doing that you run the command given by those guys.

And then you run the PDF converter.

@me-suzy
Copy link
Author

me-suzy commented Aug 25, 2022

Yes, I installed everything successfully, but it still looks the same.

This is the html paragraph with <p class="text_obisnuit2"></p> and inside this tag I have other 2 italic tags <em></em>

The basic tag doesn't see in PDF, very good. But the italic tag <em></em> is seen in PDF, and this is not good.

<p class="text_obisnuit2"><em>Pentru a cunoa&#351;te realitatea un lider trebuie s&#259; de&#355;in&#259; &#351;i arta disimul&#259;rii &ndash; o arm&#259; de temut, dar eficient&#259; &icirc;n cele mai multe situa&#355;ii.</em></p>

See the PDF after running Python code:

image

@Lucas-C
Copy link
Member

Lucas-C commented Aug 25, 2022

The following code works perfectly fine for me with the latest version of fpdf2:

from fpdf import fpdf, html

class PDF(fpdf.FPDF, html.HTMLMixin):
    pass

pdf = PDF()
pdf.add_page()
pdf.add_font("Kanit", fname="fonts/Kanit-Regular.ttf")
pdf.add_font("Kanit", style="I", fname="fonts/Kanit-Italic.ttf")
pdf.set_font("Kanit", size=24)
pdf.write_html('<p class="text_obisnuit2"><em>Pentru a cunoa&#351;te realitatea un lider trebuie s&#259; de&#355;in&#259; &#351;i arta disimul&#259;rii &ndash; o arm&#259; de temut, dar eficient&#259; &icirc;n cele mai multe situa&#355;ii.</em></p>')
pdf.output("issue_498.pdf")

The Kanit font is a Google font: https://fonts.google.com/specimen/Kanit

The resulting PDF contains text in italics: issue_498.pdf

If you have further issues, please share some fully-autonomous minimal reproducible example so that we can replicate your problem.

@me-suzy
Copy link
Author

me-suzy commented Aug 26, 2022

I copy/paste my complete code on the link below. I also write the errors that I encountered

#505 (comment)

@Lucas-C
Copy link
Member

Lucas-C commented Sep 8, 2022

The fix from PR #499 has been released in v2.5.7

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

2 participants