Friday, November 22, 2013

Export Journal Content as PDF in Liferay

 

Objective:

Export Journal Content/Web Content  as PDF.

Liferay have feature to export journal content as PDF.

Liferay already have support to export journal content as PDF document but we need to do open office configuration.

Here we are doing without help of open office configuration.

With help of JTidy and Flying Saucer we will export general content as PDF with zero configurations.

Download Export Jorinal Content  portlet from following location

You can find source and war file


Note: 

Portlet developed in Liferay 6.1GA2 EE version
If you want deploy in CE version you just do changes in liferay-plugin-package.properties

Liferay 6.1 EE version

name= ExportJournalContentAsPDF
module-group-id=liferay-ee
module-incremental-version=1
tags=
short-description=
change-log=
page-url=http://www.liferay.com
author=Liferay, Inc.
licenses=EE
portal-dependency-jars=\
    jstl-api.jar,\
    jstl-impl.jar
portal-dependency-tlds=c.tld
liferay-versions=6.1.20


Liferay 6.1 CE version

name = ExportJournalContentAsPDF
module-group-id=liferay
module-incremental-version=1
tags=
short-description=
change-log=
page-url=http://www.liferay.com
author=Liferay, Inc.
licenses=LGPL
portal-dependency-jars=\
    jstl-api.jar,\
    jstl-impl.jar
portal-dependency-tlds=c.tld
liferay-versions=6.1.1

Procedure for deploy portlet:

You can use war file and directly place in your portal deploy folder and test or you can also use source to deploy portlet.

Once portlet is deployed successfully you can see the portlet in sample category name as
Export Jorinal Content.

JTidy:

JTidy  is java based library for cleaning up malformed and faulty HTML and JTidy provides a DOM interface to the document that is being processed, which effectively makes you able to use JTidy as a DOM parser for real-world HTML.

Go through following link to get more information about JTidy.


Flying Saucer:

Flying Saucer is java library to generate PDF from HTML and XML. This is pretty interesting library to generate PDF very easy way even complex PDF too. Generally we can design content in HTML the same thing we can generate as PDF.

Go through following link to get more information about flying saucer


Why we are using JTidy with Flying Saucer?

Generally when we use HTML in flying saucer that should be well formed, if any syntax errors or any other malformed data then it won’t be exported as PDF.

Generally when we generate PDF with help of flying saucer we mostly getting html content as dynamic, so we need to use JTidy to clean HTML, means to correct syntax errors and clean malformed data.

Steps to generate PDF

  1. Get html contents from any sources
  2. Convert html data as Input Stream
  3. Apply JTidy to cleans html data make it as well formed w3c document.
  4. Pass w3c document to flying saucer to generate PDF.


Get html contents from any sources:

First we need to get HTML data from any sources we can use any URL to get html data mean its string of html tags.

Example:

We can manually prepare html data as string or we get html data from any URL.






String html = ""<!DOCTYPE HTML><<html><head><title>First parse</title></head>"
  + "<body><p>This is test HTML.</p></body></html>";


Convert html data as Input Stream:

Now once get html content as String now we need convert string to input stream


String html = ""<!DOCTYPE HTML><<html><head><title>First parse</title></head>"

  + "<body><p>This is test HTML.</p></body></html>";
InputStream is = new ByteArrayInputStream(html.getBytes());



Apply JTidy to cleans html data make it as well formed w3c document


String html = ""<!DOCTYPE HTML><<html><head><title>First parse</title></head>"
  + "<body><p>This is test HTML.</p></body></html>";
InputStream is = new ByteArrayInputStream(articleHtml.getBytes());
Tidy tidy = new Tidy();
org.w3c.dom.Document doc = tidy.parseDOM(is, null);



Pass w3c document to flying saucer to generate PDF



OutputStream outputStream = resourceResponse.getPortletOutputStream();
ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(doc, null);
renderer.layout();
renderer.createPDF(outputStream);



Note:

This process can apply anywhere to generate PDF from HTML using flying saucer.

The following is complete example to export journal content as PDF


