Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
309 views
in Technique[技术] by (71.8m points)

java - HTML to PDF using iText : How can produce a checkbox

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();
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 &#x2610;. 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...