I am getting "Invalid nested tag font found, expected closing tag table" when trying to convert one HTML to PDF using iText. Following is the program I am using.
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.StringReader;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Font.FontFamily;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.GrayColor;
import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.XMLWorkerHelper;
public class HTML2PDF {
class Watermark extends PdfPageEventHelper{
//font for watermarking text
Font font = new Font(FontFamily.HELVETICA, 52, Font.ITALIC, new GrayColor(.75f));
public void onEndPage(PdfWriter writer, Document document){
ColumnText.showTextAligned(writer.getDirectContentUnder(),
Element.ALIGN_CENTER,
new Phrase("Watermarking Text", font),
297.5f, 421,-45);//-45 specifies the angle of Watermarking Text
}
}
public void convertHTML2PDF(){
//Create objects of Document and specify the Page size of a PDF
Document document = new Document(PageSize.A4);
try{
PdfWriter pdfWriter;
//get the instance of class PdfWriter with the document objects and output path
pdfWriter = PdfWriter.getInstance(document, new FileOutputStream("/opt/remedy/html2any-cmd-linux/bin/test.pdf"));
//setting the Watermarking in the PageEvent of PdfWriter
pdfWriter.setPageEvent(new Watermark());
document.open();
String htmlContent = "";
BufferedReader in = new BufferedReader(new FileReader("/opt/remedy/html2any-cmd-linux/bin/WO00000001004641434460592596_1.html"));
String temp;
//read the html files content and stores it in a String variable
while((temp = in.readLine())!=null){
htmlContent += temp;
}
in.close();
XMLWorkerHelper xmlWorker = XMLWorkerHelper.getInstance();
// converts the html into a PDF
xmlWorker.parseXHtml(pdfWriter, document, new StringReader(htmlContent));
document.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
//main method
public static void main(String[] args){
new HTML2PDF().convertHTML2PDF();
}
}
There are some tags in html which do not need to be closed. Could you suggest how this type of html can be parsed using XMLWorker?