Kozea / WeasyPrint

The awesome document factory
https://weasyprint.org
BSD 3-Clause "New" or "Revised" License
7.19k stars 680 forks source link

Inherited style attributes from parent element is not being applied. #2244

Closed codehunter1992 closed 1 month ago

codehunter1992 commented 2 months ago

Inherited style attributes from parent element is being ignored when builds PDF.

For example,

<span style="font-family: 'comic sans ms', sans-serif'; font-size: 16px;">
    Hello world, welcome to <strong>WeasyPrint</strong>.
</span>

Word "WeasyPrint" in rendered PDF, has no correct font family and size.

Is there any solution?

liZe commented 2 months ago

Hi!

Do you have bold Comic Sans installed on your system?

codehunter1992 commented 2 months ago

Yes, since if I apply bold to entire sentence like <strong><span style="font-family: 'comic sans ms', sans-serif'; font-size: 16px;">Hello world, welcome to WeasyPrint</span></strong>, it works properly.

liZe commented 2 months ago

Could you please share your PDF with the wrong font?

codehunter1992 commented 2 months ago

1725022933811.pdf This is the PDF file. The words Lorem Ipsum, dummy text (read color), Lorem Ipsum (underline) are all should have same font family and size.

liZe commented 2 months ago

Your original example works for me (when I remove the extra quote).

ms.pdf

Your PDF seems to be rendered using an old version of WeasyPrint. Could you try a recent version?

codehunter1992 commented 2 months ago

I upgraded Weasyprint version 62.3

This is my html code:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
</head>
<body>
   <p><span style="font-family: 'comic sans ms', sans-serif; font-size: 18pt;">Hello world, welcome to <strong>WeasyPrint</strong>.</span></p>
 <body>
</html>

This is generated PDF file using the latest version of package. be2d30e4-fbe2-44e2-ac04-9e55cab1de6e.pdf

I also tried to test without html wrapper, same issue.

liZe commented 2 months ago

