简体   繁体   English

如何返回不可编辑的PDF作为响应?

[英]How to return a non-editable PDF as a response?

I have a URL: 我有一个网址:

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

It contains an editable PDF. 它包含一个可编辑的PDF。 I have make it non-editable. 我已将其设为不可编辑。 I did so and kept it in the temp directory of a folder. 我这样做了,并将其保存在文件夹的temp目录中。 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. 现在,我想发送不可编辑的PDF作为响应,当用户单击此URL时,他必须获取不可编辑的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. 现在,我需要删除temp文件夹,就好像它根本不存在一样,并返回不可编辑的PDF作为上述指定URL的响应。

How can i do this? 我怎样才能做到这一点?

  • Write a Servlet. 编写一个Servlet。
  • Flatten your pdf in a temporary file (using the createTempFile() and deleteOnExit() methods of java.io.File ). 将pdf展平到一个临时文件中(使用java.io.FilecreateTempFile()deleteOnExit()方法)。
  • Use the setContentType of the HttpServletResponse to set the MIME type of the pdf. 使用HttpServletResponse的setContentType设置pdf的MIME类型。
  • Write the contents of the temporary pdf file to the outputstream of the http response 将临时pdf文件的内容写入http响应的输出流

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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