Kozea / WeasyPrint

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

Problem with specifying width of img #2283

Closed tobiasgardner closed 3 weeks ago

tobiasgardner commented 3 weeks ago

I am having problems with converting html to pdf with weasyprint (python).

It seems like it is not adhering to "width" in the img tag:

<img src="..." alt="Logo" width="300" />

No matter what I try here, it takes up 100% of the width of the page in the pdf output. However, it works when looking at the html code through a browser.

I have tried

I have created a html file here which illustrates the problem: https://gist.github.com/tobiasgardner/be6e9efba80f13e6aa6464cbcf8613ad

The output in the browser (html) looks like this. Image

The generated pdf can be found here: Weasyprint_debug.pdf

liZe commented 3 weeks ago

Hi!

You need to add the --presentational-hints option to handle the width HTML attribute.

Note that using CSS instead of HTML attributes is often preferred for styling information.

tobiasgardner commented 3 weeks ago

Thank you @liZe! That did the trick! Or in my case, I used the following:

HTML(string=html_content).write_pdf(target=filename, presentational_hints=True)

tobiasgardner commented 3 weeks ago

@liZe May I ask another question... I am trying to setup the CSS so that my pages have:

I have tried a bunch of different ways but I am failing. If you have a guide/tutorial which shows how this can be done, I would appreciate it a lot :)

liZe commented 3 weeks ago

I have tried a bunch of different ways but I am failing. If you have a guide/tutorial which shows how this can be done, I would appreciate it a lot :)

You can take a look at our samples, the "Reports" one should be useful.