« 返回到 Development

Generate PDF File in Portlet

Introduction #

Liferay has a "Reports" portlet, which is a full-fledged report application. It's cool!

Sometimes a client may ask for a PDF file for simple reporting purposes. This can be a small portlet using a ResourceURL. The ResourceURL is as specified in JSR-286.

This method uses iText for generating PDF's. The AGPL that governs iText does not allow its use for closed-source projects. Use Liferay's OpenOffice/LibreOffice integration for these type projects.

Environment #

  • Liferay 5.1.2
  • Tomcat 6.0
  • MySQL

JSP Side #

<input type="button" value="Submit"
    onClick="location.href = '<portlet:resourceURL><portlet:param name="reportType" value="pdf" /></portlet:resourceURL>'" />

Portlet Java Code #

public void serveResource(ResourceRequest req, ResourceResponse res)
        throws PortletException, IOException {

        String rType = ParamUtil.getString(req, "reportType");
        
        HttpServletRequest request = PortalUtil.getHttpServletRequest(req);
        
        HttpSession session = request.getSession();
        
        ThemeDisplay themeDisplay = (ThemeDisplay)req.getAttribute(WebKeys.THEME_DISPLAY);
        
        Company company = themeDisplay.getCompany();
        
        if(rType != null && rType.equals("pdf")) {
        	
        	try {                
                    String msg = "Latest Weather Report";
                    
                    Document document = new Document();
                    
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    
                    PdfWriter.getInstance(document, baos);
                    
                    document.open();
                    document.add(new Paragraph(msg));
                    document.add(Chunk.NEWLINE);
                    document.add(new Paragraph("It is a sunny day today."));
                    document.close();
                    
                    res.setContentType("application/pdf");
                    res.addProperty(HttpHeaders.CACHE_CONTROL, "max-age=3600, must-revalidate");
                    
                    res.setContentLength(baos.size());
                    
                    OutputStream out = res.getPortletOutputStream();
                    baos.writeTo(out);
                    
                    out.flush();
                    out.close();
        	} catch (Exception e2) {
                    e2.System.out.println("Error in " + getClass().getName() + "\n" + e2);
        	}
        }
        else {
            try {
                Workbook wb = new HSSFWorkbook();
                
                CreationHelper createHelper = wb.getCreationHelper();
                
                Sheet sheet = wb.createSheet("new sheet");

                Row row = sheet.createRow((short)0);
                Cell cell = row.createCell(0);
                cell.setCellValue(1);

                row.createCell(1).setCellValue(1.2);
                row.createCell(2).setCellValue(
                createHelper.createRichTextString("This is a string"));
                row.createCell(3).setCellValue(true);

                res.setContentType("application/vnd.ms-excel");
                res.addProperty(
                HttpHeaders.CACHE_CONTROL, "max-age=3600, must-revalidate");

                OutputStream out = res.getPortletOutputStream();
                wb.write(out);
                out.flush();
                out.close();
            } catch (Exception e) {
                System.out.println("Exception occurred ...");
            }
        }
}

Future Enhancement #

(1) Generate an Excel report

0 附件
87450 查看
平均 (3 票)
满分为 5,平均得分为 4.33333333333333。
评论
讨论主题回复 作者 日期
Hi I just want to know what jar files I need... Mohammed Azam 2012年11月5日 上午5:00
The PDF generation seems to use iText PDF... Adam Victor Nazareth Brandizzi 2012年11月5日 上午5:01
good morning, I would ask how I can save the pdf? josmar javier Alarcon Bothia 2013年2月11日 上午6:08
Thanks for Sharing.it is working. we need to... Ravi Teja 2013年3月21日 上午5:02
Please follow this link to get sample portlet... Ravi Teja 2013年3月22日 上午5:13
Ravi Teja your link is not working djo vitch 2013年6月6日 上午3:25
Hi Vitch, Please go to below link. and there... Ravi Teja 2013年10月1日 下午10:51
How to display the values in table instead of... Kalai Arasan 2013年9月30日 上午5:16
Hi Kalai arasan, Please go throw this link... Ravi Teja 2013年10月1日 下午10:47
I wants to know the jar files needs to be... Kalai Arasan 2013年11月19日 下午10:30
PDF generation is working fine. Bur i wants to... Kalai Arasan 2013年11月19日 下午10:32
I wants to know either i create the class for... Kalai Arasan 2013年11月19日 下午10:35
Hi! i used the code to generate pdf that works... Kalai Arasan 2013年12月1日 下午11:38
We can refresh portlet after download file?... Vasily Polyakov 2014年2月17日 上午4:27
hi How can i extract lpkg file to see war file veeresh hosamani 2014年4月11日 上午1:46

Hi

I just want to know what jar files I need to import packages.
在 12-11-5 上午5:00 发帖。
The PDF generation seems to use iText PDF (http://itextpdf.com/) and the other report seems to be generated by Apache POI (http://poi.apache.org/).
在 12-11-5 上午5:01 发帖以回复 Mohammed Azam
good morning, I would ask how I can save the pdf?
在 13-2-11 上午6:08 发帖。
Thanks for Sharing.it is working.
we need to add i text jar in lib.
在 13-3-21 上午5:02 发帖以回复 josmar javier Alarcon Bothia
Please follow this link to get sample portlet
.it will open in popwindow as well as ask for download.
(http://www.liferay.com/community/forums/-/message_boards/message/22909­051)
在 13-3-22 上午5:13 发帖以回复 Ravi Teja
Ravi Teja your link is not working
在 13-6-6 上午3:25 发帖以回复 Ravi Teja
How to display the values in table instead of paragraph in pdf?
在 13-9-30 上午5:16 发帖。
Hi Kalai arasan,

Please go throw this link (http://tutorials.jenkov.com/java-itext/table.html).
在 13-10-1 下午10:47 发帖以回复 Kalai arasan
Hi Vitch,

Please go to below link. and there you can find ( pdfgeneration-portlet.rar )
https://www.liferay.com/community/forums/-/message_boards/message/22903036
在 13-10-1 下午10:51 发帖以回复 djo vitch
I wants to know the jar files needs to be included!
在 13-11-19 下午10:30 发帖。
PDF generation is working fine. Bur i wants to generate excel sheet that's the problem arise with import files because of missing jar files.
在 13-11-19 下午10:32 发帖。
I wants to know either i create the class for Workbook wb = new HSSFWorkbook(); or import file for Workbook and HSSFWorkbook.
在 13-11-19 下午10:35 发帖以回复 Kalai arasan
Hi! i used the code to generate pdf that works fine, But both pdf and excel will appears in the name of the site page automatically. How can i give the name for documents manually.
在 13-12-1 下午11:38 发帖。
We can refresh portlet after download file? File downloaded successfully, but submit button after this look like pressed. Thanks.
在 14-2-17 上午4:27 发帖。
hi
How can i extract lpkg file to see war file
在 14-4-11 上午1:46 发帖。