Html to Pdf in Java using iText

(Last Updated On: August 11, 2016)

Required Jar filess:

Html to Pdf Required Jar Files

Program:

package in.javadomain;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import org.xhtmlrenderer.pdf.ITextRenderer;
import com.lowagie.text.DocumentException;
public class Html2Pdf {
public static void main(String[] args) throws IOException,
DocumentException {
// Specify the input html file location
String htmlInputFileUrl = "D:\\javadomain.html";

String url = new File(htmlInputFileUrl).toURI().toURL().toString();
// specify the output pdf file location
String htmlToPdf = "D:\\Javadomain.pdf";
System.out.println("Pdf file created successfully in "+url);
OutputStream os = new FileOutputStream(htmlToPdf);
ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(url);
renderer.layout();
renderer.createPDF(os);
os.close();
}
}

Output:
Pdf file created successfully in file:/D:/javadomain.html

Generated Pdf:

generated pdf

293 total views, 0 views today

Leave a Reply

Your email address will not be published. Required fields are marked *