使用飞碟打印PDF格式的CheckBox

时间:2016-02-17 00:25:42

标签: pdf-generation itextsharp itext flying-saucer html-to-pdf

我正在使用飞碟将HTML转换为PDF。我想在HTML中使用HTML输入属性,例如 CheckBox ,并将其打印在生成的PDF中。但是当我在html中编写复选框代码并生成PDF时,没有打印PDF格式的复选框

<?xml version="1.0" encoding="utf-8" ?>
<html>
<input type="checkbox" name="vehicle" value="Bike"> I have a bike
<input type="checkbox" name="vehicle" value="Car" checked> I have a car
</html>

的软件包:

    this.File = Packages.java.io.File;
    this.StringWriter = Packages.java.io.StringWriter;
    this.FileOutputStream = Packages.java.io.FileOutputStream;
    this.ByteArrayOutputStream = Packages.java.io.ByteArrayOutputStream;
    this.ByteArrayInputStream = Packages.java.io.ByteArrayInputStream;
    this.HttpClient = Packages.org.apache.commons.httpclient.HttpClient;
    this.UsernamePasswordCredentials = Packages.org.apache.commons.httpclient.UsernamePasswordCredentials;
    this.Protocol =  Packages.org.apache.commons.httpclient.protocol.Protocol;
    this.SSLProtocolSocketFactory =   Packages.org.apache.commons.httpclient.protocol.SSLProtocolSocketFactory;
    this.AuthScope = Packages.org.apache.commons.httpclient.auth.AuthScope;
    this.GetMethod = Packages.org.apache.commons.httpclient.methods.GetMethod;
    this.PostMethod = Packages.org.apache.commons.httpclient.methods.PostMethod;
    this.StringUtil = Packages.com.glide.util.StringUtil;
    this.StringEntity = Packages.org.apache.http.entity.StringEntity;
    this.SimpleDateFormat = Packages.java.text.SimpleDateFormat;
    this.TimeZone = Packages.java.util.TimeZone;
    this.Date = Packages.java.util.Date;

    // Flying saucer jar references
    //
    this.tidy =  Packages.org.w3c.tidy.Tidy;
    this.domDocument = Packages.org.w3c.dom.Document;
    this.XHTMLPanel =  Packages.org.xhtmlrenderer.simple.XHTMLPanel;
    this.iTextRenderer = Packages.org.xhtmlrenderer.pdf.ITextRenderer;



    var bos = new this.ByteArrayOutputStream();
    var inputString = this.htmlResponse;
    this.debug("HTML RESPONSE" + inputString);
    var inputStream = new this.ByteArrayInputStream(inputString.getBytes());

    var outputForPdfStream  = new this.ByteArrayOutputStream();

    // tidy the html
    var domdoc =  this.domDocument;
    var iTidy = new this.tidy();
    iTidy.setShowWarnings(false);
    iTidy.setXmlTags(false);
    iTidy.setInputEncoding("UTF-8");
    iTidy.setOutputEncoding("UTF-8");
    iTidy.setXHTML(true);//
    iTidy.setMakeClean(true);
    domdoc = iTidy.parseDOM(inputStream, outputForPdfStream);

    // Convert the document to XHTML panel and then rendering it into a PDF

    var xhtmlPanel = new this.XHTMLPanel();
    xhtmlPanel.setDocument(domdoc);


    var renderer = new this.iTextRenderer();
    renderer.setDocument(xhtmlPanel.getDocument(), null);
    renderer.layout();
    renderer.createPDF(bos);
    bos.flush();
    inputStream.close();
    this.debug("INPUT STREAM" + inputStream);

    var byteArray = bos.toByteArray();
    var encodedString  = this.StringUtil.base64Encode(byteArray);
    this.debug("Encoded String" + encodedString);

我是否需要使用任何特定包打印PDF格式的复选框。请让我知道,如果你有任何问题。任何帮助,将不胜感激。谢谢。

1 个答案:

答案 0 :(得分:0)

pdf不支持复选框。在转换之前用图像替换它们。我们在项目中做的很相似。

org.jsoup.nodes.Document doc = Jsoup.parse(formContent);

然后处理html标签输入,

String inputElementTag = inputElement.tagName();
 switch (inputElementTag) {
        case HTML_TAG_INPUT:
            String inputType = inputElement.attr(HTML_ATTR_TYPE);
            switch (inputType) {
            case HTML_INPUT_CHECKBOX:
                processCheckbox(inputElement, value);
                break;
            }
        }

然后替换为图像

private void processCheckbox(Element inputElement, String value) {
        if (value != null && Boolean.valueOf(value)) {
            inputElement.attr(HTML_ATTR_CHECKED, HTML_ATTR_CHECKED);
            inputElement.before(String.format(HTML_IMAGE_CHECKBOX,
                    inputElement.attr(HTML_ATTR_STYLE),
                    inputElement.attr(HTML_ATTR_CLASS),
                    IMAGE_CHECKED));
        } else {
            inputElement.removeAttr(HTML_ATTR_CHECKED);
            inputElement.before(String.format(HTML_CHECKBOX_EMPTY_HTML,
                    inputElement.attr(HTML_ATTR_STYLE),
                    inputElement.attr(HTML_ATTR_CLASS),
                    IMAGE_UNCHECKED));
        }
        inputElement.remove();
    }

要解析html并替换你可以使用Jsoup