java 使用 iText 外部 CSS 将 HTML 转换为 PDF
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12043035/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
HTML to PDF using iText External CSS
提问by Hardik Mishra
I am using Flying Saucer to render some PDF documents from strings to HTML.
我正在使用 Flying Saucer 将一些 PDF 文档从字符串呈现为 HTML。
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
InputStream is = new ByteArrayInputStream(html.getBytes("UTF-8"));
Document doc = builder.parse(is);
response.setContentType("application/pdf; charset=UTF-8");
response.setHeader("Content-disposition", "inline; filename=\"" + outFileName + "\"");
OutputStream os = response.getOutputStream();
ITextRenderer iTextRenderer = new ITextRenderer();
iTextRenderer.setDocument(doc,null);
iTextRenderer.layout();
iTextRenderer.createPDF(os);
os.flush();
os.close();
This works fine When I have plain text. I have referenced an external CSS in my HTML content. But, When PDF gets generated CSS doesn't get applied.
当我有纯文本时,这很好用。我在我的 HTML 内容中引用了一个外部 CSS。但是,当 PDF 生成时,不会应用 CSS。
I have read that The setDocument()
method takes two parameters: document and url. The url parameter indicates the base url used to prepend to relative paths that appear in the xhtml, such as an external CSS
我读过该setDocument()
方法需要两个参数:document 和 url。url 参数表示用于添加到 xhtml 中出现的相对路径的基本 url,例如外部 CSS
So, I have tried to supply
所以,我试图提供
context path/css
上下文路径/css
direcotry in the baseURL and used it in the setDocument()
. Still no result
目录中的 baseURL 并在setDocument()
. 还是没有结果
So, My Question What is the correct URL to pass as baseURL ?
所以,我的问题作为 baseURL 传递的正确 URL 是什么?
String baseURL = ""; // What goes here as root URL for resources
iTextRenderer.setDocument(doc,baseURL);
回答by ollo
The FAQtells this:
该FAQ讲述了这样:
The url is the "base", which for a normal URL would be the parent location—the parent directory, or the location where the document you are rendering is located.If your document has absolute URIs for CSS and images, or it has no external references at all, then the base url can be null.If your document has any relative URIs for CSS or images, then the base URL should not be null, but rather should point to the directory or address where the current document is located.
url 是“基础”,对于普通 URL 来说,它是父位置——父目录,或者你正在渲染的文档所在的位置。如果你的文档有 CSS 和图像的绝对 URI,或者它没有如果您的文档有任何 CSS 或图像的相对 URI,则基本 URL 不应为空,而应指向当前文档所在的目录或地址。
Did you test the path to your document instead of the path to your css? However, i had some trouble with linking CSS too so i inserted the URI (no problems so far :-) ). If you use a link as i posted above, does it work?
您是否测试了文档的路径而不是 css 的路径?但是,我在链接 CSS 时也遇到了一些麻烦,所以我插入了 URI(目前没有问题:-))。如果您使用我上面发布的链接,它是否有效?
Sorry for new post, but comments told me i have only negative char's left ...
抱歉新帖子,但评论告诉我我只剩下负字符了......
回答by ollo
You can insert the CSS paths by putting<link rel="stylesheet" type="text/css" href="file://path/to/your.css" />
into your document (head).
您可以通过放入<link rel="stylesheet" type="text/css" href="file://path/to/your.css" />
文档(头部)来插入 CSS 路径。
(In some cases you can use a simple path instead of an URI)
(在某些情况下,您可以使用简单路径而不是 URI)
回答by Lalo Mtz
The problem is not the filepath, the problem is the media. One thing is to render to screen and other to render to print media like a pdf file. So you need to add an attribute to the stylesheet TAG inside your XML file.
问题不在于文件路径,而在于媒体。一件事是渲染到屏幕,另一件事是渲染到印刷媒体,如 pdf 文件。因此,您需要向 XML 文件中的样式表标签添加一个属性。
From code.google.com/p/flying-saucer/wiki/FAQPDF
来自 code.google.com/p/flying-saucer/wiki/FAQPDF
My PDF isn't picking up my CSS!
PDF is treated as "print" media; see the CSS 2.1 specification section on media types. Make sure you have specified the media type for your CSS when you link or embed it; use type "print" or "all".
我的 PDF 没有选择我的 CSS!
PDF 被视为“印刷”媒体;请参阅有关媒体类型的 CSS 2.1 规范部分。确保在链接或嵌入 CSS 时为 CSS 指定了媒体类型;使用类型“打印”或“全部”。
The quick answer is to add the attribute media="all"
to the XML stylesheet TAG like this:
快速回答是将属性添加 media="all"
到 XML 样式表 TAG 中,如下所示:
<?xml-stylesheet href="foo.css" media="all" type="text/css"?>
alternative you can use media="print"
您可以使用的替代方法 media="print"