How to send PDF file data as a response using Servlet?

前端 未结 2 800
Happy的楠姐
Happy的楠姐 2021-01-14 13:36

My requirement is responding the PDF data to the mobile client(iPhone) using HTTP Servlet.

I did in the following way, But I am not getting expected output in the cl

相关标签:
2条回答
  • 2021-01-14 14:03
    package com.javatpoint;
    
    import java.io.*;
    import java.util.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    import com.darwinsys.spdf.PDF;
    import com.darwinsys.spdf.Page;
    import com.darwinsys.spdf.Text;
    import com.darwinsys.spdf.MoveTo;
    
    public class ServletPDF extends HttpServlet {
    
        @Override
        public void doGet(HttpServletRequest request, HttpServletResponse response)
                throws IOException {
    
            PrintWriter out = response.getWriter();
            response.setContentType("application/pdf");
    
            response.setHeader("Content-disposition","inline; filename='javatpoint.pdf'");
    
            PDF p = new PDF(out);
            Page p1 = new Page(p);
            p1.add(new MoveTo(p, 200, 700));
            p1.add(new Text(p, "www.javatpoint.com"));
            p1.add(new Text(p, "by Sonoo Jaiswal"));
    
            p.add(p1);
            p.setAuthor("Ian F. Darwin");
    
            p.writePDF();
        }
    }
    
    0 讨论(0)
  • 2021-01-14 14:21

    You have to use ServletOutputStream and its write() method to write bytes to the response .

    0 讨论(0)
提交回复
热议问题