Wednesday 27 November 2013

Creation of pdf by using liferay portal

In this application I am creating a pdf by using data from table namely studentdetails
Description of table
table name: studentdetails
column name:
FatherName, Emailid, firstlanguage, lastschoolname, mothername, nationality, religion, secondlanguage, sex, stream, studentid, date of birth, sname
Please add
itext-1.1.4.jar
 file in your lib directory manually"

My action class

package com.test.pdf;
  3
  4import java.io.IOException;
  5
  6import javax.portlet.PortletException;
  7import javax.portlet.PortletSession;
  8import javax.portlet.ResourceRequest;
  9import javax.portlet.ResourceResponse;
 10
 11import pdf.ashraf.userdata.model.studentDetails;
 12import pdf.ashraf.userdata.service.studentDetailsLocalServiceUtil;
 13
 14
 15import com.lowagie.text.Anchor;
 16import com.lowagie.text.DocumentException;
 17import com.lowagie.text.Font;
 18import com.lowagie.text.FontFactory;
 19import com.lowagie.text.PageSize;
 20import com.lowagie.text.Paragraph;
 21import com.lowagie.text.Phrase;
 22import com.lowagie.text.Rectangle;
 23import com.lowagie.text.pdf.CMYKColor;
 24import com.lowagie.text.pdf.PdfPCell;
 25import com.lowagie.text.pdf.PdfPTable;
 26import com.lowagie.text.pdf.PdfWriter;
 27import java.io.ByteArrayOutputStream;
 28import java.io.File;
 29import java.io.FileOutputStream;
 30import java.io.FileWriter;
 31import java.io.IOException;
 32import java.io.OutputStream;
 33import java.io.PrintWriter;
 34import java.io.StringReader;
 35import java.util.ArrayList;
 36import java.util.Iterator;
 37import java.util.List;
 38
 39import com.liferay.portal.kernel.exception.PortalException;
 40import com.liferay.portal.kernel.exception.SystemException;
 41import com.liferay.portal.kernel.servlet.HttpHeaders;
 42import com.liferay.portal.kernel.util.Base64;
 43import com.liferay.portal.kernel.util.StringPool;
 44import com.liferay.util.bridges.mvc.MVCPortlet;
 45
 46
 47public class PdfAsfrafPortlet extends MVCPortlet
 48{
 49    public void serveResource(ResourceRequest request, ResourceResponse response)
 50        throws PortletException, IOException
 51            {
 52               
 53                try
 54                {
 55                    createPDF(request,response);
 56                }
 57                catch (SystemException e)
 58                {                   
 59                    e.printStackTrace();
 60                }
 61            }
 62   
 63public void createPDF(ResourceRequest resourceRequest, ResourceResponse resourceResponse)
 64    throws IOException, PortletException, SystemException
 65        {
 66        try
 67          {
 68           resourceRequest.setCharacterEncoding(StringPool.UTF8);                
 69           com.lowagie.text.Document document = new com.lowagie.text.Document();    //blank pdf created
 70           ByteArrayOutputStream baos = new ByteArrayOutputStream();
 71           PdfWriter.getInstance(document, baos);                        //predefine class PdfWriter calls static method getInstance
 72           document.open();                                                //open pdf in write method
 73           PdfPTable table= new PdfPTable(13);                            //creating a pdf table having single column hence PdfPTable table= new PdfPTable(1);
 74           table.setWidthPercentage(100);                                //setting width size                                            //to design cell
 75           table.addCell("FatherName");        //adding value in row. here number of rows is depends on the number of      table.addcell();
 76           table.addCell("Emailid");
 77           table.addCell("firstlanguage");
 78           table.addCell("lastschoolname");
 79           table.addCell("mothername");                                           //adding value in row
 80           table.addCell("nationality");
 81           table.addCell("religion");
 82           table.addCell("secondlanguage");
 83           table.addCell("gender");
 84           table.addCell("stream");
 85           table.addCell("studentid");
 86           table.addCell("date of birth");
 87           table.addCell("sname");
 88          
 89            List<studentDetails> tbl=null;
 90            tbl=studentDetailsLocalServiceUtil.findWholeTbl();       //to fetch whole table
 91            if(tbl!=null)
 92            {
 93            System.out.println("tbl size:  "+tbl.size());
 94            Iterator<studentDetails> it=tbl.iterator();
 95            while(it.hasNext())
 96            {
 97                studentDetails obj=it.next();
 98               
 99                table.addCell(obj.getFatherName());
100                table.addCell(obj.getEmailId());
101                table.addCell(obj.getFirstLanguage());
102                table.addCell(obj.getLastSchoolName());
103                table.addCell(obj.getMotherName());
104                table.addCell(obj.getNationality());
105                table.addCell(obj.getReligion());
106                table.addCell(obj.getSecondLanguage());
107                table.addCell(obj.getSex());
108                table.addCell(obj.getStream());
109                long l=obj.getStudentId();
110                String sl=String.valueOf(l);
111                table.addCell(sl);
112                String sd=String.valueOf(obj.getDateOfBirth());
113                table.addCell(sd);
114                table.addCell(obj.getSName());
115               
116               
117            }//while
118            }//if
119          
120           document.add(table);                                            //adding table to created pdf document
121           document.close();                                            //we have to first close the document
122           String fileName="attachment;filename=ashraf.pdf";            //filename
123           resourceResponse.setContentType("application/pdf");            //setting the content type either application or pdf(Portable Document Format)
124           resourceResponse.addProperty(HttpHeaders.CONTENT_DISPOSITION, fileName);    //
125           OutputStream out = resourceResponse.getPortletOutputStream();
126           byte[] downloadBytes = Base64.decode((String) resourceRequest.getAttribute("fileToDownloadBase64"));
127           out.write(downloadBytes);
128           baos.writeTo(out);
129           out.flush();
130           out.close();
131          }
132          catch (Exception e)
133          {
134           e.printStackTrace();
135          }
136         }//createPdf
137}//end of class

and my view.jsp 

<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
 3
 4<portlet:defineObjects />
 5
 6This is the <b>Pdf Asfraf Portlet</b> portlet in View mode.
 7<portlet:resourceURL var="submitFormDetailsResourceURL" id="appform" escapeXml="false"/>
 8<br><br><br>
 9<form action="<%=submitFormDetailsResourceURL.toString() %>" method="post">
10<h2>This project working fine</h2>
11<p>Here I am retreiving data from table asif_studentDetails and then just publish that data in pdf format
12<input type="submit" value="Download" name="submitbutton">

Thanks and regards
asif aftab

No comments:

Post a Comment