I am using Flying Saucer to render some PDF documents from strings to 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.
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
So, I have tried to supply
context path/css
direcotry in the baseURL and used it in the setDocument()
. Still no result
So, My Question What is the correct URL to pass as baseURL ?
String baseURL = ""; // What goes here as root URL for resources
iTextRenderer.setDocument(doc,baseURL);