import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.portlet.PortletException;
import javax.portlet.ResourceRequest;
import javax.portlet.ResourceResponse;
import com.liferay.portal.kernel.language.LanguageUtil;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.portal.theme.ThemeDisplay;
import com.liferay.portlet.journal.model.JournalArticleDisplay;
import com.liferay.portlet.journalcontent.util.JournalContentUtil;
import com.liferay.util.bridges.mvc.MVCPortlet;
import com.lowagie.text.DocumentException;
import org.w3c.dom.Document;
import org.w3c.tidy.Tidy;
import org.xhtmlrenderer.pdf.ITextRenderer;
public class ExportJorinalContentAction extends MVCPortlet {
            @Override
            public void serveResource(
                                    ResourceRequest resourceRequest, ResourceResponse resourceResponse)
                        throws IOException, PortletException {
                        String articleId=ParamUtil.getString(resourceRequest,"webContentSelectBox");
                        ThemeDisplay themeDisplay = (ThemeDisplay)resourceRequest.getAttribute(WebKeys.THEME_DISPLAY);
                        long groupId =ParamUtil.getLong(resourceRequest,"sitesSelectBox");
                        try {
                        //get journal article
                        JournalArticleDisplay articleDisplay = JournalContentUtil.getDisplay(groupId, articleId, "", LanguageUtil.getLanguageId(resourceRequest),themeDisplay);
                        //set up response to handle PDF
                        resourceResponse.reset();
                        resourceResponse.setContentType("application/pdf");
                        resourceResponse.setProperty("Content-disposition", "attachment; filename=\"" + articleDisplay.getTitle().concat(StringPool.PERIOD).concat("pdf") + "\"");
                        OutputStream outputStream = resourceResponse.getPortletOutputStream();
                        String articleHtml = "<!DOCTYPE HTML><html><body>"+articleDisplay.getContent()+"</body></html>";
                        //prepend portal URL to local document library relative URLs
                        articleHtml = articleHtml.replaceAll("src=\"/documents", "src=\""+themeDisplay.getPortalURL()+"/documents");
                        Tidy tidy = new Tidy();
                        // Create inputStream to parse with tidy.
                        InputStream is = new ByteArrayInputStream(articleHtml.getBytes());
                        // Create XML Document from tidy
                        Document doc = tidy.parseDOM(is, null);
                        //render PDF
                        ITextRenderer renderer = new ITextRenderer();
                        renderer.setDocument(doc, null);
                        renderer.layout();
                        renderer.createPDF(outputStream);
                                   
                        } catch (DocumentException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                        }
                       
            }
}


Important points

  • With the help of JTidy and Flying saucer we can generate PDF from  HTML
  • We can also apply CSS to html in flying saucer.
  • Without configuration of open office in Liferay we can export Journal Content as PDF with the help of Flying saucer.


 I have written Article about flying saucer.


Note:

In above example I did not use JTidy to clean HTML but better use JTidy to clean HTML and pass to flying saucer so that we can get PDF without any problems.

Screens:

Journal Content Export Portlet




Example Journal Content for Export




Example PDF after Export




Reference Links:



Thursday, November 21, 2013

Liferay Document Conversion Portlet

Objective:

Convert document from one format to other format.

Liferay have inbuilt support to convert various document from one format to other formats with the help of Open Office service.

Download LiferayDocConverter  portlet from following location

You can find source and war file


Note: 

Portlet developed in Liferay 6.1GA2 EE version
If you want deploy in CE version you just do changes in liferay-plugin-package.properties

Liferay 6.1 EE version

name=LiferayDocConverter
module-group-id=liferay-ee
module-incremental-version=1
tags=
short-description=
change-log=
page-url=http://www.liferay.com
author=Liferay, Inc.
licenses=EE
portal-dependency-jars=\
    jstl-api.jar,\
    jstl-impl.jar
portal-dependency-tlds=c.tld
liferay-versions=6.1.20


Liferay 6.1 CE version

name = LiferayDocConverter
module-group-id=liferay
module-incremental-version=1
tags=
short-description=
change-log=
page-url=http://www.liferay.com
author=Liferay, Inc.
licenses=LGPL
portal-dependency-jars=\
    jstl-api.jar,\
    jstl-impl.jar
portal-dependency-tlds=c.tld
liferay-versions=6.1.1

Procedure for deploy portlet:

You can use war file and directly place in your portal deploy folder and test or you can also use source to deploy portlet.

Once portlet is deployed successfully you can see the portlet in sample category name as Doc Converter.

Note:

Please read full article before test this portlet.

Here also some limitations means some of the conversion may not possible any how mostly we can convert documents form one format to other format very easy.

