iText in Action: Chapter 9: Integrating iText in your web applications


If you compile and execute this example, you'll get the following result:

You can download the full source code of HtmlMovies1, or read it here:

 * This class is part of the book "iText in Action - 2nd Edition"
 * written by Bruno Lowagie (ISBN: 9781935182610)
 * For more info, go to:
 * This example only works with the AGPL version of iText.
package part3.chapter09;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import com.lowagie.database.DatabaseConnection;
import com.lowagie.database.HsqldbConnection;
import com.lowagie.filmfestival.Country;
import com.lowagie.filmfestival.Director;
import com.lowagie.filmfestival.Movie;
import com.lowagie.filmfestival.PojoFactory;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.html.simpleparser.HTMLWorker;
import com.itextpdf.text.html.simpleparser.StyleSheet;
import com.itextpdf.text.pdf.PdfWriter;
public class HtmlMovies1 {
    /** The resulting HTML file. */
    public static final String HTML = "results/part3/chapter09/movies_1.html";
    /** The resulting PDF file. */
    public static final String PDF = "results/part3/chapter09/html_movies_1.pdf";
    /** The StyleSheet. */
    protected StyleSheet styles = null;
    /** Extra properties. */
    protected HashMap<String,Object> providers = null;
     * Main method.
     * @param    args    no arguments needed
     * @throws DocumentException 
     * @throws IOException 
     * @throws SQLException
    public static void main(String[] args)
        throws IOException, DocumentException, SQLException {
        new HtmlMovies1().createHtmlAndPdf(HTML, PDF);
     * Creates a list with movies in HTML and PDF simultaneously.
     * @param html a path to the resulting HTML file
     * @param pdf a path to the resulting PDF file
     * @throws SQLException
     * @throws IOException
     * @throws DocumentException
    public void createHtmlAndPdf(String html, String pdf)
        throws SQLException, IOException, DocumentException {
        // Create a database connection
        DatabaseConnection connection = new HsqldbConnection("filmfestival");
        // Create a stream to produce HTML
        PrintStream out = new PrintStream(new FileOutputStream(html));
        // step 1
        Document document = new Document();
        // step 2
        PdfWriter.getInstance(document, new FileOutputStream(pdf));
        // step 3;
        // step 4
        List<Movie> movies = PojoFactory.getMovies(connection);
        String snippet;
        for (Movie movie : movies) {
            // create the snippet
            snippet = createHtmlSnippet(movie);
            // use the snippet for the HTML page
            // use the snippet for the PDF document
            List<Element> objects
                = HTMLWorker.parseToList(
                    new StringReader(snippet), styles, providers);
            for (Element element : objects)
        // step 5
        // flush and close the HTML stream
        // close the database connection
     * Sets the styles for the HTML to PDF conversion
     * @param styles a StyleSheet object
    public void setStyles(StyleSheet styles) {
        this.styles = styles;
     * Set some extra properties.
     * @param providers the properties map
    public void setProviders(HashMap<String, Object> providers) {
        this.providers = providers;
     * Creates an HTML snippet with info about a movie.
     * @param movie the movie for which we want to create HTML
     * @return a String with HTML code
    public String createHtmlSnippet(Movie movie) {
        StringBuffer buf = new StringBuffer("\t<span class=\"title\">");
        buf.append("</span><br />\n");
        for (Country country : movie.getCountries()) {
            buf.append("\t\t<li class=\"country\">");
        buf.append("\tYear: <i>");
        buf.append(" minutes</i><br />\n");
        buf.append("\tDuration: <i>");
        buf.append(" minutes</i><br />\n");
        for (Director director : movie.getDirectors()) {
            buf.append("\t\t<li><span class=\"director\">");
            buf.append(", ");
        return buf.toString();
Powered by GeSHi

The HtmlMovies1 example is part of the book iText in Action (ISBN 9781935182610).

It's a small standalone application. You can use this example for inspiration, but please read the book if there's something you don't understand about the example. You'll find more info in Chapter 9. Note that this example depends on DatabaseConnection, HsqldbConnection, Movie, Director, Country, PojoFactory, and maybe some other examples.

Keywords for this example: HTML, HTMLWorker, Basic building blocks > create from HTML, HTML > create PDF from HTML

If you want this example to work, you need the following jars: hsqldb.jar, iText.jar

This example uses the following resources: filmfestival.script.

This example is used as a helper class for: HtmlMovies2