Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6978379
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:44:53+00:00 2026-05-27T17:44:53+00:00

I was looking for a way to send a PDF (direct display) file to

  • 0

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 does the job. I’ve read about Portlet Resource Serving in JSR 286 Specification, can someone please elaborate on that for Spring 3.0 Portlet MVC?

<servlet>
    <display-name>DownloadServlet</display-name>
    <servlet-name>DownloadServlet</servlet-name>
    <servlet-class>com.liferay.portal.pdf.DownloadServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>DownloadServlet</servlet-name>
    <url-pattern>/DownloadServlet/*</url-pattern>
</servlet-mapping>

And the Servlet Consists of:

private void downloadServlet(HttpServletRequest req,
            HttpServletResponse resp) throws ServletException, IOException {

        logger.debug(" downloadServlet ::  ");
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        ServletOutputStream op = null;
        try {
            //Something
            pdfContentVO=//getpdf VO here

            String filename = "PDFFILE_"+pdfNumber+".pdf";

            op = resp.getOutputStream();
            resp.setContentType("application/pdf");     
            resp.setHeader("Content-Disposition", "attachment; filename="
                    + filename);
            resp.setContentLength(pdfContentVO.getPdfData().length); 
            System.out.println("pdfcontent"+pdfContentVO.getPdfData());
            op.write(pdfContentVO.getPdfData());
            op.flush();
            op.close();


         } catch(final IOException e) {
            System.out.println ( "IOException." );
            throw e;
        } finally {
            if (bis != null)
            {

                bis.close();
            }
            if (bos != null)
            {
                bos.flush();
                bos.close();
            }
        }

    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T17:44:54+00:00Added an answer on May 27, 2026 at 5:44 pm

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am looking for a way to send and receive keyboard information regardless of
I'm looking for a way to send the output of an arbitrary Emacs command
I am looking for a way to periodically send some data over all clients
I'm looking a way to enable IP logging with log4net in ASP.NET. I found
My soap client is working fine and I'm looking for a way send data
I'm looking for a way to send the contents of the clípboard as key
For an open source project I am looking for a way to send multiple
My client is looking for a way to send a text message to the
I am looking for a way to programmatically send an email to the user
I am looking for a way to double-click on file name in Windows Explorer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.