HTML to PDF using iText : How can produce a checkbox

15,850

Solution 1

creating pdfs with iText from html is a bit troubled. i advise to use the flying saucer library for this. it is also using iText in the background.

Solution 2

Are you generating the HTML?

If so, then instead of using an HTML checkbox you could using the Unicode 'ballot box' character, which is or ☐. It's just a box, you can't electronically tick it or untick it; but if the PDF is intended for printing then of course people can tick it using a pen or pencil.

For example:

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

Note that this will only work if you're using a Unicode font in your PDF; I think that iText won't use a Unicode font unless you tell it to.

Solution 3

You may be out of luck here.

The "htmlWorker" which is used to parse the html tags, doesn't 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 i b u sub sup em strong s strike h1 h2 h3 h4 h5 h6 img";

You can access the source code for "HtmlWorker" from here. http://www.java2s.com/Open-Source/Java-Document/PDF/pdf-itext/com/lowagie/text/html/simpleparser/HTMLWorker.java.htm
It is from this source that I figured that out.

 public void startElement(String tag, HashMap h) {
         if (!tagsSupported.containsKey(tag))
                return; //return if tag not supported
 // ...
}

Solution 4

The only alternative I'm aware of at that point is to hack iText. The new XMLWorker should be considerably more extensible than The Old Way (HTMLWorker), but it'll still be Non Trivial.

There might be some magic style tag you can pass in that will show up in a "generic tag" for a PdfPageEventHandler... lets see here...

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

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

In your PdfPageEventHandler's OnGenericTag function, you have to parse that tag and recreate whatever it is you're trying to recreate.




That's just crazy enough to work!

Share:
15,850
RealHowTo
Author by

RealHowTo

Author of the Real's How-to @ https://www.rgagnon.com/howto.html

Updated on June 04, 2022

Comments

  • RealHowTo
    RealHowTo about 2 years

    I have a simple HTML page, iText is able to produce a PDF from it. It's fine but the checkbox is ignored. What can I do about it ?

    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 got it working 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();
        }
    }
    
  • Mark Storer
    Mark Storer about 13 years
    Flying saucer supports <input>?
  • Mark Storer
    Mark Storer about 13 years
    Not that I've tried it, mind you. The "generictag" thing might fail, at which point you're back to hacking iText yourself (or maybe flying saucer, or waiting for XMLWorker to get better).
  • Mark Storer
    Mark Storer about 13 years
    I prefer google code search, but that works too. Code search: \m/ >.< \m/
  • RealHowTo
    RealHowTo about 13 years
    one interesting thing about flying saucer is the code for FORM elements is commented! I will take a look at the new iText XMLWorker.
  • saban
    saban about 13 years
    i don't know exactly. but it supports most html features. in the past i used it in an api just for html and pdf reports. btw, if you want to just showing an input element in the pdf (i mean if it won't be fillable form) you can use a div and set it's styles via css.
  • Redlab
    Redlab about 13 years
    Must disappoint you The XMLWorker does not contain mapping for from elements to PDF, however, it's not hard to add implementations of a TagProcessor that does understand form elements.
  • RealHowTo
    RealHowTo almost 13 years
    I got it working with YAHP which uses FlyingSaucer/Itext, I have updated my question with the solution.
  • SRy
    SRy over 11 years
    Yes I tried YAHP....It's really cool.I don't for a Famous library like FS doesn't have ability to support simple tags in html.
  • Ashish
    Ashish about 7 years
    Is there similar one for radio button ?