I have researched about to convert HTML to PDF.
I got 2 approaches.
1. Using Tidy and XSL-FO.
2. Using the project xhtmlrenderer
Basically the 1st approach is :
1. Your HTML will need to be validate in according to XHTML, for this you could use Tidy.
2. After you will need to transform this new XHTML document in XLS-FO, you can review this link to find the stylesheet resource (XHMTL to XLS-FO).
3. Finally, convert your XLS-FO document in a PDF document.
There are 2 links that could help with this approach:
http://www.onjava.com/lpt/a/3924
http://www.javaworld.com/javaworld/jw-04-2006/jw-0410-html.html
The 2nd approach is using the project xhtmlrenderer (https://xhtmlrenderer.dev.java.net/)
This is easier than 1st approach. This tool hides the steps mentioned in the 1st approach and use CSS.
This project uses a CSS parser (http://sourceforge.net/projects/cssparser/).
the unique problem the I found out was when you want to use external CSS file in your HTML file.
In the example used in the project I had modified (red highlighted) :
...
String css = "../webapps/myContext/PDFservlet.css";
// put in some style
buf.append("<head><link rel='stylesheet' type='text/css' "+
"href='"+css+"' media='print'/></head>");
....
Document doc = builder.parse( input );
ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(doc, null);
I have deployed the project in Tomcat 5.5. The problem was related to find the css file by the renderer (URI and BASE URL).
Too, If your HTML is not validate in according to XHTML you should use Tidy(Jtidy).
Both approach made use of iText library(www.lowagie.com/iText/ ).
I got 2 approaches.
1. Using Tidy and XSL-FO.
2. Using the project xhtmlrenderer
Basically the 1st approach is :
1. Your HTML will need to be validate in according to XHTML, for this you could use Tidy.
2. After you will need to transform this new XHTML document in XLS-FO, you can review this link to find the stylesheet resource (XHMTL to XLS-FO).
3. Finally, convert your XLS-FO document in a PDF document.
There are 2 links that could help with this approach:
http://www.onjava.com/lpt/a/3924
http://www.javaworld.com/javaworld/jw-04-2006/jw-0410-html.html
The 2nd approach is using the project xhtmlrenderer (https://xhtmlrenderer.dev.java.net/)
This is easier than 1st approach. This tool hides the steps mentioned in the 1st approach and use CSS.
This project uses a CSS parser (http://sourceforge.net/projects/cssparser/).
the unique problem the I found out was when you want to use external CSS file in your HTML file.
In the example used in the project I had modified (red highlighted) :
...
String css = "../webapps/myContext/PDFservlet.css";
// put in some style
buf.append("<head><link rel='stylesheet' type='text/css' "+
"href='"+css+"' media='print'/></head>");
....
Document doc = builder.parse( input );
ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(doc, null);
I have deployed the project in Tomcat 5.5. The problem was related to find the css file by the renderer (URI and BASE URL).
Too, If your HTML is not validate in according to XHTML you should use Tidy(Jtidy).
Both approach made use of iText library(www.lowagie.com/iText/ ).
Comments
san diego seo