I'm currently working on building a PDF based report in Java. I have looked at iText and BIRT but it seems like a lot of effort to learn their API's what I'm trying to find is a solution that will let me generate the report in HTMl (something I already know) and output that to PDF. Can anyone provide some possible solutions? - Thanks! - Duncan krebs
-
You can view this: http://stackoverflow.com/questions/235851/using-itext-to-convert-html-to-pdf – Beez Apr 06 '11 at 16:52
5 Answers
Flying Saucer converts XHTML to PDF. It is great. It is not fast. It fails if there is a slight error in your XHTML syntax. (such as <br>
when it should be <br/>
)
This is the link that got me started. It seems to use iText, but once you have the thing working, just change the HTML and it updates.
http://today.java.net/pub/a/today/2007/06/26/generating-pdfs-with-flying-saucer-and-itext.html
There may be a better way, this is how I did it.

- 85,281
- 83
- 234
- 341
If your source HTML is styled with CSS and not necessarily well-formed, try PD4ML library (free for non-profit use).

- 339
- 4
- 11
-
1Welcome to Stack Overflow! Thanks for posting your answer! Please be sure to read the [FAQ on Self-Promotion](http://stackoverflow.com/faq#promotion) carefully. Also note that it is *required* that you post a disclaimer every time you link to your own site/product. – Andrew Barber Nov 05 '12 at 17:54
i can recommend jodconverter it uses openoffice in headless mode
1 install openoffice (for linux "zypper install libreoffice")
2 WIN put it on the path-variable so "soffice" can be run from everywhere, for me it was "C:\Program Files (x86)\LibreOffice 4\program"
3 LINUX make sure the user which runs the java process owns his home directory, because openoffice needs to store configs there, for me tomcat ran the process, but its home dir was owned by root
4 add jodconverter-lib to your java project
<dependency>
<groupId>com.artofsolving</groupId>
<artifactId>jodconverter</artifactId>
<version>2.2.1</version>
</dependency>
convert
// ensure open office is running
String[] commands = new String[] {"soffice","--headless","--accept=socket,host=localhost,port=8100;urp;"};
Runtime.getRuntime().exec(commands);
// convert
String html = "<div>hey there</div>";
ByteArrayOutputStream pdfOutputStream = new ByteArrayOutputStream();
DefaultDocumentFormatRegistry defaultDocumentFormatRegistry = new DefaultDocumentFormatRegistry();
OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
connection.connect();
DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
converter.convert(IOUtils.toInputStream(html, Charset.forName("UTF-8")), defaultDocumentFormatRegistry.getFormatByFileExtension("html"), pdfOutputStream, defaultDocumentFormatRegistry.getFormatByFileExtension("pdf"));
connection.disconnect();
byte[] pdfBytes = pdfOutputStream.toByteArray();

- 14,365
- 19
- 99
- 170
Using phantomjs, you can convert HTML to PDF very easily:
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriverService;
import org.openqa.selenium.remote.DesiredCapabilities;
public class Screenshot {
public static final String SCRIPT = "var page = require('webpage').create();\n" +
"page.open('@@URL@@', function() {\n" +
" page.render('@@FILE@@');\n" +
"});\n";
public static void main(String[] args) {
final String url = args[0];
final String file = args[1];
final String script = SCRIPT.replace("@@URL@@", url).replace("@@FILE@@", file);
final DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,
"/path/to/phantomjs/bin/phantomjs");
try {
PhantomJSDriver phantomJSDriver = new PhantomJSDriver(capabilities);
phantomJSDriver.executePhantomJS(script);
} finally {
phantomJSDriver.close();
}
}
}
If the file
name ends with .pdf
then the webpage will be saved as PDF. Phantomjs also supports PNG, JPG and GIF output.
This is a very simple example, more generally the screenshot process is very customizable (set viewport size, enable/disable javascript, etc). Look at PhantomJS's page on screen capturing for more info.

- 1,920
- 1
- 16
- 17