Internally liferay using JOD converter to do this conversion process.JOD converter is java library have set of classes form which we can convert documents.

Liferay already implemented their own java classes with the help of JOD converter library so that we can use life ray classes to do this process.

Liferay provides one of the class called DocumentConversionUtil by using this we can convert documents from one format to other format.

The following are the sequence of steps we need to do.

  1. Download open office and install in your machine
  2. Start open office service
  3. Configure open office in Liferay
  4. Use DocumentConversionUtil to convert documents


Download open office and install in your machine

The following is direct down load link to download Open Office Apache_OpenOffice_4.0.1_Win32


The following is link for all platforms open office download


Once you downloaded open office then install in your machine and follow the installation steps based on Operating System you have.

Note:

Please follow the installation process based on your OS

Start open office service

Windows:

One installation successfully done then we have to start open office service from command prompt.

In windows open command prompt and go to installation location of open office and then go to directory called program.

Go to the folder where it was installed (for example, C:\Program Files\OpenOffice.org\program) and start OpenOffice service with the following

Command:


soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard



Linux:

 Goto Openoffice installing path.From command prompt type this line
 "cd /usr/lib/openoffice/program"

Command:


sudo soffice "-accept=socket,host=localhost,port=8100;urp;StarOffice.ServiceManager" -nologo -headless –nofirststartwizard"



Go through following link for complete details




The following is screen shot to run open office service in Windows.



Configure Open Office in Liferay

Once service is stared then we need to configure open office in liferay from administration screen,

Login as admin and go control panel from that go to server administration which is in left side server section.

Now click on External Service and enable open office service

Note:

 Here open office service port by default is 8100

If you change the port number at time of open office installation, then you need to change in liferay configuration too.

The following is liferay open office configuration screen




Use DocumentConversionUtil to convert documents

Once we did all above then we can start convert documents.

So now we can use DocumentConversionUtil to convert documents

The following is method to convert document from one type to other type



public static File convert(
                                    String id, InputStream inputStream, String sourceExtension,
                                    String targetExtension)  


The following is complete code to convert file and down load file


public class DocConverterAction extends MVCPortlet {

            @Override
            public void serveResource(
                                    ResourceRequest resourceRequest, ResourceResponse 
resourceResponse)
                        throws IOException, PortletException {
                         UploadPortletRequest uploadPortletRequest =PortalUtil.getUploadPortletRequest(resourceRequest);
                        String destinationExtension=ParamUtil.getString(uploadPortletRequest,"destinationExtension");
                         File sourceFile = uploadPortletRequest.getFile("sourceFile");
                         String extension = "";
                         String fileName=sourceFile.getName();
                         int i =fileName.lastIndexOf('.');
                         Timestamp stamp = new Timestamp(System.currentTimeMillis());
                          Date date = new Date(stamp.getTime());
                          System.out.println("=="+date);
                         File destinationFile=null;
                         if (i > 0) {
                             extension = fileName.substring(i+1);
                         }
                         InputStream inputStream = new FileInputStream(sourceFile);
                         HttpServletResponse httpResponse = PortalUtil.getHttpServletResponse(resourceResponse);
                         HttpServletRequest httpRequest = PortalUtil.getHttpServletRequest(resourceRequest);
                         try {
                                     destinationFile=DocumentConversionUtil.convert(String.valueOf(stamp.getTime()),
inputStream,extension,destinationExtension);
                                     InputStream is = new BufferedInputStream(new FileInputStream(destinationFile));
                                     ServletResponseUtil.sendFile(httpRequest,httpResponse ,destinationFile.getName(), is,getCotentType(destinationExtension));
                        } catch (Exception e) {
                                    if(e instanceof SystemException)
                                    System.out.println("555555555555555555"+e.getMessage());
                                    e.printStackTrace();
                        }
            }
}


Possible conversions



Important points

  • Liferay have in build support to convert document from one format other with help of Open office service.
  • To enable open office service in liferay we have to install open office and start open office service then we need to configure in liferay.
  • Once we configuration done then we can implement in plug-in portlet where ever you required,
  • Once we configure this we can also download journal content with different formats and also in document media portlet you can see different conversion features.


Portlet screen



Reference links





Author
Meera Prince

Recent Posts

Recent Posts Widget

Popular Posts