Flying-Saucer/Itext PDF in Servlet Not Finding CSS File

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



Leave a reply



Submit