HTML to PDF using iText: how to create a checkbox

I have a simple HTML page, iText can create a PDF file. This is normal, but the flag is ignored. What can i do with this?

import java.io.FileOutputStream; import java.io.StringReader; import com.itextpdf.text.Document; import com.itextpdf.text.PageSize; import com.itextpdf.text.html.simpleparser.HTMLWorker; import com.itextpdf.text.pdf.PdfWriter; public class HtmlToPDF { public static void main(String ... args ) { try { Document document = new Document(PageSize.LETTER); PdfWriter pdfWriter = PdfWriter.getInstance(document, new FileOutputStream("c://temp//testpdf.pdf")); document.open(); String str = "<HTML><HEAD></HEAD><BODY><H1>Testing</H1><FORM>" + "check : <INPUT TYPE='checkbox' CHECKED/><br/>" + "</FORM></BODY></HTML>"; htmlWorker.parse(new StringReader(str)); document.close(); System.out.println("Done."); } catch (Exception e) { e.printStackTrace(); } } } 



I worked with YAHP ( http://www.allcolor.org/YaHPConverter/ ).

 import java.io.File; import java.io.FileOutputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; // http://www.allcolor.org/YaHPConverter/ import org.allcolor.yahp.converter.CYaHPConverter; import org.allcolor.yahp.converter.IHtmlToPdfTransformer; public class HtmlToPdf_yahp { public static void main(String ... args ) throws Exception { htmlToPdfFile(); } public static void htmlToPdfFile() throws Exception { CYaHPConverter converter = new CYaHPConverter(); File fout = new File("c:/temp/x.pdf"); FileOutputStream out = new FileOutputStream(fout); Map properties = new HashMap(); List headerFooterList = new ArrayList(); String str = "<HTML><HEAD></HEAD><BODY><H1>Testing</H1><FORM>" + "check : <INPUT TYPE='checkbox' checked=checked/><br/>" + "</FORM></BODY></HTML>"; properties.put(IHtmlToPdfTransformer.PDF_RENDERER_CLASS, IHtmlToPdfTransformer.FLYINGSAUCER_PDF_RENDERER); //properties.put(IHtmlToPdfTransformer.FOP_TTF_FONT_PATH, fontPath); converter.convertToPdf(str, IHtmlToPdfTransformer.A4P, headerFooterList, "file://c:/temp/", out, properties); out.flush(); out.close(); } } 
+4
java pdf-generation itext
May 26 '11 at 4:12
source share
4 answers

creating PDF files with iText from html is a little difficult. I suggest using a flying saucer library for this. It also uses iText in the background.

+2
May 26 '11 at 6:52
source share
— -

Do you generate HTML?

If so, then instead of using the HTML checkbox, you can use the Unicode character "voting ballot", which is or &#x2610; . This is just a box; you cannot electronically tag or discard it; but if the PDF is for printing, then of course people can mark it with a pen or pencil.

For example:

  String str = "<HTML><HEAD></HEAD><BODY><H1>Testing</H1><FORM>" + "check : &#x2610;<br/>" + "</FORM></BODY></HTML>"; 

Please note that this will only work if you use the Unicode font in your PDF; I think iText will not use the Unicode font unless you report it.

+5
May 26 '11 at 4:32
source share

You may be out of luck.

The "htmlWorker" used to parse html tags does not seem to support the "input" tag.

 public static final String tagsSupportedString = "ol ul li a pre font span br p div body table td th tr ibu sub sup em strong s strike h1 h2 h3 h4 h5 h6 img"; 

Here you can get the source code for "HtmlWorker". http://www.java2s.com/Open-Source/Java-Document/PDF/pdf-itext/com/lowagie/text/html/simpleparser/HTMLWorker.java.htm
It was from this source that I understood this.

  public void startElement(String tag, HashMap h) { if (!tagsSupported.containsKey(tag)) return; //return if tag not supported // ... } 
+3
May 26 '11 at 4:31
source share

The only alternative I know at this point is to hack iText. The new XMLWorker should be significantly more extensible than The Old Way (HTMLWorker), but it will still be illogical.

There may be some magic style tag that you can pass that appears in the "common tag" for PdfPageEventHandler ... lets see here ...

Reading the code, it looks like the style or attribute "generictag" will be passed to the object ...text.Chunk via setGenericTag() .

So what you need to do is XSLT your unsupported tags in div / p / any with the attribute "generictag", which is a string that encodes the information needed to recreate the original element.

In your PdfPageEventHandler OnGenericTag function, you need to parse this tag and recreate everything you are trying to recreate.




It is just crazy to work!

+1
May 26 '11 at 16:17
source share



All Articles