Convert Html To Pdf in java using iText


In this core java tutorial we will learn How To Convert Html To Pdf in java using iText library - core java tutorial with program and examples.

Follow these steps to Convert Html To Pdf in java using iText library >
  • Build html using StringBuilder to be converted to pdf.
  • Create input stream to convert html and write it to pdf and instantiate with  new ByteArrayInputStream(stringBuilderHtml.toString().getBytes());
XMLWorkerHelper.getInstance().parseXHtml(pdfWriter, document, is);


Download jars required to execute program >

Or, you may download jars from here.


We will write program to convert below html file to pdf in java >
<!DOCTYPE html>
<html>
<body>
<table border='1'>
<tr>
 <td>Java tutorial</td>
 <td> <a href="http://www.javamadesoeasy.com/2015/04/core-java-tutorial.html">JavaMadeSoEasy.com</a> </td>  
</tr>
</table>
</body>
</html>



How to Set the Font Name, Size, Style and Colour In Pdf using itext in java

How To Set HyperLink (Anchor - a tag) In Pdf in java - iText java tutorial

Convert Html To Pdf in java using iText - iText java tutorial

How To Set Header and Footer in pdf in java using Itext Example - iText java tutorial



Program/Example How To Convert Html To Pdf in java - Using iText library

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import com.itextpdf.text.Document;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.XMLWorkerHelper;
/**
*
*  Convert Html To Pdf Using Itext library Example
*
*/
public class ConvertHtmlToPdfUsingItextExample {
public static void main(String[] args) throws Exception{
          String pdfFilePath = "e:/Convert Html To Pdf Using Itext - Example in java.pdf";
          OutputStream fos = new FileOutputStream(new File(pdfFilePath));
          Document document = new Document();
          PdfWriter pdfWriter = PdfWriter.getInstance(document, fos);
          //Build html using StringBuilder to be converted to pdf.
          StringBuilder stringBuilderHtml = new StringBuilder();
          stringBuilderHtml.append("<!DOCTYPE html>");
          stringBuilderHtml.append("<html>");
          stringBuilderHtml.append("<body>");
          stringBuilderHtml.append("<table border='1'>");
          stringBuilderHtml.append("  <tr>");
          stringBuilderHtml.append(" <td>Java tutorial</td>");
          stringBuilderHtml.append(" <td> <a href='
              >JavaMadeSoEasy.com</a></td>");
          stringBuilderHtml.append("  </tr>");
          stringBuilderHtml.append("</table>");
          stringBuilderHtml.append("</body>");
          stringBuilderHtml.append("</html>");
                           
          document.open();
          InputStream is =
                  new ByteArrayInputStream(stringBuilderHtml.toString().getBytes());
          XMLWorkerHelper.getInstance().parseXHtml(pdfWriter, document, is);
          document.close();
         
          fos.close();
          System.out.println("PDF created in >> " + pdfFilePath);
}
}
/* OUTPUT of above program/Example -
PDF created in >> e:/Convert Html To Pdf Using Itext - Example in java.pdf
*/


PDF formed after executing above java program will look like this >


Summary -

So in this core java tutorial we learned How To Convert Html To Pdf in java using iText library - core java tutorial with program and examples.


Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.

References >



RELATED LINKS>

iText library tutorial in java - How to Create and work with PDF files in java

Creating the Table and set the Column Width And Spacing In Pdf in java

Rotate cells, cell padding, horizontal and vertical alignment of Cell in table in Pdf in java itext - iText java tutorial example




Creating LIST in pdf in java - iText tutorial >

Create Greek List In pdf in java using iText - iText java tutorial

How to Create Roman List In pdf in java - iText java tutorial

Create ZapfDingbats List In pdf in java - iText java tutorial program

How to Create Ordered List In pdf in java using iText - iText java tutorial

Creating the UnOrdered List In pdf in java - iText java tutorial example

How to Create Lists And SubLists In Pdf in java - iText java tutorial



Creating PASSWORD PROTECTED PDF in java - iText tutorial >

How to Create Password Protected Pdf in java - iText Example




Set FONT NAME, SIZE, STYLE, COLOUR in pdf in java - iText tutorial >

How to Set the Font Name, Size, Style and Colour In Pdf using itext in java


Create ANCHOR (HYPERLINK), SUPERSCRIPT, SUBSCRIPT, UNDERLINE AND STRIKETHROUGH in pdf in java - iText tutorial >

How To Set HyperLink (Anchor - a tag) In Pdf in java - iText java tutorial

How To Set SuperScript And SubScript In Pdf in java - iText java tutorial

UnderLine Text In Pdf in java - iText java tutorial example

Strikethrough Text In Pdf in java - iText java tutorial program


HTML to PDF in java - iText tutorial >

Convert Html To Pdf in java using iText - iText java tutorial


Create new page, set page height and width in java - iText tutorial >

How To Create New Pages In Pdf Using Itext - iText java tutorial example

How to Find Pdf Page Height And Width in java - iText java tutorial program



HEADER and FOOTER in pdf in java - iText tutorial >

How To Set Header and Footer in pdf in java using Itext Example - iText java tutorial



BAR AND PIE CHARTS in pdf in java - iText tutorial >

How to create Bar Chart In Pdf in java - iText java tutorial

Create Pie Chart In Pdf in java using itext - iText java tutorial



MODIFY/ EDIT pdf in java - iText tutorial >

How to Modify - Add Text To Existing PDF in java - iText java tutorial example

How To Read And Add Image To Existing Pdf in java Example - iText java tutorial



Solve common Exceptions in itext in java - iText tutorial >

How to solve the Document Has No Pages IOException in iText in java


Alternates to itext in java - iText tutorial >

What are alternatives to iText library for creating Pdf in java?


No comments:

Post a Comment