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

Wrong characters widths when pages are built separately #1489

Closed
wf-r opened this issue Nov 8, 2021 · 2 comments
Closed

Wrong characters widths when pages are built separately #1489

wf-r opened this issue Nov 8, 2021 · 2 comments
Labels
bug Existing features not working as expected
Milestone

Comments

@wf-r
Copy link

wf-r commented Nov 8, 2021

Hi,

(this is a separate issue for the comment (#628 (comment)) given in #628).

On Version 53.3 I get unexpected horizontal whitespaces using the following code, which I cannot reproduce on 52.5:

import jinja2
import weasyprint

pdf_pages = weasyprint.HTML(
    string=jinja2.Environment(
        loader=jinja2.FileSystemLoader(searchpath='.')
    ).get_template(
        'example.html'
    ).render(
        ueberschrift='Lw, UmP, TrD',
    ),
    base_url='.'
).render().pages
pdf_pages.extend(
    weasyprint.HTML(
        string=jinja2.Environment(
            loader=jinja2.FileSystemLoader(searchpath='.')
        ).get_template(
            'example.html'
        ).render(
            ueberschrift='Bw, Offz, GeoInfoD',
        ),
        base_url='.'
    ).render().pages
)

with open('out.pdf', 'w+b') as out:
    weasyprint.Document(
        pages=pdf_pages,
        metadata=weasyprint.document.DocumentMetadata(),
        # font_config=weasyprint.text.fonts.FontConfiguration(),  # for 53.3
        font_config=weasyprint.document.FontConfiguration(),  # for 52.5
        url_fetcher=None,
        # optimize_size='none'  # for 53.3
    ).write_pdf(out)

The two respective documents are attached, on 53.3 I get unexpected horizontal whitespaces, on 52.5 it works fine. However, the behaviour is pretty random:

  • if i render only one page in the snippet above, everything is fine
  • other strings render fine

out-52_5.pdf
out-53_3.pdf

System:

  • Weasyprint: 53.3 and 52.5
  • Python 3.8.6
  • Windows 10 64bit Build 18363
  • Pango 1.48.4

If you would like to investigate and need further information please let me know.

Best
Wolfgang

Template used in snippet:

<!DOCTYPE html>
<html>
	<head>

		<!-- CSS property to place div
			side by side -->
		<style>
			@page{
				size: A4 landscape;
				margin: 0px;
				padding: 0px;
			}
			html {
				height: 100%;
			}
			body {
				height: 100%;
				display: flex;
				flex-direction: column;
			}
			h1{
				text-align:center; 
			}
			#boxes{
				flex-grow: 1;
				display: flex;
				flex-direction: row;
			}
			.box {
			height: 100%;
				width: 50%;
				margin-left: 20px;
				margin-right: 20px;
				background: #C0C0C0;
				padding-left: 20px;
				padding-right: 20px;
				padding-bottom: 20px;
				border-radius: 30px;
			}
		</style>
	</head>

	<body>
		<h1>{{ ueberschrift }}</h1>
		<div id="boxes">
			<div class="box">
				Some random tetx
            		</div>
			<div class="box">
				Some more random text
        	       </div>
		</div>
	</body>
</html>					 
@trevmono
Copy link

I have experienced exactly the same. I have narrowed it down through trial and error. The last page (or pages in the last render added to the document) always works correctly but every previous pages has unexplained padding or margin between some characters. If I delete the last page from the document before doing write_pdf, the new last page now gets the correct layout and the others before still don't.

If the last item added to the document is multiple pages itself, all of its pages render correctly. For example, if one of the render objects has 10 pages and it is at the end of the combined document, those 10 pages will all work. If I put another single page at the end then all of the pages now don't work.

I am also having problems rendering any italic styles.

So something is wrong with write_pdf and fonts. I noticed there were font changes in the last beta, so the reason will be in there somewhere. I'm not smart enough to work it out myself, but I've narrowed it down for others.

@liZe liZe added the bug Existing features not working as expected label Nov 10, 2021
@liZe liZe added this to the 53.4 milestone Nov 10, 2021
@liZe liZe changed the title Unexpected horizontal whitespace in 53.3 Wrong characters widths when pages are built separately Nov 10, 2021
@liZe
Copy link
Member

liZe commented Nov 10, 2021

Thanks for the bug report.

This problem is caused by the way we handle fonts. Everything works well when the pages come from the same document, but it’s broken when they’re built separately.

@liZe liZe closed this as completed in 43d0e54 Nov 13, 2021
liZe added a commit that referenced this issue Nov 13, 2021
Using the font hash avoids collisions when we have the same font description
but not the same hash, for example when the same font is used in multiple HTML
instances.

It could be possible to only store the font once, this solution is not optimal
but this more reliable.

Fix #1489.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Existing features not working as expected
Projects
None yet
Development

No branches or pull requests

3 participants