Foros de discusión

Add "export to PDF/Excel" button in custom portlet

thumbnail
Emilio José Lamas Fraga, modificado hace 12 años.

Add "export to PDF/Excel" button in custom portlet

Junior Member Mensajes: 57 Fecha de incorporación: 29/11/11 Mensajes recientes
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!
thumbnail
jelmer kuperus, modificado hace 12 años.

RE: Add "export to PDF/Excel" button in custom portlet

Liferay Legend Mensajes: 1191 Fecha de incorporación: 10/03/10 Mensajes recientes
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
thumbnail
Emilio José Lamas Fraga, modificado hace 12 años.

RE: Add "export to PDF/Excel" button in custom portlet

Junior Member Mensajes: 57 Fecha de incorporación: 29/11/11 Mensajes recientes
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:


String cadena = "test";
StringBundler sb = new StringBundler(12);
sb.append("");
sb.append("");
sb.append("<meta content="\&quot;&quot;);" sb.append(ContentTypes.TEXT_HTML_UTF8); sb.append("\" http-equiv="\&quot;content-type\&quot;">");
sb.append("<base href="\&quot;&quot;);" sb.append(themeDisplay.getPortalURL()); sb.append("\">");
sb.append("");
sb.append("");
sb.append(cadena);
sb.append("");
sb.append("");
HttpServletRequest servletRequest = PortalUtil.getHttpServletRequest(request);
HttpServletResponse servletResponse = PortalUtil.getHttpServletResponse(response);
InputStream is = new UnsyncByteArrayInputStream(sb.toString().getBytes(StringPool.UTF8));
FileInputStream fis = (FileInputStream) DocumentConversionUtil.convert(cadena, is, "html","pdf");
response.setWindowState(LiferayWindowState.EXCLUSIVE);
String fileName = "name.pdf";
String contentType = MimeTypesUtil.getContentType(fileName);
ServletResponseUtil.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:


FileInputStream fis = (FileInputStream) DocumentConversionUtil.convert(cadena, is, "html","txt"); 
String 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?
thumbnail
Ben Carson, modificado hace 11 años.

RE: Add "export to PDF/Excel" button in custom portlet

Junior Member Mensajes: 25 Fecha de incorporación: 9/01/12 Mensajes recientes
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
thumbnail
Harish Kumar, modificado hace 11 años.

RE: Add "export to PDF/Excel" button in custom portlet

Expert Mensajes: 483 Fecha de incorporación: 31/07/10 Mensajes recientes
I have done this using InputStream instead of FileInputStream like this -


InputStream convertedIS=null;
			try {
				convertedIS = DocumentConversionUtil.convert("12345", is, sourceExtension, targetExtension);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}