为什么我无法将jersey的pdf发送给浏览器客户端?

时间:2019-06-09 16:15:12

标签: java pdf jersey jax-rs

我有问题:我想在网络服务中编辑pdf,然后通过jersey将已编辑的pdf发送给客户端。问题是,如果我在浏览器中调用该URL,它说加载PDF时出错。

我已经尝试将响应类型设置为除pdf之外的其他格式,并且还尝试了互联网上的另一个pdf。

@Path("/certificate")
public class CertificateService{
    @GET
    @Path("{language}/{name}")
    @Produces("application/pdf")
    public Response createPDF(@PathParam("language") String language,@PathParam("name") String name) throws IOException {


        PDDocument document = null;
        try {
            // Load the document and get the AcroForm

            DateFormat formatter;
            URL url = new URL("http://app-learning.com/simplestock/assets/Zertifikat-de.pdf");
                    BufferedInputStream file = new BufferedInputStream(url.openStream());
                    document = document.load(file);

                formatter = new SimpleDateFormat("MM/dd/yyyy");


            PDDocumentCatalog docCatalog = document.getDocumentCatalog();
            PDAcroForm acroForm = docCatalog.getAcroForm();


            // Fill the text field
            PDTextField field1 = (PDTextField) acroForm.getField("Text1");
            field1.setValue(name);
            // Optional: don't allow this field to be edited
            field1.setReadOnly(true);

            Date c = Calendar.getInstance().getTime();


            String today = formatter.format(c);

            PDTextField field2 = (PDTextField) acroForm.getField("Text2");
            field2.setValue(today);
            // Optional: don't allow this field to be edited
            field2.setReadOnly(true);






        } catch (IOException e) {

        }



      final File file = File.createTempFile("certi", ".pdf");
      document.save(file);
      document.close();



        return Response.ok(file).header("Content-Disposition",  "filename=restfile.pdf").type("application/pdf").build();


    }

0 个答案:

没有答案