I am trying to convert HTML to PDF using Flying Saucer. I need to be able to use style sheet to do positioning, set font size, color, etc. From what I have searched so far, the FS is the best way to do that. I can't find a discussion forum that I can post a question to on their site, so that's why I am posting a question here. Hopefully, someone can help me here.
 
I have taken the sample code (the 99 bottles of beer) from this article:
http://today.java.net/pub/a/today/2007/06/26/generating-pdfs-with-flying-saucer-and-itext.html
 
I got it to work in Eclipse. However, my ultimate goal is to port the code into a Lotus Domino java agent. When I do that, I get the error below. The PDF is created, but it completely ignores the CSS. It treats the style as text and it is shown as plain text on the PDF. I tried using an external style sheet, but the result is the same.
I appreciate any advice you can give me. Thanks.
 
Here's the error:
06/30/2009 03:37:21 PM  HTTP JVM: Jun 30, 2009 3:37:08 PM org.xhtmlrenderer.util.XRLog log
INFO: Using CSS implementation from: org.xhtmlrenderer.context.StyleReference
06/30/2009 03:37:21 PM  HTTP JVM: Jun 30, 2009 3:37:08 PM org.xhtmlrenderer.util.XRLog log
WARNING: Could not parse default stylesheet
java.lang.NullPointerException
 at org.xhtmlrenderer.simple.extend.XhtmlCssOnlyNamespaceHandler.getDefaultStylesheetStream(XhtmlCssOnlyNamespaceHandler.java:407)
 at org.xhtmlrenderer.simple.extend.XhtmlCssOnlyNamespaceHandler.getDefaultStylesheet(XhtmlCssOnlyNamespaceHandler.java:372)
 at org.xhtmlrenderer.context.StyleReference.getStylesheets(StyleReference.java:242)
06/30/2009 03:37:21 PM  HTTP JVM: Jun 30, 2009 3:37:09 PM org.xhtmlrenderer.util.XRLog log
INFO: TIME: parse stylesheets  156ms
06/30/2009 03:37:21 PM  HTTP JVM: Jun 30, 2009 3:37:09 PM org.xhtmlrenderer.util.XRLog log
INFO: media = print
06/30/2009 03:37:21 PM  HTTP JVM: Jun 30, 2009 3:37:09 PM org.xhtmlrenderer.util.XRLog log
INFO: Requesting stylesheet: http://www.w3.org/1999/xhtml
06/30/2009 03:37:22 PM  HTTP JVM: Jun 30, 2009 3:37:09 PM org.xhtmlrenderer.util.XRLog log
WARNING: (http://www.w3.org/1999/xhtml) Found < (other) where one of a hex color, ., [, or : was expected at line 1. Skipping ruleset.
06/30/2009 03:37:22 PM  HTTP JVM: Jun 30, 2009 3:37:09 PM org.xhtmlrenderer.util.XRLog log
INFO: Matcher created with 1 selectors

Here's the java code
>>>>>>>> Java Agent: START >>>>>>>>>>>>>>
import lotus.domino.*;
import java.io.PrintWriter;
import java.io.FileOutputStream;
import java.io.File;
import java.io.OutputStream;
import java.io.StringBufferInputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.xhtmlrenderer.pdf.ITextRenderer;
public class JavaAgent extends AgentBase {
 public void NotesMain() {
 try {
 
 Session session = getSession();
 AgentContext agentContext = session.getAgentContext();
 lotus.domino.Document docCon = agentContext.getDocumentContext();
 Database db = agentContext.getCurrentDatabase();
   
 // (Your code goes here)
 StringBuffer buf = new StringBuffer();
 buf.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
 buf.append("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">");
 buf.append("<html xmlns=\"http://www.w3.org/1999/xhtml\">");
 // put in some style
 buf.append("<head><style language=\"text/css\">");
 buf.append("h2 { background: #5555ff; color: white; border: 10px solid black; padding: 3em; font-size: 200%; }" );
 buf.append("</style></head>");
 
 // generate the body
 buf.append("<body>");
 for(int i=99; i>0; i--) {
  buf.append("<h3>"+i+" bottles of beer on the wall, " + i + " bottles of beer!</h3>");
  buf.append("<p>Take one down and pass it around, " + (i-1) + " bottles of beer on the wall</p>\n");
 }
 buf.append("<h2>No more bottles of beer on the wall, no more bottles of beer. ");
 buf.append("Go to the store and buy some more, 99 bottles of beer on the wall.</h2>");
 buf.append("</body>");
 buf.append("</html>");
 // parse the markup into an xml Document
 DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
 Document doc = builder.parse(new StringBufferInputStream(buf.toString()));
 ITextRenderer renderer = new ITextRenderer();
 renderer.setDocument(doc, null);
 /* make a filename that's based on docid */
 String fName = docCon.getUniversalID() + ".pdf";
 String filePath= "C:\\" + fName ;
 PrintWriter pw = getAgentOutput();
 OutputStream os = new FileOutputStream(filePath);
 renderer.layout();
 renderer.createPDF(os);
 os.close();

 } catch(Exception e) {
  e.printStackTrace();
  }
 }
}
>>>>>>>> Java Agent: END >>>>>>>>>>>>>>
 
 
------------------------------------------------------------------------------
_______________________________________________
iText-questions mailing list
iText-questions@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/itext-questions

Buy the iText book: http://www.1t3xt.com/docs/book.php
Check the site with examples before you ask questions: 
http://www.1t3xt.info/examples/
You can also search the keywords list: http://1t3xt.info/tutorials/keywords/

Reply via email to