Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 转换PDF页面上的现有表单_Java_Html_Spring Mvc_Pdf_Struts - Fatal编程技术网

Java 转换PDF页面上的现有表单

Java 转换PDF页面上的现有表单,java,html,spring-mvc,pdf,struts,Java,Html,Spring Mvc,Pdf,Struts,在我的STRUTS MVC JAVA项目中,我在HTML文件中有一个表单。该HTML包含在JSP文件中。我想要的是在SUBMIT附近提供一个按钮,名为“下载为PDF”。当我点击这个下载按钮时,它应该被下载到我的本地机器上,包含所有的值,无论我在表单中填写了什么 请帮忙,因为我对这类问题不太了解 谢谢,您可以在中操作生成PDF并将其发送给客户端。查看 使用此代码作为基础: private void printStatement(Statement s, HttpServletResponse re

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

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


谢谢,

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

查看

使用此代码作为基础:

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());
        }
    }
private void printStatement(语句s,HttpServletResponse resp)引发IOException{
StringBuffer sb=新的StringBuffer();
sb.追加(“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“信用卡处理对账单\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“发件人:\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“+s.getStartDate()+”\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“地址:\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“货币:\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“+s.getCurrency().toString()+”\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“\n”+
"");
FopFactory FopFactory=FopFactory.newInstance();
TransformerFactory tFactory=TransformerFactory.newInstance();
试一试{
分别为setContentType(“应用程序/pdf”);
Fop Fop=fopFactory.newFop(“application/pdf”,resp.getOutputStream());
Transformer Transformer=t工厂.newTransformer();
Source src=new StreamSource(new ByteArrayInputStream(sb.toString().getBytes(“ISO-8859-1”));
Result res=新的SAXSult(fop.getDefaultHandler());
变换(src,res);
}捕获(例外e){
logger.error(“生成PDf的错误:”+e.getLocalizedMessage());
}
}
添加一个按钮,这正是要做的

您只需将以下两行添加到页面:

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

看看: