Skip to content

Supply url_fetcher to weasyprint to support /media and /static files #9394

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

LqdBcnAtWork
Copy link

Pull request with changes from a comment made on #9351.

This allows weasyprint to grab files from /media and /static locations by redirecting them to open() calls with respect to MEDIA_ROOT and STATIC_ROOT settings.

Probably could be made better. But at least ensures that the resulting path is always still a child of the parent folder.

Copy link

netlify bot commented Mar 26, 2025

Deploy Preview for inventree-web-pui-preview canceled.

Name Link
🔨 Latest commit c55c53a
🔍 Latest deploy log https://app.netlify.com/sites/inventree-web-pui-preview/deploys/67e459500d4d47000802ac1b

@@ -42,6 +44,31 @@

logger = structlog.getLogger('inventree')

WE_BASE_URL = 'http://localhost'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An issue here is that the request URL may or may not exactly match the server's internal URL, depending on proxying settings. Adding a hard-coded URL here is not a good idea.

@LqdBcnAtWork
Copy link
Author

Just a clarifying question, would it be possible to use these somehow with markdownify?

That was the "problem" this code was meant to solve. The notes editor uses relative paths (eg: /media/notes/image.png) for the SRC attribute. Which markdownify uses without transformation.

This pull request makes it so weasyprint can grab those images as well for pdf generation. Currently images aren't included when notes are transformed into html by markdownify, as weasyprint has no method of getting the assets. It actually throws an error as there are relative paths with no base path provided.

@SchrodingersGat
Copy link
Member

@LqdBcnAtWork yeah I definitely appreciate what you are trying to achieve here. Having another read through, this might not be a terrible idea ;)

  • You upload an image into the notes for a part, which then loads an image against /media/images/my_image.png
  • When you try to render the same markdown into HTML (for weasyprint report rendering) it fails because it doesn't know how to access the image
  • Add a custom fetcher to weasyprint which supports just /media/ and /static/ requests
  • This works not just for markdown code, but any HTML we want to render into the report

So, there are still some issues to deal with here:

  1. Remove the hard-coded URL prefix - should use the defined site_url
  2. Can we support lookup of assets without a HOST prefix e.g. /media/images/my_image.png vs http://server.com/media/images/my_image.png
  3. We need to be able to support other types of django storage backends - not just filesystem storage

@matmair
Copy link
Contributor

matmair commented Apr 1, 2025

Let us know if you want pointers how the requested changes might be achieved

@SchrodingersGat
Copy link
Member

@LqdBcnAtWork are you still looking into this?

@LqdBcnAtWork
Copy link
Author

My apologies, I am still planning on doing more on this. But I've been pulled aside to other projects for the time being.

I'll get back to this eventually. It's becoming a conversation of when, not if, we'll switch to Inventree.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants