简体   繁体   中英

How to return a non-editable PDF as a response?

I have a URL:

http://www.irs.gov/pub/irs-pdf/fw4.pdf

It contains an editable PDF. I have make it non-editable. I did so and kept it in the temp directory of a folder. Now i want to send the non-editable PDF as a response, when the user clicks this url, he must get the non-editable pdf. This is what I have done till now:

String strDirectoy ="C:\\Temp";
boolean success = (
    new File(strDirectoy)).mkdir();
    if (success) {
        System.out.println("Directory: " 
        + strDirectoy + " created");
    }  
PdfReader reader = new PdfReader("http://www.irs.gov/pub/irs-pdf/fw4.pdf");//C:\\fw4.pdf
PdfStamper stamp2 = new PdfStamper(reader, new FileOutputStream("C:\\Temp\\Flattened.pdf"));
AcroFields form2 = stamp2.getAcroFields();
stamp2.setFormFlattening(true);
stamp2.close();

Now i need to delete the temp folder as if it never existed and return the non-editable PDF as the response for the above specified URL.

How can i do this?

  • Write a Servlet.
  • Flatten your pdf in a temporary file (using the createTempFile() and deleteOnExit() methods of java.io.File ).
  • Use the setContentType of the HttpServletResponse to set the MIME type of the pdf.
  • Write the contents of the temporary pdf file to the outputstream of the http response

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM