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

[59.0] Image caching not working with Django #1917

Closed
kahkeong opened this issue Jul 19, 2023 · 2 comments
Closed

[59.0] Image caching not working with Django #1917

kahkeong opened this issue Jul 19, 2023 · 2 comments
Labels
crash Problems preventing documents from being rendered

Comments

@kahkeong
Copy link

kahkeong commented Jul 19, 2023

Im working with Django 4.0 and using the url_fetcher parameter when building the HTML as follows:

 html = HTML(
        string=template.render(context),
        base_url=settings.STATIC_ROOT,
        url_fetcher=django_url_fetcher
    )

It is working fine with v58.1 but Im having issue with v59.0.

Below is a snippet of the trace log:

  File "/Users/keong/.pyenv/versions/hcp/lib/python3.10/site-packages/weasyprint/images.py", line 106, in draw
    image_name = stream.add_image(self, width, height, interpolate, ratio)
  File "/Users/keong/.pyenv/versions/hcp/lib/python3.10/site-packages/weasyprint/pdf/stream.py", line 383, in add_image
    xobject = image.get_xobject(width, height, interpolate)
  File "/Users/keong/.pyenv/versions/hcp/lib/python3.10/site-packages/weasyprint/images.py", line 160, in get_xobject
    pillow_image = Image.open(io.BytesIO(self.image_data.data))
  File "/Users/keong/.pyenv/versions/hcp/lib/python3.10/site-packages/weasyprint/images.py", line 235, in data
    return Path(self._filename).read_bytes()
  File "/Users/keong/.pyenv/versions/3.10.7/lib/python3.10/pathlib.py", line 1126, in read_bytes
    with self.open(mode='rb') as f:
  File "/Users/keong/.pyenv/versions/3.10.7/lib/python3.10/pathlib.py", line 1119, in open
    return self._accessor.open(self, mode, buffering, encoding, errors,
FileNotFoundError: [Errno 2] No such file or directory: '/media/logos/abcd.png'

After some debugging, I found out the code that is responsible for caching the files is using relative path instead of absolute path.

I managed to get it work locally by adding an extra line right above L315
which combines the absolute path of my Django project with the relative file path.
filename = absolute_path_to_the_project + filename

@liZe liZe added the crash Problems preventing documents from being rendered label Jul 19, 2023
@liZe
Copy link
Member

liZe commented Aug 20, 2023

Hi!

Do you use django-weasyprint? If so, the problem is probably in the django_url_fetcher function, and you should open an issue in django-weasyprint.

@kahkeong
Copy link
Author

yes it is indeed an issue with django-weasyprint, django-weasyprint 2.2.0 resolved my issue, thank you !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
crash Problems preventing documents from being rendered
Projects
None yet
Development

No branches or pull requests

2 participants