بلاگ

آخرین خبر ها و آپدیت ها

تولید فایل PDF با استفاده از اوراکل ADF

Generating PDF file or any sort of file is a common requirement among web portal applications. Generally, the requirement could be getting some data from a database and writing the same on to the file.

Oracle ADF (Application Development Framework) is a fast, simple and rich internet application development framework. Oracle ADF Faces Components are Ajax-enabled components that help in building a rich web user interface for Java EE applications.

When it comes to reports in Oracle ADF, the framework supports a feature called “Export to Excel.” The component supporting that feature isaf:exportCollectionActionListener. But unfortunately, even though it’s a sophisticated tool for designing and developing web applications, it doesn’t have a feature for generating dynamic PDF files. This post will help you understand how to generate PDF and download it on a command button click.

Requirements:

1. Generate some static data PDF file on a command button click.

2. Download the same file to the specified location.

Process:

1. Create a new project.

Here we use Jdeveloper 11g Release (11.1.1.3.0) version. In order to create a new project click on option File > New option in the pane.  The dialog appears as below and select Fusion Web Application (ADF).

PDF20using20Oracle20ADF201-resized-600.jpg

Once you create a new Application, JDeveloper creates you two projects as shown in the below screen shot.

PDF20using20Oracle20ADF202-resized-600.jpg

2. Create a UI Page:

Our goal is to generate a PDF file when clicked on a command button and download the generated file. Now to have that provision we need a UI page with a command button. So we shall create a UI page with simple Command Button.

Step 1: Right on View Controller project, select New option and then JSF page under JSF category.

PDF20using20Oracle20ADF203-resized-600.jpg

Step 2: create a Jspx page as shown below.

PDF20using20Oracle20ADF204-resized-600.jpg

Note: Make sure you check Create as XML Document option.

Step 3: Once you are done with creating the Jspx file, drag and drop the Panel Box and Command Button from the Common Components palate. The page looks like this:

PDF20using20Oracle20ADF205-resized-600.jpg

Step 4: Drag and drop fileDownloadActionListener on to the command button from the Component Palate.  And change properties as shown in the below screen.

PDF20using20Oracle20ADF206-resized-600.jpg

Properties:

PDF20using20Oracle20ADF207-resized-600.jpg

Where Filename is the output file name and Method is the Bean method call when clicked on button.

Source: 

PDF20using20Oracle20ADF208-resized-600.jpg

3. Create a Bean classes:

To generate a PDF and download the file we need some Java program which runs at the server side and provides the desired results. To make this happen we need a Jar file called IText. In this sample, we used ITextPdf-5.1.3.jar.

Step 1: In order to reflect the classes in the jar file which helps in generating PDF, we need to import the jar file. To import jar file, Click on ViewController > ProjectProperties > Libraries and Classpath > Add Jar/ Directory and import from the location as shown in the below screen shot.

PDF20using20Oracle20ADF209-resized-600.jpg

Step2: Create a two bean (java) classes, where one class contains all the Accessors and another class contains the PDF generation and Downloading methods.

PDF20using20Oracle20ADF2010-resized-600.jpg

4. Create download the generated file methods.

PDFGeneratorBean.java source code:

