Static files are not loaded generating pdf from html when application run in ASGI layer

** I use Weasyprint to generate pdf from HTML. When running the application in ASGI static files are not loaded in pdf, but if I run the application in WSGI then the static file loads correctly. How can I solve this issue?**

This part is generating pdf.

HTML(string=html_string,base_url=self.request.build_absolute_uri()).write_pdf(response,stylesheets=[css],presentational_hints=True,font_config=font_config)

asgi.py

application = ProtocolTypeRouter({
    "http": application,
    "websocket": AuthMiddlewareStack(
        URLRouter(
            notification.routing.websocket_urlpatterns
    ),
        ),
        
    # Just HTTP for now. (We can add other protocols later.)
})

routing.py

websocket_urlpatterns = [
    re_path(r'ws/notification/(?P<user_id>\w+)/$', consumers.NotificationConsumer.as_asgi()),
    #re_path(r'ws/notification-main/(?P<user_id>\w+)/$', consumers.NotificationConsumerMain.as_asgi()),
    re_path(r'ws/loginstatus/(?P<token>\w+)/$', consumers.LoginStatus.as_asgi()),
    #path('ws/notification/<user_id>/', consumers.NotificationConsumer.as_asgi()),
]

The only way I can think of is to figure out how the data flows through this process.

I’d be trying to find every spot along the way where data is accessed or retrieved and try to determine what’s working or not.

For example -

What does the view look like that is generating your PDF?

Have you verified that the html is complete and correct at the point where you’re trying to generate the PDF?

Since you’re rendering HTML, are the static files being specified as img tags in that html? Does Weasyprint try to access those files directly or does it issue requests to retrieve them?

Hopefully this gives you some ideas of how you might be able to approach this.

1 Like

Thanks. I fixed it, I need to write a program to open a valid URL in weasyprint.