External CSS in Flying Saucer
Asked Answered
E

6

7

I would like to know how to include the External CSS in Flying-Saucer.Before that THB I checked with all the available links in StackOverflow but they are not helpful.That's the reason why made this one my self.

TestCSS.xhtml renamed version of TestCSS.html.So content of them are same. Below (Image 1 ) is the Structure of my Project in Eclipse IDE.If I run the TestCSS.html it will give the page result as Image 2 in Browser.

Below are Code which are not working as External CSS :

This one Working :
<style>
.redFontClass
{
  color : red;
}
.blueFontClass
{
  color : blue;
}
</style>

This one NOT Working :
<link href="RedCSS.css" rel="stylesheet" type="text/css" />

This one NOT Working :
<link rel="stylesheet" 
href="http://localhost:8888/Fly-Sauccer-Web/css/RedCSS.css" type="text/css" />

This one NOT Working :
<link href="file:///C:/Users/Joseph.M/WorkPlace_Struts2/Fly-Sauccer-Web/WebContent/css/RedCSS.css"  rel="stylesheet" type="text/css" />

I tried with all the ways including absolute path of css inside of the xhtml also.but css is not getting applied.Please help me to fix the problem.

Image 1

enter image description here

Image 2

enter image description here

RedCSS.css

.fontClass
{
  color : red;
}

TestCSS.html

<html>
<head>
<link href="file:///C:/Users/Joseph.M/WorkPlace_Struts2/Fly-Sauccer-Web/WebContent/css/RedCSS.css"  rel="stylesheet" type="text/css" />
</head>
<body>
<b>This Should come assss <span class = "fontClass" >Red</span> </b>
</body>
</html>

Java Code :

public static void main(String[] args) throws Exception{

    // Path of Input File 
    String inputFile = "C:\\Users\\Joseph.M\\WorkPlace_Struts2\\Fly-Sauccer-Web\\WebContent\\TestCSS.xhtml";
    // Path of Output File 
    String outputFile = "C:\\Users\\Joseph.M\\WorkPlace_Struts2\\Fly-Sauccer-Web\\output.pdf";
    OutputStream os = new FileOutputStream(outputFile);             
    ITextRenderer renderer = new ITextRenderer();

    DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    InputStream is = new ByteArrayInputStream(FileUtils.readFileToByteArray(new File(inputFile)));
    Document doc = builder.parse(is);
    is.close();
    renderer.setDocument(doc,null);        
    renderer.layout();
    renderer.createPDF(os);             
    os.close();
}
Elimination answered 23/9, 2014 at 8:55 Comment(4)
Hey have you checked if the css is loaded properly? If yes you could do color:red !important;.Moidore
Please explain, what you mean by "css is loaded".How could I check css is loaded or not in java .That is possible in Web alone.there I didn't face any problem.Proble with PDF generation in Java Code.Elimination
Looking at your project structure, shouldn't the href be css/RedCSS.css?Credulity
Thanks for reply.I tried that too but no use.Elimination
Q
0

Given the structure of the project, <link href="css/RedCSS.css" rel="stylesheet" type="text/css" /> should definitely work.

Here is a working sample :

File struct :

enter image description here

File 1 : testRed.html

<html>
<head>
    <link href="css/testRed.css" rel="stylesheet" type="text/css" />
</head>
<body>
    Should be <b class="redFontClass">red</b>
</body>
</html>

File 2 : css/testRed.css

.redFontClass {color : red;}

Java code :

  String inputFile = "testRed.html";
  String outputFile = "testRed.pdf";
  OutputStream os = new FileOutputStream(outputFile);
  ITextRenderer renderer = new ITextRenderer();

  DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
  InputStream is = new ByteArrayInputStream(FileUtils.readFileToByteArray(new File(inputFile)));
  Document doc = builder.parse(is);
  is.close();
  renderer.setDocument(doc, null);
  renderer.layout();
  renderer.createPDF(os);
  os.close();
Quinn answered 29/9, 2014 at 11:10 Comment(4)
Thanks for reply,but it seems not working.Could you give me folder structure for all the files you mentioned ?Elimination
I added a screenshot of my java project in the response.Quinn
It is not working for me. could you give me your email id ? I will send the zip file where you can find error.Elimination
You'd better upload it on a public file server and add the link in the question.Quinn
I
0

I tried an similar thing on my localmachine and i tried it with the solution of obourgain <link href="css/testRed.css" rel="stylesheet" type="text/css" /> The response for this way was 302 which means that the resource was found with redirecting but the result of this get was empty. When i add a / before css all works fine. <link href="/css/testRed.css" rel="stylesheet" type="text/css" />

Infliction answered 2/10, 2014 at 8:43 Comment(0)
G
0

I use the "classpath" keyword in a Spring Boot environment.

<link rel="stylesheet" type="text/css" media="all" th:href="@{classpath:templates/style.css}"/>

worked for me.

I hope this helps someone with a Spring Boot + Thymeleaf + Flying Saucer setup.

Gastroenterology answered 7/11, 2018 at 21:23 Comment(2)
Please explain why it worked for you, and why it should work for others. Random coincidence is not good enough reason to exhume a post from 4 years ago.Fregger
it is not working for Spring Boot + Thymeleaf + Flying Saucer setup. run time error: "org.thymeleaf.exceptions.TemplateProcessingException: Link base "classpath:css/pdfexport.css" cannot be context relative (/) or page relative unless you implement the org.thymeleaf.context.IWebContext interface (context is of class: org.thymeleaf.context.Context) (offerDetailsPDFTemplate:5) "Stanleystanly
S
0

Recently I faced a similar problem: it turned out that the CSS file was in a different encoding. You may have the same problem. First, you need to find out the encoding of the file:

file -i <my css file>

And then convert it into UTF-8:

iconv -f <my css file enconding> -t UTF-8 <my css file> > <my css file in utf-8>

In my case, after converting from UTF-16 to UTF-8, a PDF file has been generated.

Somali answered 12/10, 2020 at 8:51 Comment(0)
P
0

Please refer this link. https://web.archive.org/web/20150905173204/http://today.java.net/pub/a/today/2007/06/26/generating-pdfs-with-flying-saucer-and-itext.html

Its mentioned that when you want to use an external css file, you should have a media="print" attribute on the link tag.

link href="file:///C:/Users/Joseph.M/WorkPlace_Struts2/Fly-Sauccer-Web/WebContent/css/RedCSS.css" rel="stylesheet" type="text/css" media="print"

Pion answered 8/12, 2020 at 2:19 Comment(0)
J
0

For me, I just did exactly what Wolf359 did:

A lil bit of detail, this is my html template:

<link th:href="@{classpath:/css/styles.css}" rel="stylesheet" type="text/css" />

And in the Spring Boot project under the src/main/resources folder, I have

src/main/resources
|
|── css/
|   |── styles.css
|   |
. . . . .

It works like a charm!

Jonijonie answered 20/3, 2022 at 4:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.