飞碟中的外部CSS

问题描述 投票:0回答:6

我想知道如何在 Flying-Saucer 中包含外部 CSS。在那之前 THB 我检查了

StackOverflow
中的所有可用链接,但它们没有帮助。这就是我自己制作这个的原因。

TestCSS.xhtml
重命名了
TestCSS.html
的版本。所以它们的内容是相同的。 下面(图 1)是 Eclipse IDE 中我的项目的结构。如果我运行 TestCSS.html,它将在浏览器中给出如图 2 所示的页面结果。

下面是不能作为外部 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" />

我尝试了所有方法,包括xhtml内部css的绝对路径。但是css没有得到应用。请帮我解决这个问题。

图1

enter image description here

图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 代码:

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();
}
java css flying-saucer
6个回答
0
投票

考虑到项目的结构,

<link href="css/RedCSS.css" rel="stylesheet" type="text/css" />
肯定可以工作。

这是一个工作示例:

文件结构:

enter image description here

文件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>

文件2:

css/testRed.css

.redFontClass {color : red;}

Java 代码:

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

0
投票

我在本地机器上尝试了类似的事情,并使用 obourgain 的解决方案进行了尝试


    <link href="css/testRed.css" rel="stylesheet" type="text/css" />
这种方式的响应是302,这意味着通过重定向找到了资源,但这次获取的结果是空的。 当我在 css 之前添加 / 时,一切正常。
<link href="css/testRed.css" rel="stylesheet" type="text/css" />


0
投票

我在 Spring Boot 环境中使用“classpath”关键字。


    <link href="/css/testRed.css" rel="stylesheet" type="text/css" />

为我工作。

我希望这对使用 Spring Boot + Thymeleaf + Flying Saucer 设置的人有所帮助。


0
投票

最近我遇到了类似的问题:原来CSS文件的编码不同。您可能有同样的问题。首先,您需要找出文件的编码:

<link href="/css/testRed.css" rel="stylesheet" type="text/css" />

然后将其转换为UTF-8:

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

在我的例子中,从 UTF-16 转换为 UTF-8 后,生成了一个 PDF 文件。


0
投票

请参考此链接。 https://web.archive.org/web/20150905173204/http://today.java.net/pub/a/today/2007/06/26/generate-pdfs-with-flying-saucer-and-itext .html

它提到,当你想使用外部CSS文件时,你应该在链接标签上有一个media =“print”属性。

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


0
投票

对我来说,我只是做了Wolf359所做的事情:

一点细节,这是我的 html 模板:

file -i <my css file>

并且在

iconv -f <my css file enconding> -t UTF-8 <my css file> > <my css file in utf-8>
文件夹下的Spring Boot项目中,我有

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

它就像一个魅力!

© www.soinside.com 2019 - 2024. All rights reserved.