Question on Java Servlet to open a PDF file using iText

前端 未结 3 1347
北海茫月
北海茫月 2021-01-24 15:34

The code below grabs a PDF file and displays it in the browser.

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputS         


        
相关标签:
3条回答
  • 2021-01-24 16:22

    If the PDF file is already exists, then you don't have to use itext. You just read data from the file and write it into OutputStream of response.

    Here is some code

    public class WelcomeServlet extends HttpServlet {
    
        private static final String DOCUMENT_LOCATION = "H:\\testPDF.pdf"; // a test pdf on my PC
    
        @Override
        public void init(ServletConfig config) throws ServletException {
            super.init(config);
        }
    
    
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
            // set some response headers
            response.setHeader("Expires", "0");
            response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
            response.setHeader("Pragma", "public");
            response.setContentType("application/pdf");
    
            InputStream in = new FileInputStream(DOCUMENT_LOCATION);
            OutputStream out = response.getOutputStream();
    
            // Copy the bits from instream to outstream
            byte[] buf = new byte[1024];
            int len;
            while ((len = in.read(buf)) > 0) {
               out.write(buf, 0, len);
            }
            in.close();
    
        }
    } 
    
    0 讨论(0)
  • 2021-01-24 16:30

    You aren't using the reader in the second example. I'm not familiar with PdfStamper but I'd guess it uses the reader and thus the contents of your file will be in baos, but not in the second case.

    0 讨论(0)
  • 2021-01-24 16:35

    When you use PdfStamper it is reading in the file from the disk and writing it to baos. When you removed the PdfStamper, baos NEVER GETS WRITTEN TO. So of course, baos is empty, so never actually returns anything.

    EDIT: you want to actually do this (the PdfReader is only necessary if you want to modify the PDF):

    private static void copy(InputStream is, OutputStream os) throws IOException
    {
        byte buffer[] = new byte[8192];
        int bytesRead, i;
    
        while ((bytesRead = is.read(buffer)) != -1) {
            os.write(buffer, 0, bytesRead);
        }
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
                FileInputStream baos = new FileInputStream(DOCUMENT_LOCATION);
    
                // set some response headers
                response.setHeader("Expires", "0");
                response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
                response.setHeader("Pragma", "public");
                response.setContentType("application/pdf");
                response.setContentLength(new File(DOCUMENT_LOCATION).length());
    
                OutputStream os = response.getOutputStream();
                copy(baos, os);
                os.flush();
                os.close();
    
            }
        } 
    
    0 讨论(0)
提交回复
热议问题