Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
Sonal Sinha
How to generate Reports in Liferay 6.1
March 22, 2013 1:33 AM
Answer

Sonal Sinha

Rank: New Member

Posts: 11

Join Date: January 16, 2013

Recent Posts

I want to generate a pdf on click of a button in a portlet. The portlet shows information in table form i want to export that data to pdf.
Please help me how to proceed further.
I am attaching a screen shot of portlet for the reference.
Attachment

Attachments: export to pdf.png (11.9k)
Subhash Pavuskar
RE: How to generate Reports in Liferay 6.1
March 22, 2013 2:02 AM
Answer

Subhash Pavuskar

Rank: Regular Member

Posts: 234

Join Date: March 12, 2012

Recent Posts

Hi,
Try using Jasper Report !! Click Here
Ravi Teja
RE: How to generate Reports in Liferay 6.1
March 22, 2013 4:39 AM
Answer

Ravi Teja

Rank: New Member

Posts: 21

Join Date: July 9, 2012

Recent Posts

Sonal Sinha:
I want to generate a pdf on click of a button in a portlet. The portlet shows information in table form i want to export that data to pdf.
Please help me how to proceed further.
I am attaching a screen shot of portlet for the reference.


Hi Sonal,

Check this url.

may be this will help to you.

I implemented and generated the pdf using this(i also done on click event giving pop-window and ask for download)

Here i am attaching my Sample Report-generation portlet.
Plz find attachment.(i done in 6.0)
Attachments: pdfgeneration-portlet.rar (9,283.4k)
Meera Prince
RE: How to generate Reports in Liferay 6.1
March 22, 2013 6:46 AM
Answer

Meera Prince

Rank: Liferay Legend

Posts: 1110

Join Date: February 8, 2011

Recent Posts

Hi
Use flying saucer library so that you can directly send html then that will be converted as PDF.

http://today.java.net/pub/a/today/2007/06/26/generating-pdfs-with-flying-saucer-and-itext.html

https://code.google.com/p/flying-saucer/wiki/FAQ

http://flyingsaucerproject.github.com/flyingsaucer/r8/guide/users-guide-R8.html


Regards,
Meera Prince
Kalai Arasan
RE: How to generate Reports in Liferay 6.1
October 1, 2013 1:33 AM
Answer

Kalai Arasan

Rank: Regular Member

Posts: 103

Join Date: January 2, 2013

Recent Posts

Hi!
I tried this pdfgeneration portlet for generate pdf nothing is working. Please tell me what is wrong?
Sharad Sinha
RE: How to generate Reports in Liferay 6.1
October 3, 2013 2:08 AM
Answer

Sharad Sinha

Rank: Junior Member

Posts: 44

Join Date: March 7, 2013

Recent Posts

Hi Sonal,

Try with this code and let me know it is working or not..

Write this code in your java class and call this java method when you click on export PDF button.


Thanks & Regards
Sharad Sinha
Attachments: GeneratePdf.java (0.8k), itext-2.1.7.jar (1,103.6k)
Meera Prince
RE: How to generate Reports in Liferay 6.1
October 3, 2013 6:05 AM
Answer

Meera Prince

Rank: Liferay Legend

Posts: 1110

Join Date: February 8, 2011

Recent Posts

Sonal Sinha:
I want to generate a pdf on click of a button in a portlet. The portlet shows information in table form i want to export that data to pdf.
Please help me how to proceed further.
I am attaching a screen shot of portlet for the reference.



http://www.liferaysavvy.com/2013/09/liferay-pdf-generation-from-html-using.html
Mohammad Azharuddin
RE: How to generate Reports in Liferay 6.1
November 13, 2013 9:58 PM
Answer

Mohammad Azharuddin

Rank: Liferay Master

Posts: 500

Join Date: September 17, 2012

Recent Posts

Hi when i try ur code i can able to save pdf file in my machine and read it ..but when i try to download i getting below error .....PLEASE HELP

