Aakash Kumar
Aakash Kumar

Reputation: 893

Converting Exisiting Form on page in PDF

In my STRUTS MVC JAVA project, I have a form in a HTML file. That HTML is included in a JSP file. What I want is to provide a button near SUBMIT as "Download as PDF". When I click on this Download button, it should be downloaded to my local machine with all values, whatever I filled in the form.

Please help as I don't have much idea about such type of issues.

Thanks,

Upvotes: 2

Views: 81

Answers (3)

user1914292
user1914292

Reputation: 1556

Add a PDFmyForm button, this does exactly that.

You just add the following two lines to your page:

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

Upvotes: 2

DmitryKanunnikoff
DmitryKanunnikoff

Reputation: 2266

Look at Apache FOP

Use this code as a base:

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());
        }
    }

Upvotes: 0

Petr Fl&#237;dr
Petr Fl&#237;dr

Reputation: 146

You can operatively generate PDF in iText and send it to client.

Upvotes: 0

Related Questions