It works for me. I really do think that you don’t have the bold font installed, it was automatically generated with older versions of WeasyPrint, but it’s not anymore (#1470).

What’s your OS, and how did you install Comic Sans?

codehunter1992 commented 2 months ago

I am using MacOS 14.3.1. I didn't install the font myself, I just guessed that the font was installed because below html works properly.

<strong><span style="font-family: 'comic sans ms', sans-serif'; font-size: 16px;">Hello world, welcome to WeasyPrint</span></strong>
liZe commented 2 months ago

Could you please share the PDF generated with this HTML?

codehunter1992 commented 2 months ago

Here you go the PDF file. 1725025244839.pdf

liZe commented 2 months ago

Well, it’s installed, and the documentation says it’s installed by default. To be honest, I have no idea why it doesn’t work for you. Do you have the same problem with other fonts? You can try with Futura or Didot, for example.

codehunter1992 commented 2 months ago

Same issue with another font. For example: <p><span style="font-family: tahoma, arial, helvetica, sans-serif; font-size: 18pt;">Hello world, welcome to <strong>WeasyPrint</strong>.</span></p>

<p><span style="font-family: arial, helvetica, sans-serif; font-size: 18pt;">Hello world, welcome to <strong>WeasyPrint</strong>.</span></p>

All didn't work. :(

codehunter1992 commented 2 months ago

Well, it’s installed, and the documentation says it’s installed by default. To be honest, I have no idea why it doesn’t work for you. Do you have the same problem with other fonts? You can try with Futura or Didot, for example.

You said "it doesn't work for you". Does this mean it works from your side? If so, what is your Weasyprint version and OS? Can I know?

liZe commented 2 months ago

Does this mean it works from your side?

Yes, it does work for me.

ms.pdf

If so, what is your Weasyprint version and OS? Can I know?

I use version 62.3 on Linux.

Did you install WeasyPrint with Homebrew?

codehunter1992 commented 2 months ago

No, I installed using pip and running it in python project. What is your version number of Linux? Since I run a server on Ubuntu 20 and got same issue on it.

liZe commented 2 months ago

It works for me on latest Gentoo and Ubuntu computers. I’ve never seen this problem before, on countless Linux distributions. And it’s never been reported, as far as I can remember.

Since I run a server on Ubuntu 20 and got same issue on it.

It’s really unlikely this error could happen on a fresh Ubuntu 20.04 install, or we would have spotted this before. Is there any specific configuration (maybe a Fontconfig configuration) you may have on both computers?

No, I installed using pip and running it in python project.

On MacOS, how did you install the non-Python dependencies, such as Pango and Fontconfig?

codehunter1992 commented 2 months ago

I have no any specific configuration regarding it.

I didn't install non-python dependencies such as Pango and Fontfonfig manually from MacOS. Nothing let me install them manually.

On Ubuntu, I installed Weasyprint 52.5 because of the dependencies' version conflicts.

liZe commented 2 months ago

I didn't install non-python dependencies such as Pango and Fontfonfig manually from MacOS. Nothing let me install them manually.

Then they were already installed by another application, maybe with older broken versions.

The only documented way to make WeasyPrint work is to use Homebrew, and unfortunately we can’t help you much if you used anything else. You should try with Homebrew, as it’s known to work.

On Ubuntu, I installed Weasyprint 52.5 because of the dependencies' version conflicts.

Just to be sure, could you please write ms.html with

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
</head>
<body>
   <p><span style="font-family: 'comic sans ms', sans-serif; font-size: 18pt;">Hello world, welcome to <strong>WeasyPrint</strong>.</span></p>
 <body>
</html>

then try:

sudo apt install weasyprint
weasyprint --info
weasyprint ms.html ms.pdf

and share your PDF?

codehunter1992 commented 2 months ago

I did tested on MacOS with this command line - weasyprint ms.html ms.pdf (not in python project) and it generates PDF file properly without any issue.

html = HTML(string=html_string, encoding="utf-8")
page_formatter = CSS(string="@page { size: A4; margin: 0.75in }", encoding="utf-8")
page_stylesheet = CSS(filename=f"{MEDIA_ROOT}assets/document_template/style.css")
html.write_pdf(
    MEDIA_ROOT + self.file_path, stylesheets=[page_formatter, page_stylesheet], presentational_hints=True
)

These are my python code to generate PDF from html string. Any code line may occur such issue do you think?

liZe commented 2 months ago

I did tested on MacOS with this command line - weasyprint ms.html ms.pdf (not in python project) and it generates PDF file properly without any issue.

:+1:

Any code line may occur such issue do you think?

It’s hard to tell without the whole code, but maybe using a shared FontConfiguration will help?

codehunter1992 commented 2 months ago

On Ubuntu - by running sudo apt install weasyprint I got this.

Reading package lists... Done
Building dependency tree       
Reading state information... Done
E: Unable to locate package weasyprint

(Sorry, I just noticed that I run Ubuntu server OS 18.04)

By running weasyprint --info returns me weasyprint: command not found

After activate python venv, it returns me

System: Linux
Machine: x86_64
Version: #115~18.04.1-Ubuntu SMP Mon May 22 20:06:37 UTC 2023
Release: 5.4.0-1109-azure

WeasyPrint version: 52.5
Python version: 3.9.12
Cairo version: 11510
Pango version: 14014
CairoSVG version: 2.7.1

weasyprint ms.html ms.pdf generates a pdf file, but font doesn't work in entire html code. ms.pdf

liZe commented 2 months ago

Version: #115~18.04.1-Ubuntu

That’s Ubuntu 18.04, not 20.04. WeasyPrint is not packaged for 18.04, let’s test with venv then.

weasyprint ms.html ms.pdf generates a pdf file, but font doesn't work in entire html code.

You have to install Comic Sans MS. Should work with sudo apt install ttf-mscorefonts-installer.

codehunter1992 commented 2 months ago

Yes, after install ttf-mscorefonts-installer, weasyprint ms.html ms.pdf generates PDF correctly. Thanks. But I need to make python project work :) Any other more suggestions?

liZe commented 2 months ago

Any other more suggestions?

Did you try the shared FontConfiguration as explained above?

Otherwise, it means that something specific to your project breaks WeasyPrint. You probably know better what your project does, so we can’t really help you about that :/.

codehunter1992 commented 2 months ago

OK, Thanks, I will try more myself, and keep posted here. You can also update once you get a new idea.

liZe commented 2 months ago

You can also update once you get a new idea.

I really don’t know what can cause this issue.

liZe commented 1 month ago

@codehunter1992 Did you find anything interesting about this issue?

codehunter1992 commented 1 month ago

It was a silly issue. There was a bad line in the css file that I used to generate to PDF. After remove that line from css, it works properly. Thank you for your attention.