question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add CSS outside WebContext

See original GitHub issue

Hi,

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:closed
  • Created 5 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
danielfernandezcommented, Mar 23, 2019

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.

1reaction
ultraqcommented, Sep 6, 2018

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 standard href 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 the href with that address, eg: href="http://localhost:8080/static/css/CDM.css"

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found