Spring MVC to open PDF as the view

2020-07-23 03:22发布

Which is the appropriate View class to render existing PDF? AbstractView?

I am fetching PDF via a webservice ,so I'm not looking to subclass AbstractPdfView to render PDF.

I'd like to stay with the Spring controller classes which return a ModelAndView which means writing my own subclass of AbstractView to just write the PDF to a ServletOutputStream. Any other built in support available in Spring MVC?

Thanks

标签: spring-mvc
3条回答
▲ chillily
2楼-- · 2020-07-23 04:11

I agree with @Biju Kunjummen's answer but using iText would be also nice to generate the PDF.

here is the code snippet of the controller method.

@RequestMapping(value = "/common/reportgenerator/generatePDF")
    public void generatePdf(HttpServletRequest req,HttpServletResponse res)
    {
        res.setContentType("text/html;charset=UTF-8");
        ServletOutputStream outStream=null;
        try 
        {
            String calledFrom = req.getHeader("referer");
            calledFrom=req.getRequestURL().substring(0,req.getRequestURL().lastIndexOf("/"))+"/ReportGenerator.egp";
            calledFrom += "?isPdf=yes&"+req.getQueryString();
            System.out.println(calledFrom+"?isPdf=yes&"+req.getQueryString());



            InputStream input = new URL(calledFrom).openStream();
            StringWriter writer = new StringWriter();
            CopyUtils.copy(input, writer);

            //System.out.println(writer.toString());

            res.setContentType("application/pdf");
            res.setHeader("Content-Disposition", "attachment;filename=report.pdf");
            outStream = res.getOutputStream();

            ITextRenderer renderer = new ITextRenderer();


            renderer.setDocument(calledFrom);
            renderer.layout();
            renderer.createPDF(outStream);

        } 
        catch (Exception e) 
        {
            new AbcException(e,exceptionHandlerService);
        }
        finally
        {
            try
            {
                outStream.flush();
                outStream.close();
            }
            catch(Exception ex)
            {
                new AbcException(ex,exceptionHandlerService);
            }

        }


    }

Hope this helps you. Cheers.

查看更多
手持菜刀,她持情操
3楼-- · 2020-07-23 04:14

I think the best way is to simply stream it out using HttpServletResponse:

OutputStream out = response.getOutputStream();

out.write(..); //buffer and write..
查看更多
Root(大扎)
4楼-- · 2020-07-23 04:16

There is no such class.

You have to manually write that file. Please see answer here: Display the PDF file stored on the webserver on Browser new window using Spring MVC

I have changed that code to:

 // get absolute path of the application
 ServletContext context = request.getSession().getServletContext();
 String appPath = context.getRealPath("/");
 // construct the complete absolute path of the file
 String fullPath = appPath + "WEB-INF/pdfs/201507.pdf";

Also, see the answer for not downloading the pdf and putting the inputStream in the finally block.

查看更多
登录 后发表回答