External CSS in Flying Saucer

十年热恋 提交于 2019-12-12 11:01:15

问题


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

Image 2

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();
}

回答1:


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 :

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();



回答2:


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" />




回答3:


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.



来源:https://stackoverflow.com/questions/25990886/external-css-in-flying-saucer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!