Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
Emilio Jose Lamas Fraga
Add "export to PDF/Excel" button in custom portlet
January 2, 2012 7:54 AM
Answer

Emilio Jose Lamas Fraga

Rank: Junior Member

Posts: 58

Join Date: November 29, 2011

Recent Posts

Hello everyone!

I´m facing a problem trying to implement a "export to PDF" button in a custom portlet.

I was trying to copy the logical from the Web Content Display portlet (i have an OpenOffice server running and the button works well) but the problem is
that in my portlet i cannot call the action that generates the PDF (com.liferay.portlet.journalcontent.action.ExportArticleAction) because it's in portal-impl, but not in portal-service. I cannot copy the method that creates the PDF neither for the same reason (it uses several imports i can't reach from my portlet)

Any ideas of how to do that?

And -being more general- How can you use in your custom portlets code from packages which are in portal-impl.jar, but not in portal-service.jar?

Thanks in advance!
Jelmer Kuperus
RE: Add "export to PDF/Excel" button in custom portlet
January 2, 2012 11:51 AM
Answer

Jelmer Kuperus

Rank: Liferay Legend

Posts: 1192

Join Date: March 10, 2010

Recent Posts

ExportArticleAction uses com.liferay.portlet.documentlibrary.util.DocumentConversionUtil but that same class can also be accessed via com.liferay.portal.kernel.util.DocumentConversionUtil which is in portal-service.jar. So available to plugins

Additionally you can use PortalClassInvoker to call methods on classes in portal-impl.jar, but that is considered bad style. though sometimes unavoidable
Emilio Jose Lamas Fraga
RE: Add "export to PDF/Excel" button in custom portlet
January 3, 2012 4:03 AM
Answer

Emilio Jose Lamas Fraga

Rank: Junior Member

Posts: 58

Join Date: November 29, 2011

Recent Posts

Hi Jelmer!

That was helpful, thank you so much!, but I´m afraid I'm still stuck...
At this moment, I'm just trying to generate a PDF that contains a simple line of text. I have the next code on my portlet action:

 1
 2String cadena = "test";
 3StringBundler sb = new StringBundler(12);
 4sb.append("<html>");
 5sb.append("<head>");
 6sb.append("<meta content=\"");
 7sb.append(ContentTypes.TEXT_HTML_UTF8);
 8sb.append("\" http-equiv=\"content-type\" />");
 9sb.append("<base href=\"");
10sb.append(themeDisplay.getPortalURL());
11sb.append("\" />");
12sb.append("</head>");
13sb.append("<body>");
14sb.append(cadena);
15sb.append("</body>");
16sb.append("</html>");
17HttpServletRequest servletRequest = PortalUtil.getHttpServletRequest(request);
18HttpServletResponse servletResponse = PortalUtil.getHttpServletResponse(response);
19InputStream is = new UnsyncByteArrayInputStream(sb.toString().getBytes(StringPool.UTF8));
20FileInputStream fis = (FileInputStream) DocumentConversionUtil.convert(cadena, is, "html","pdf");
21response.setWindowState(LiferayWindowState.EXCLUSIVE);
22String fileName = "name.pdf";
23String contentType = MimeTypesUtil.getContentType(fileName);
24ServletResponseUtil.sendFile(servletRequest, servletResponse,fileName, fis,contentType);


I'm not sure if the StringBuilder structure is necessary, but i'm just following ExportArticleAction, from the journalcontent Portlet.
No errors in console, but the PDF file it's not created. Instead of that, the browser shows a new file full of unencoding characters.
If I change the extension from "pdf" to "txt" in the previous lines like this:

1
2FileInputStream fis = (FileInputStream) DocumentConversionUtil.convert(cadena, is, "html","txt");
3String fileName = "name.txt";


A TXT file is created properly, but containing the HTML code of the portlet which i'm calling the action. No signs from the content of the StringBuilder.


Any ideas? What i'm doing wrong?
Ben Carson
RE: Add "export to PDF/Excel" button in custom portlet
October 30, 2012 6:44 AM
Answer

Ben Carson

Rank: Junior Member

Posts: 25

Join Date: January 9, 2012

Recent Posts

Hi Emilio,
Did you ever get this figured out? If you did, posting your solution here would be really helpful.
Thanks,

Ben

UPDATE: well I figured out what I was doing wrong. I didn't have OpenOffice services running:
https://www.liferay.com/documentation/liferay-portal/6.1/user-guide/-/ai/openoffi-1
Harish Kumar
RE: Add "export to PDF/Excel" button in custom portlet
October 30, 2012 7:17 AM
Answer

Harish Kumar

Rank: Expert

Posts: 472

Join Date: July 31, 2010

Recent Posts

I have done this using InputStream instead of FileInputStream like this -

1
2InputStream convertedIS=null;
3            try {
4                convertedIS = DocumentConversionUtil.convert("12345", is, sourceExtension, targetExtension);
5            } catch (Exception e) {
6                // TODO Auto-generated catch block
7                e.printStackTrace();
8            }