Serve PDF in Spring Portlet MVC Architecture - Liferay 6.0.6

前端 未结 1 518
别跟我提以往
别跟我提以往 2020-12-09 13:56

I was looking for a way to send a PDF (direct display) file to the browser through Liferay Portal. Found many solutions - the most popular one being writing a Servlet that d

相关标签:
1条回答
  • 2020-12-09 14:38

    I couldn't find anything on Servlet<->Portlet mapping thing. So I used Resource mapping for sending a pdf with Spring Portlet MVC using annotations. Ref:http://developers.sun.com/portalserver/reference/techart/jsr286/jsr286_2.html

    In JSP:

        <portlet:resourceURL var="PDFActionURL">
            <portlet:param name="reportType" value="pdf" />
            <portlet:param name="pdfNumber" value="${pdfNumber}" />
        </portlet:resourceURL>
        <input type="button" name="viewPDFButton" value="View PDF" onClick="self.location = '${PDFActionURL}';" />
    

    In Spring ApplicationContext.xml of the portlet, include these:

    <context:annotation-config />
    <context:component-scan base-package="com.xxx" />
    

    Define a new controller:

    import java.io.IOException;
    import java.io.OutputStream;
    
    import javax.portlet.PortletException;
    import javax.portlet.ResourceRequest;
    import javax.portlet.ResourceResponse;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.portlet.bind.annotation.ResourceMapping;
    
    import com.xxx.pdf.PDFBO;
    import com.xxx.PDFDTO;
    import com.liferay.portal.kernel.servlet.HttpHeaders;
    import com.liferay.portal.kernel.util.ParamUtil;
    
    @Controller("pdfController")
    @RequestMapping(value = "view")
    public class PDFController {
        private final static Logger LOG = LoggerFactory.getLogger(PDFController.class);
    
        //This is using Spring 3.0 Annotation Mapping (Spring Portlet MVC Architecture)
        @ResourceMapping
        public void serveResource(ResourceRequest resourceRequest, ResourceResponse res) throws PortletException, IOException {
            LOG.info("In serveResource: ResourceURL");
            String returnType = ParamUtil.getString(resourceRequest, "reportType");
            String pdfNumber = ParamUtil.getString(resourceRequest, "pdfNumber");
            LOG.info("returnType:" + returnType + " pdfNumber:" + pdfNumber);
            String filename = "FILENAME_"+pdfNumber+".pdf";
            // HttpServletRequest request =
            // PortalUtil.getHttpServletRequest(resourceRequest);
    
            if (returnType != null && returnType.equals("pdf")) {
                try {
                    //GET YOUR PDF HERE
                    //PDFBO pdfBO = new PDFBO();
                    //PDFDTO pdfContentVO = null;
                    //pdfContentVO = pdfBO.getPDF(pdfNumber);
                    res.setContentType("application/pdf");
                    res.addProperty(HttpHeaders.CACHE_CONTROL, "max-age=3600, must-revalidate");
                    res.addProperty(HttpHeaders.CONTENT_DISPOSITION,"filename="+ filename);
                    //Use this to directly download the file
                    //res.addProperty(HttpHeaders.CONTENT_DISPOSITION,"attachment");
                    OutputStream out = res.getPortletOutputStream();
                    //out.write(pdfContentVO.getPdfData());
                    out.write(/*get pdf byte[] Array Here */);
                    out.flush();
                    out.close();
                } catch (Exception e) {
                    LOG.info("Error in " + getClass().getName() + "\n" + e);
                }
            }
        }
    }
    

    Edit: If you are using previous versions of Liferay, this is a great article to implement file download/serving through Liferay - https://www.liferay.com/web/raymond.auge/blog/-/blogs/801426

    0 讨论(0)
提交回复
热议问题