December 16, 2009

HTML convert to Pdf in Java

« Python XML Processing - libxml2 | Main | Java Swing Example »

The IText library represents a Java API for creation PDF files. The following example describes how the developer can use IText to convert HTML to PDF.

Add Itext Maven dependencies:
    <dependency>
      <groupId>com.lowagie</groupId>
      <artifactId>itext</artifactId>
      <version>2.1.7</version>
    </dependency>
Example Pdf creation Java code:
package org.developers.blog.html2pdf;

import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.html.simpleparser.HTMLWorker;
import com.lowagie.text.html.simpleparser.StyleSheet;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.pdf.codec.Base64;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;

public class Html2Pdf {

    public static void main(String[] args) throws Exception {
        Document pdfDocument = new Document();
        Reader htmlreader = new BufferedReader(new InputStreamReader(
                                 new FileInputStream("/home/rafsob/OpenSourceProjekte/simple-monitor-plugin/target/site/index.html")
                                ));
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PdfWriter.getInstance(pdfDocument, baos);
        pdfDocument.open();
        StyleSheet styles = new StyleSheet();
        styles.loadTagStyle("body", "font", "Bitstream Vera Sans");
        ArrayList arrayElementList = HTMLWorker.parseToList(htmlreader, styles);
        for (int i = 0; i < arrayElementList.size(); ++i) {
            Element e = (Element) arrayElementList.get(i);
            pdfDocument.add(e);
        }
        pdfDocument.close();
        byte[] bs = baos.toByteArray();
        String pdfBase64 = Base64.encodeBytes(bs); //output
        File pdfFile = new File("/tmp/pdfExample.pdf");
        FileOutputStream out = new FileOutputStream(pdfFile);
        out.write(bs);
        out.close();
    }
}

Regards
Rafael Sobek

Technorati Tags:

Posted by rafael.sobek at 8:16 AM in Java

 

[Trackback URL for this entry]

Comment: Noman at Mi, 9 Jun 8:36 AM

hello
thanx for your post abt html to pdf conversion

Your comment:

(not displayed)
 
 
 

Live Comment Preview:

 
« December »
SunMonTueWedThuFriSat
  12345
6789101112
13141516171819
20212223242526
2728293031