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

Reproducible PDF creation breaks when you add a background-image to your CSS #1666

Closed
DutchGerman opened this issue Jun 29, 2022 · 1 comment
Labels
bug Existing features not working as expected
Milestone

Comments

@DutchGerman
Copy link

In build v55.0b1 a feature to generate reproducible PDFs was added.

SOURCE_DATE_EPOCH is set to 0

With the following python code

  from weasyprint import HTML, CSS
  import hashlib

  html = """
    <html>
      <head>
        <title>Hello World</title>
        <meta name="dcterms.created" content="2002-01-01">
      </head>
      <body>
        <p>Foobar</p>
      </body>
    </html>
  """
  
  css = """
    body {
      color: #515151;
    }
  """
  
  pdf = HTML(string=html).write_pdf(stylesheets=[CSS(string=css)])
  md5 = hashlib.md5(pdf).hexdigest()
  print(md5)

It runs correctly and outputs the hash 92ae45e4db7a51b1941cd98e8b4c1c8b consistently

But when I add a background-image to the body like so:

  from weasyprint import HTML, CSS
  import hashlib

  html = """
    <html>
      <head>
        <title>Hello World</title>
        <meta name="dcterms.created" content="2002-01-01">
      </head>
      <body>
        <p>Foobar</p>
      </body>
    </html>
  """
  
  css = """
    body {
      color: #515151;
      background-image: url("https://i.imgur.com/o3v4JUp.png");
    }
  """
  
  pdf = HTML(string=html).write_pdf(stylesheets=[CSS(string=css)])
  md5 = hashlib.md5(pdf).hexdigest()
  print(md5)

The output changes with every pass.

1#: bc1c85fdaa77406c185dcab8a94e0602
2#: 88f96791669a895f2a4ff7315613d5d1
3#: c6cc5cf67671110b9c4b64d83f16d263

I also tried to add a base64 background like so:

  css = """
    body {
      color: #515151;
      background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAAMSURBVBhXY/j//z8ABf4C/qc1gYQAAAAASUVORK5CYII=')
    }
  """

Which results it the same behavior

@liZe liZe added the bug Existing features not working as expected label Jun 29, 2022
@liZe liZe added this to the 56.0 milestone Jun 29, 2022
@liZe liZe closed this as completed in 00d5b03 Jun 29, 2022
@liZe
Copy link
Member

liZe commented Jun 29, 2022

Hi, and thanks a lot for this bug report!

The problem comes from the hash() function that’s not stable. Using md5 instead works well.

Unfortunately, I didn’t find a nice way to test this. If anyone has a good idea, don’t hesitate to open a PR!

netbsd-srcmastr referenced this issue in NetBSD/pkgsrc Jul 9, 2022
Version 56.0
------------

Released on 2022-07-07.

This version also includes the changes from unstable b1 version listed
below.

New features:

* `70f9b62 <https://github.com/Kozea/WeasyPrint/commit/70f9b62>`_:
  Support format 5 for bitmap glyphs

Bug fixes:

* `1666 <https://github.com/Kozea/WeasyPrint/issues/1666>`_
  Fix reproducible PDF generation with embedded images
* `1668 <https://github.com/Kozea/WeasyPrint/issues/1668>`_:
  Fix @page:nth() selector
* `3bd9a8e <https://github.com/Kozea/WeasyPrint/commit/3bd9a8e>`_:
  Don’t limit the opacity groups to the original box size
* `cb9540b <https://github.com/Kozea/WeasyPrint/commit/cb9540b>`_,
  `76d174f <https://github.com/Kozea/WeasyPrint/commit/76d174f>`_,
  `9ce6547 <https://github.com/Kozea/WeasyPrint/commit/9ce6547>`_:
  Minor bugfixes for split table rows
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

2 participants