在PDF中转换页面上的现有表单

时间:2014-04-09 12:11:14

标签: java html spring-mvc pdf struts

在我的STRUTS MVC JAVA项目中,我在HTML文件中有一个表单。该HTML包含在JSP文件中。我想要的是在SUBMIT附近提供一个按钮“下载为PDF”。当我点击这个下载按钮时,它应该被下载到我的本地机器上,包含所有值,无论我填写的是什么。

请帮忙,因为我对这类问题不太了解。

谢谢,

3 个答案:

答案 0 :(得分:2)

添加PDFmyForm按钮,就是这样。

您只需在页面中添加以下两行:

<script type="text/javascript" src="http://www.pdfmyform.com/js/pdfmyform.js"></script>
<a onclick="pdfmyform(this);" href="#">PDF this page!</a>

答案 1 :(得分:0)

您可以在iText中可操作地生成PDF并将其发送给客户。

答案 2 :(得分:0)

查看Apache FOP

使用此代码作为基础:

private void printStatement(Statement s, HttpServletResponse resp) throws IOException {
        StringBuffer sb = new StringBuffer();

        sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
                "\n" +
                "<fo:root xmlns:fo=\"http://www.w3.org/1999/XSL/Format\">\n" +
                "\n" +
                "    <fo:layout-master-set>\n" +
                "        <fo:simple-page-master master-name=\"spm\"\n" +
                "                               page-height=\"29.7cm\"\n" +
                "                               page-width=\"21cm\"\n" +
                "                               margin-top=\"1cm\"\n" +
                "                               margin-bottom=\"1cm\"\n" +
                "                               margin-left=\"1cm\"\n" +
                "                               margin-right=\"1cm\">\n" +
                "            <fo:region-body/>\n" +
                "        </fo:simple-page-master>\n" +
                "    </fo:layout-master-set>\n" +
                "\n" +
                "    <fo:page-sequence master-reference=\"spm\">\n" +
                "        <fo:flow flow-name=\"xsl-region-body\">\n" +
                "\n" +
                "            <fo:block font-weight=\"bold\" font-size=\"16pt\" font-family=\"sans-serif\" line-height=\"24pt\"\n" +
                "                      space-after.optimum=\"15pt\" text-align=\"center\" padding-top=\"3pt\">\n" +
                "                Credit card processing statement\n" +
                "            </fo:block>\n" +
                "\n" +
                "            <fo:table table-layout=\"fixed\" width=\"100%\" border-collapse=\"separate\">\n" +
                "                <fo:table-column column-width=\"35mm\"/>\n" +
                "                <fo:table-column column-width=\"60mm\"/>\n" +
                "                <fo:table-column column-width=\"35mm\"/>\n" +
                "                <fo:table-column column-width=\"60mm\"/>\n" +
                "                <fo:table-body>\n" +
                "\n" +
                "                    <fo:table-row>\n" +
                "                        <fo:table-cell>\n" +
                "                            <fo:block font-weight=\"bold\"  font-size=\"10pt\" font-family=\"serif\">\n" +
                "                                From:\n" +
                "                            </fo:block>\n" +
                "                        </fo:table-cell>\n" +
                "\n" +
                "                        <fo:table-cell>\n" +
                "                            <fo:block font-weight=\"normal\" font-size=\"10pt\" font-family=\"serif\">\n" +
                "                                " + s.getStartDate() + "\n" +
                "                            </fo:block>\n" +
                "                        </fo:table-cell>\n" +
                "\n" +
                "                        <fo:table-cell><fo:block> </fo:block></fo:table-cell>\n" +
                "                        <fo:table-cell><fo:block> </fo:block></fo:table-cell>\n" +
                "                    </fo:table-row>\n" +
                "\n" +
                "\n" +
                "                    <fo:table-row>\n" +
                "                        <fo:table-cell>\n" +
                "                            <fo:block font-weight=\"bold\"  font-size=\"10pt\" font-family=\"serif\">\n" +
                "                                Address:\n" +
                "                            </fo:block>\n" +
                "                        </fo:table-cell>\n" +
                "\n" +
                "                        <fo:table-cell>\n" +
                "                            <fo:block font-weight=\"normal\" font-size=\"10pt\" font-family=\"serif\">\n" +
                "                                <![CDATA[" + s.getSite().getMerchant().getAddress() + "]]>\n" +
                "                            </fo:block>\n" +
                "                        </fo:table-cell>\n" +
                "\n" +
                "                        <fo:table-cell>\n" +
                "                            <fo:block font-weight=\"bold\"  font-size=\"10pt\" font-family=\"serif\">\n" +
                "                                Currency:\n" +
                "                            </fo:block>\n" +
                "                        </fo:table-cell>\n" +
                "\n" +
                "                        <fo:table-cell>\n" +
                "                            <fo:block font-weight=\"normal\" font-size=\"10pt\" font-family=\"serif\">\n" +
                "                                " + s.getCurrency().toString() + "\n" +
                "                            </fo:block>\n" +
                "                        </fo:table-cell>\n" +
                "                    </fo:table-row>\n" +
                "\n" +
                "                </fo:table-body>\n" +
                "            </fo:table>\n" +
                "        </fo:flow>\n" +
                "    </fo:page-sequence>\n" +
                "</fo:root>");


        FopFactory fopFactory = FopFactory.newInstance();
        TransformerFactory tFactory = TransformerFactory.newInstance();

        try {
            resp.setContentType("application/pdf");
            Fop fop = fopFactory.newFop("application/pdf", resp.getOutputStream());
            Transformer transformer = tFactory.newTransformer();
            Source src = new StreamSource(new ByteArrayInputStream(sb.toString().getBytes("ISO-8859-1")));
            Result res = new SAXResult(fop.getDefaultHandler());
            transformer.transform(src, res);
        } catch (Exception e) {
            logger.error("Error of PDf generating: " + e.getLocalizedMessage());
        }
    }