flying-saucer/iText PDF in servlet not finding css file
I decided with just reading my CSS file on the server side into a String.
The readFile
method is based off of Jon Skeet's post at How do I create a Java string from the contents of a file?):
buf.append("<head><style>");
buf.append(readFile(getServletContext().getRealPath("/PDFservlet.css"), "UTF-8"));
buf.append("</style></head>");
HTML to PDF using Flying Saucer: Internal CSS showing in PDF page
If you replace:
@page {background-color: #f0f0f0;}
by
<!--
@page {background-color: #f0f0f0;}
-->
then the <style>
tag will recognize the style, but the style won't "count" as HTML because we've put it inside a comment.
HTML to PDF using iText External CSS
The FAQ tells this:
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.
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?
Sorry for new post, but comments told me i have only negative char's left ...
xhtml to pdf servlet with flyingsaucer
String css = getServletContext().getRealPath("/PDFservlet.css");
This is not right. It has to be an URL, not a local disk file system path. IText is attempting to download it by an URL "the usual way", like as a webbrowser would do.
One of the ways to construct the proper URL would be this:
StringBuffer url = req.getRequestURL();
String base = url.substring(0, url.length() - req.getRequestURI().length() + req.getContextPath().length());
String css = base + "/PDFservlet.css";
Related Topics
Nested Rows in Bootstrap 3 Fail to Respect The Grid
CSS Selector for a Child Element Whose Parent Element Has a Certain Class
Vertically Center Constrained Image in Bootstrap Thumbnail
How to Start New Row of CSS Table Cells Without a Row Wrapper Element
How to Change The Font of Some Textoutput Elements
Ie8 Ignores Td Width, Works on Ie7
Do I Still Need to Include Type="Value" in HTML5
Use CSS Variable with SCSS Darken and Ligthen Filter
Making a Button and Textbox The Same Height Next to Each Other
How to Position Two Divs Horizontally Within Another Div
What's The Correct Way to Set a Base Rem Value in CSS
Width and Height for a Span Does Not Show Up Unless a Text Is Entered
How to Remove Unused CSS Using Webpack 4