Adobe Reader could not open 'test(1).pdf' because it is either not a supported file type or because the file has been damaged (for example. it was sent as an email attachment and wasn't correctly decoded).



String sb= "<html><body> This is my Project </body></html>";
ByteArrayOutputStream baos = new ByteArrayOutputStream();
OutputStream file = new FileOutputStream(new File("E:\\Test.pdf"));
Document document = new Document();
if i try PdfWriter writer = PdfWriter.getInstance(document, file);// file is saving in machine can able to read...unable read downloaded file
if i try replace above line with this PdfWriter writer = PdfWriter.getInstance(document, baos );// file is saving in machine ,not able to read from both location
document.open();
InputStream is = new ByteArrayInputStream(sb.toString().getBytes());
XMLWorkerHelper xmlWorkerHelper=XMLWorkerHelper.getInstance();
// XMLWorkerHelper.getInstance().parseXHtml(writer, document, is);
xmlWorkerHelper.parseXHtml(writer, document, new StringReader(sb.toString()));

resourceResponse.setContentType("application/pdf");
resourceResponse.setProperty(HttpHeaders.CONTENT_DISPOSITION,
"attachement;filename=test");
resourceResponse.addProperty(HttpHeaders.CACHE_CONTROL,
"max-age=3600, must-revalidate");
resourceResponse.setContentLength(baos.size());
OutputStream out = (OutputStream) resourceResponse.getPortletOutputStream();
baos.writeTo(out);
out.flush();
out.close();
document.close();
file.close();
Mohammad Azharuddin
RE: How to generate Reports in Liferay 6.1
November 13, 2013 8:22 AM
Answer

Mohammad Azharuddin

Rank: Liferay Master

Posts: 500

Join Date: September 17, 2012

Recent Posts

meera prince:
Sonal Sinha:
I want to generate a pdf on click of a button in a portlet. The portlet shows information in table form i want to export that data to pdf.
Please help me how to proceed further.
I am attaching a screen shot of portlet for the reference.



http://www.liferaysavvy.com/2013/09/liferay-pdf-generation-from-html-using.html


Please make sure that what code u have given works
Mohammad Azharuddin
RE: How to generate Reports in Liferay 6.1
November 13, 2013 11:16 PM
Answer

Mohammad Azharuddin

Rank: Liferay Master

Posts: 500

Join Date: September 17, 2012

Recent Posts

Hi Guys

Any updates
Mohammad Azharuddin
RE: How to generate Reports in Liferay 6.1
November 14, 2013 4:19 AM
Answer

Mohammad Azharuddin

Rank: Liferay Master

Posts: 500

Join Date: September 17, 2012

Recent Posts

This is working


String html= "<html><body> This is my Project </body></html>";
ByteArrayOutputStream baos = new ByteArrayOutputStream();
OutputStream portletOutputStream=null;
Document document=null;
try {
document=new Document();
PdfWriter pdfWriter=PdfWriter.getInstance(document, baos);
document.open();
XMLWorkerHelper xmlWorkerHelper=XMLWorkerHelper.getInstance();
xmlWorkerHelper.parseXHtml(pdfWriter, document, new StringReader(html.toString()));
document.close();
resourceResponse.setContentType("application/pdf");
resourceResponse.setProperty(HttpHeaders.CONTENT_DISPOSITION,
"attachement;filename=test");
resourceResponse.addProperty(HttpHeaders.CACHE_CONTROL,
"max-age=3600, must-revalidate");
resourceResponse.setContentLength(baos.size());
portletOutputStream = (OutputStream) resourceResponse.getPortletOutputStream();
baos.writeTo(portletOutputStream);
portletOutputStream.flush();

} catch (Exception e) {
_log.error(e.getMessage());
}
finally{
baos.close();
portletOutputStream.close();
}
Attachments: itextpdf-5.4.2.jar (1,879.0k), xmlworker-5.4.4.jar (222.3k)
asif aftab
RE: How to generate Reports in Liferay 6.1
November 27, 2013 3:31 AM
Answer

asif aftab

Rank: Junior Member

Posts: 95

Join Date: September 2, 2013

Recent Posts

This code is working too


In this application I am first take data from a table studentdetails and then printing its value to pdf.
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 the attach .jar file in your lib file manually
My action class
  1
  2package 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

 1
 2<%@ 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">
13</form>
Attachments: itext-1.1.4.jar (1,294.7k)
Meera Prince
RE: How to generate Reports in Liferay 6.1
November 27, 2013 5:55 PM
Answer

Meera Prince

Rank: Liferay Legend

Posts: 1110

Join Date: February 8, 2011

Recent Posts

Hi

Go through following link..

Use JTidy with flying saucer to generate pdf from HTML..

Use following portlet jar files then problem will be solved....

http://www.liferaysavvy.com/2013/11/export-journal-content-as-pdf-in-liferay.html
Subhash patel
Forget password is not working
November 27, 2013 10:45 PM
Answer

Subhash patel

Rank: New Member

Posts: 1

Join Date: February 13, 2013

Recent Posts

Hello everyone my forget password is not working plz give me any suggestion.......
Mohammad Azharuddin
RE: Forget password is not working
November 29, 2013 2:29 AM
Answer

Mohammad Azharuddin

Rank: Liferay Master

Posts: 500

Join Date: September 17, 2012

Recent Posts

Hi Subhash patel

Please post it as new thread... read this