Add CSS outside WebContext
See original GitHub issueHi,
I’m using thymeleaf to generate PDF in background processes (batch) using Spring Boot 2…
To add some style to my PDF’s I would like to add some CSS using this.
<link rel="stylesheet" href="static/css/CDM.css" th:href="@{/static/css/CDM.css}"/>
Then I got the following exception :
org.thymeleaf.exceptions.TemplateProcessingException: Link base "/static/css/CDM.css" cannot be context relative (/...) unless the context used for executing the engine implements the org.thymeleaf.context.IWebContext interface
I’ve seen this issue on github but I don’t get how I could get a WebContext in a batch process?
How could I load my CSS files so that they are applied in my PDF?
I’ve seen this issue. But I don’t get how to make it work outside a WebContext…
Issue Analytics
- State:
- Created 5 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Spring Boot + Thymeleaf css is not applied to template
Unlike web context, in our case, offline template is on server backend, so the context ... It might help you to apply css...
Read more >Tutorial: Using Thymeleaf
Out-of -the-box, Thymeleaf allows you to process six kinds of templates, each of which is called a Template Mode: HTML; XML; TEXT; JAVASCRIPT ......
Read more >Guidelines for Creating Plug-Ins Compatible with the vSphere ...
Add Cascading Style Sheets (CSS) classes to the plugin-icons.css file for the icons that are displayed outside the views, such as Home screen...
Read more >16.3. Using symfony outside of a web context - Uniwebsidad
Using symfony outside of a web context. You may want to execute a script from the command line (or via a cron table)...
Read more >A Comparison of Three Methods for Styling Components in ...
Stylesheets and the Style Prop. The place to start is going to be the “out-of-the-box” solution for styling components in React Native, ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Very late for a response, but the thing is, a context-relative URL can only be resolved when the template is being executed as a part of a web application running as such in a web server… because it’s web servers who create web contexts, that serve as a root for such URLs.
So this means your template needs to be executed by means of the normal Spring Boot web mechanisms (i.e. as a Spring view) in order to get to execute that kind of links. As opposed to, for instance, having your own
TemplateEngine
instance and executing it on your own.However, most PDF-producing mechanism I know actually do this: start a web server, make it render a page, perform an HTTP request, and then render the result into the PDF… and this rendering phase is needed precisely because you are applying styles, so maybe you should be setting a separate web server for your PDF exporter?
Given this is a quite old ticket and you mentioned you already had a workaround I will be closing it now. But plese don’t hesitate opening if there is anything else you needed to add.
Hi @schtrym, sorry for not getting back to you sooner, but is this still an issue for you? If so, the problem you’re running up against is that you can’t use
@{...}
expressions outside of a web context because that particular expression looks up some information on the running web server to fill out things like the scheme, protocol, domain, port, etc… These, obviously, don’t exist in a batch process.However, do you know where your CSS files will live relative to the output template that you’re creating? If so, you can drop the
th:href="@{...}"
and instead fill in the standardhref
attribute to contain the relative path to the CSS file, eg:href="../static/css/CSM.css"
. Or if your CSS file lives on a web server that you know the address of, you can fill in thehref
with that address, eg:href="http://localhost:8080/static/css/CDM.css"