小编典典

关于Java Servlet使用iText打开PDF文件的问题

java

下面的代码获取一个PDF文件,并将其显示在浏览器中。

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;
import com.lowagie.text.pdf.PdfStream;
import com.lowagie.text.pdf.PdfWriter;

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 {

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PdfReader reader = new PdfReader(DOCUMENT_LOCATION);
        PdfStamper stamper = null;
        try {
            stamper = new PdfStamper(reader, baos);
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        try {
            stamper.close();
        } catch (DocumentException e) {
            System.out.println("Why is it making me do this?");
            e.printStackTrace();
        }

        // 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(baos.size());

        OutputStream os = response.getOutputStream();
        baos.writeTo(os);
        os.flush();
        os.close();

    }
}

如果将其更改为此代码,则不会返回文件。唯一的区别是删除了处理PdfStamper的行。

import java.io.ByteArrayOutputStream;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.io.PrintWriter;

    import javax.servlet.ServletConfig;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    import com.lowagie.text.Document;
    import com.lowagie.text.DocumentException;
    import com.lowagie.text.pdf.PdfReader;
    import com.lowagie.text.pdf.PdfStamper;
    import com.lowagie.text.pdf.PdfStream;
    import com.lowagie.text.pdf.PdfWriter;

    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 {

            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            PdfReader reader = new PdfReader(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(baos.size());

            OutputStream os = response.getOutputStream();
            baos.writeTo(os);
            os.flush();
            os.close();

        }
    }

为什么这只能与PdfStamper一起使用?我不想做任何冲压,我只想在浏览器中显示PDF。我缺少一些基本上可以代替iText中的PdfStamper的东西吗?


阅读 291

收藏
2020-11-30

共1个答案

小编典典

使用时,PdfStamper它是从磁盘读取文件并将其写入baos。当您删除时PdfStamperbaos切勿写入。因此,当然baos是空的,因此永远不会实际返回任何东西。

编辑:您要实际执行此操作(仅当您要修改PDF时才需要PdfReader):

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();

        }
    }
2020-11-30