package winne.filegeneration.view.backing;</p>
<p>import java.io.File;</p>
<p>import java.io.FileInputStream;</p>
<p>import java.io.IOException;</p>
<p>import java.io.OutputStream;</p>
<p>import javax.faces.application.FacesMessage;</p>
<p>import javax.faces.context.ExternalContext;</p>
<p>import javax.faces.context.FacesContext;</p>
<p>import javax.servlet.ServletContext;</p>
<p>import javax.servlet.http.HttpServletResponse;</p>
<p>import oracle.adf.view.rich.component.rich.RichDocument;</p>
<p>import oracle.adf.view.rich.component.rich.RichForm;</p>
<p>import oracle.adf.view.rich.component.rich.layout.RichPanelBox;</p>
<p>import oracle.adf.view.rich.component.rich.layout.RichPanelGroupLayout;</p>
<p>import oracle.adf.view.rich.component.rich.layout.RichPanelStretchLayout;</p>
<p>import oracle.adf.view.rich.component.rich.nav.RichCommandButton;</p>
<p>public class PDFGeneratorBean {</p>
<p>private RichPanelStretchLayout psl1;</p>
<p>private RichForm f1;</p>
<p>private RichDocument d1;</p>
<p>private RichCommandButton cb1;</p>
<p>private RichPanelGroupLayout pgl1;</p>
<p>private RichPanelBox pb1;</p>
<p>public void setPsl1(RichPanelStretchLayout psl1) {</p>
<p>this.psl1 = psl1;</p>
<p>}</p>
<p>public RichPanelStretchLayout getPsl1() {</p>
<p>return psl1;</p>
<p>}</p>
<p>public void setF1(RichForm f1) {</p>
<p>this.f1 = f1;</p>
<p>}</p>
<p>public RichForm getF1() {</p>
<p>return f1;</p>
<p>}</p>
<p>public void setD1(RichDocument d1) {</p>
<p>this.d1 = d1;</p>
<p>}</p>
<p>public RichDocument getD1() {</p>
<p>return d1;</p>
<p>}</p>
<p>public void setCb1(RichCommandButton cb1) {</p>
<p>this.cb1 = cb1;</p>
<p>}</p>
<p>public RichCommandButton getCb1() {</p>
<p>return cb1;</p>
<p>}</p>
<p>public void setPgl1(RichPanelGroupLayout pgl1) {</p>
<p>this.pgl1 = pgl1;</p>
<p>}</p>
<p>public RichPanelGroupLayout getPgl1() {</p>
<p>return pgl1;</p>
<p>}</p>
<p>public void setPb1(RichPanelBox pb1) {</p>
<p>this.pb1 = pb1;</p>
<p>}</p>
<p>public RichPanelBox getPb1() {</p>
<p>return pb1;</p>
<p>}</p>
<p>public void addMessage(FacesMessage.Severity type, String message) {</p>
<p>FacesContext fctx = FacesContext.getCurrentInstance();</p>
<p>FacesMessage fm = new FacesMessage(type, message, null);</p>
<p>fctx.addMessage(null, fm);</p>
<p>}</p>
<p>}</p>
<p>DownloadFileBean.java source code:</p>
<p>package winne.filegeneration.view.backing;</p>
<p>import com.itextpdf.text.BaseColor;</p>
<p>import com.itextpdf.text.Document;</p>
<p>import com.itextpdf.text.DocumentException;</p>
<p>import com.itextpdf.text.Font;</p>
<p>import com.itextpdf.text.Paragraph;</p>
<p>import com.itextpdf.text.pdf.PdfWriter;</p>
<p>import java.io.File;</p>
<p>import java.io.FileInputStream;</p>
<p>import java.io.FileOutputStream;</p>
<p>import java.text.SimpleDateFormat;</p>
<p>import java.util.Date;</p>
<p>import javax.faces.context.ExternalContext;</p>
<p>import javax.faces.context.FacesContext;</p>
<p>import javax.servlet.ServletContext;</p>
<p>import javax.servlet.http.HttpServletResponse;</p>
<p>import oracle.adf.model.BindingContainer;</p>
<p>import oracle.adf.model.BindingContext;</p>
<p>public class DownloadFileBean {</p>
<p>public DownloadFileBean() {</p>
<p>}</p>
<p>private static String FILE = “c:/temp/Salary_Breakdown_June.pdf”;</p>
<p>private static Font catFont =</p>
<p>new Font(Font.FontFamily.TIMES_ROMAN, 18, Font.BOLD);</p>
<p>private static Font redFont =</p>
<p>new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.NORMAL, BaseColor.RED);</p>
<p>private static Font subFont =</p>
<p>new Font(Font.FontFamily.TIMES_ROMAN, 16, Font.BOLD);</p>
<p>private static Font smallBold =</p>
<p>new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.BOLD);</p>
<p>private BindingContainer bindings;</p>
<p>public void generatePDF(FacesContext facesContext,</p>
<p>java.io.OutputStream outputStream) {</p>
<p>this.generatePDFFile(facesContext, outputStream); // Generate PDF File</p>
<p>this.downloadPDF(facesContext, outputStream); // Download PDF File</p>
<p>}</p>
<p>private void downloadPDF(FacesContext facesContext,</p>
<p>java.io.OutputStream outputStream) {</p>
<p>try {</p>
<p>facesContext = facesContext.getCurrentInstance();</p>
<p>ServletContext context =</p>
<p>(ServletContext)facesContext.getExternalContext().getContext();</p>
<p>ExternalContext ctx = facesContext.getExternalContext();</p>
<p>HttpServletResponse res = (HttpServletResponse)ctx.getResponse();</p>
<p>res.setContentType(“application/pdf”);</p>
<p>System.out.println(context.getRealPath(“/”));</p>
<p>File file = new File(FILE);</p>
<p>FileInputStream fdownload;</p>
<p>byte[] b;</p>
<p>fdownload = new FileInputStream(file);</p>
<p>int n;</p>
<p>while ((n = fdownload.available()) &gt; 0) {</p>
<p>b = new byte[n];</p>
<p>int result = fdownload.read(b);</p>
<p>outputStream.write(b, 0, b.length);</p>
<p>if (result == -1)</p>
<p>break;</p>
<p>}</p>
<p>outputStream.flush();</p>
<p>} catch (Exception e) {</p>
<p>e.printStackTrace();</p>
<p>}</p>
<p>}</p>
<p>private void generatePDFFile(FacesContext facesContext,</p>
<p>java.io.OutputStream outputStream) {</p>
<p>try {</p>
<p>System.out.println(“In Generate PDF…………….”);</p>
<p>Document document = new Document();</p>
<p>PdfWriter.getInstance(document, new FileOutputStream(FILE));</p>
<p>document.open();</p>
<p>addMetaData(document);</p>
<p>addTitlePage(document);</p>
<p>//addContent(document);</p>
<p>document.close();</p>
<p>System.out.println(“End of PDF………………….”);</p>
<p>facesContext = facesContext.getCurrentInstance();</p>
<p>ServletContext context =</p>
<p>(ServletContext)facesContext.getExternalContext().getContext();</p>
<p>System.out.println(context.getRealPath(“/”));</p>
<p>File file = new File(FILE);</p>
<p>FileInputStream fdownload;</p>
<p>byte[] b;</p>
<p>System.out.println(file.getCanonicalPath());</p>
<p>System.out.println(file.getAbsolutePath());</p>
<p>fdownload = new FileInputStream(file);</p>
<p>int n;</p>
<p>while ((n = fdownload.available()) &gt; 0) {</p>
<p>b = new byte[n];</p>
<p>int result = fdownload.read(b);</p>
<p>outputStream.write(b, 0, b.length);</p>
<p>if (result == -1)</p>
<p>break;</p>
<p>}</p>
<p>outputStream.flush();</p>
<p>} catch (Exception e) {</p>
<p>e.printStackTrace();</p>
<p>}</p>
<p>// / return null;</p>
<p>// Add event code here…</p>
<p>}</p>
<p>// iText allows to add metadata to the PDF which can be viewed in your Adobe</p>
<p>// Reader</p>
<p>// under File -&gt; Properties</p>
<p>private static void addMetaData(Document document) {</p>
<p>document.addTitle(“My first PDF”);</p>
<p>document.addSubject(“Using iText”);</p>
<p>document.addKeywords(“Java, PDF, iText”);</p>
<p>document.addAuthor(“Lars Vogel”);</p>
<p>document.addCreator(“Lars Vogel”);</p>
<p>}</p>
<p>private static void addTitlePage(Document document) throws DocumentException {</p>
<p>Date month = new Date();</p>
<p>SimpleDateFormat currentMonth = new SimpleDateFormat(“MMMM”);</p>
<p>Paragraph preface = new Paragraph();</p>
<p>// We add one empty line</p>
<p>addEmptyLine(preface, 1);</p>
<p>// Lets write a big header</p>
<p>preface.add(new Paragraph(“Salary Structure for Month – ” +</p>
<p>currentMonth.format(month), catFont));</p>
<p>document.add(preface);</p>
<p>// Start a new page</p>
<p>// document.newPage();</p>
<p>}</p>
<p>private static void addEmptyLine(Paragraph paragraph, int number) {</p>
<p>for (int i = 0; i &lt; number; i++) {</p>
<p>paragraph.add(new Paragraph(” “));</p>
<p>}</p>
<p>}</p>
<p>public static oracle.binding.BindingContainer getBindings() {</p>
<p>return BindingContext.getCurrent().getCurrentBindingsEntry();</p>
<p>}

5Output.

PDF20using20Oracle20ADF2011-resized-600.jpg

Note: The dialog box pop up appearance depends on browser settings. Make sure that you have “Ask before download” option checked in your browser settings.

Hopefully this post is helpful!

Do you have questions or need clarification? Let me know what you think – or have anything to add to this topic. Post in the comments section or click the button, below, and I’ll get back to you!

Need help? Talk to an expert now!

References:
http://www.vogella.com/articles/JavaPDF/article.html
http://hasamali.blogspot.in/2011/09/download-file-in-oracle-adf-gui.html

royal visionتولید فایل PDF با استفاده از اوراکل ADF
Share this post

نظر خود را وارد کنید:

Related Posts