*Contact.java*

import java.io.InputStream;

import org.apache.tapestry5.StreamResponse;

import com.kids.crm.reports.pdf.PDFGenerator;
import com.kids.crm.reports.pdf.PDFStreamResponse;



public class Contact
{
        public StreamResponse onSubmit() {
        // Create PDF
        InputStream is = PDFGenerator.generatePDF("This is the content of a
Dynamically Generated PDF");
        // Return response
        return new PDFStreamResponse(is,"MyDynamicSample");
    }
}



*Contact.tml*
<html t:type="layout" title="Contact com.kids.crm"
      xmlns:t="http://tapestry.apache.org/schema/tapestry_5_1_0.xsd";
      xmlns:p="tapestry:parameter">

    <p>Contact com.kids.crm ...</p>
<t:form t:id="doReportsForm" action="reportsForm" target="_blank">

       <t:submit t:id="onSubmit" name="Run Report" value="Run Report" />

</t:form>
</html>


PDFGenerator.java

package com.kids.crm.reports.pdf;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;

public class PDFGenerator {
        public static InputStream generatePDF(String teststring) {
                // step 1: creation of a document-object
                Document document = new Document();

                System.out.println("\n\n\n\n\ntold ya 1\n\n\n\n");
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                try {
                        // step 2:
                        // we create a writer that listens to the document
                        // and directs a PDF-stream to a file
                        PdfWriter writer = PdfWriter.getInstance(document, 
baos);
                        // step 3: we open the document
                        document.open();
                        // step 4: we add a paragraph to the document
                        document.add(new Paragraph(teststring));
                } catch (DocumentException de) {
                        System.err.println(de.getMessage());
                }
                // step 5: we close the document
                document.close();
                ByteArrayInputStream bais = new 
ByteArrayInputStream(baos.toByteArray());
                return bais;
        }

}


*PDFStreamResponse.java*

package com.kids.crm.reports.pdf;

import java.io.IOException;
import java.io.InputStream;

import org.apache.tapestry5.StreamResponse;
import org.apache.tapestry5.services.Response;

public class PDFStreamResponse implements StreamResponse{
        private InputStream is;
    private String filename="default";

    public PDFStreamResponse(InputStream is, String... args) {
            this.is = is;
            if (args != null) {
                    this.filename = args[0];
            }
    }

    public String getContentType() {
            return "application/pdf";
    }

    public InputStream getStream() throws IOException {
            return is;
    }

    public void prepareResponse(Response arg0) {
            arg0.setHeader("Content-Disposition", "attachment; filename="
                            + filename + ".pdf");
    }


}





--
View this message in context: 
http://tapestry.1045711.n5.nabble.com/fault-found-tp5026068p5026121.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tapestry.apache.org
For additional commands, e-mail: users-h...@tapestry.apache.org

Reply via email to