0

I want to open a blank PDF in browser in my application. I am using the Spring Web model-view-controller (MVC) framework.

Here the code

    @Override
        public ModelAndView handleRequest(HttpServletRequest request,
                HttpServletResponse response) throws Exception {
    byte[] data = "No file attached".getBytes();
    String fileName = "NofileFound.pdf";
            response.setHeader("Content-Disposition","attachment; filename="+fileName);
            response.setContentType("application/pdf");         
            OutputStream out = response.getOutputStream();       

Document document = new Document();         
            PdfWriter writer = PdfWriter.getInstance(new Document(), new FileOutputStream("NofileFound.pdf"));
            document.open();
            document.add(new Paragraph("test"));
            document.close();
            writer.close();

            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            PdfWriter.getInstance(document, byteArrayOutputStream);         
            data = byteArrayOutputStream.toByteArray();

            out.write(data);
            out.flush();
            out.close();        
            return null;
    }

But when I open the file I got this error:

not a supported file type
Amadeu Cabanilles
  • 913
  • 3
  • 19
  • 47
  • 1
    You can not make blank pdf like this. You have to use some sort of third library like iText, PDFBox etc. – Chirag Parmar Nov 21 '16 at 10:00
  • remove the line response.setHeader("Content-Disposition","attachment; filename="+fileName); – Ye Win Nov 21 '16 at 10:03
  • @YeWin - Are you sure your solution will work? I doubt! – Chirag Parmar Nov 21 '16 at 10:05
  • I also doubt, that why I just give comment and want to try once by OP. If not ok I have to tell other possible solution. – Ye Win Nov 21 '16 at 10:07
  • @YeWin- I doubt because PDFs have certain standards that we need to follow. Otherwise it will not be recognised as a pdf. I only see `"No file attached".getBytes()` text. See https://en.wikipedia.org/wiki/Portable_Document_Format for more details. – Chirag Parmar Nov 21 '16 at 10:11
  • removing the line response.setHeader("Content-Disposition","attachment; filename="+fileName) does not work either :-( – Amadeu Cabanilles Nov 21 '16 at 10:11
  • 1
    Also just imagine, if pdf creation was that easy as creating a simple text file, why there are paid libraries out there. They will be of no use. ;) – Chirag Parmar Nov 21 '16 at 10:13
  • i think you need to provide full path of your file – Onkar Nov 21 '16 at 10:16
  • see SaviNuclear answer at this link, i think it help http://stackoverflow.com/questions/32476706/how-to-display-pdf-file-in-browser – Ye Win Nov 21 '16 at 10:16
  • yes, i also agree with @Onkar – Ye Win Nov 21 '16 at 10:17

1 Answers1

0
   @Override
        public ModelAndView handleRequest(HttpServletRequest request,
                HttpServletResponse response) throws Exception {

    response.setContentType("application/pdf");
    Document document = new Document();
    PdfWriter.getInstance(document, response.getOutputStream());
    document.open();
    document.add(new Paragraph("empty pdf"));
    document.close();

    return null
}
Amadeu Cabanilles
  • 913
  • 3
  • 19
  • 47