PDF已生成但没有内容

时间:2013-09-19 15:58:43

标签: java jersey jax-rs flying-saucer

我可以在通过Chrome Rest客户端插件测试JAX-RS服务时保存PDF,但它没有内容。有什么想法为什么这个代码产生一个没有内容的PDF?

@Produces("application/pdf")
    @GET
    @Path("/{id}")
    public Response getPdfById(@PathParam("id") Long id) {
        StreamingOutput stream = new StreamingOutput() {
            @Override
            public void write(OutputStream output) throws IOException, WebApplicationException {
                try {
                    StringBuffer buf = new StringBuffer();

                    buf.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
                    buf.append("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\""); 
                    buf.append(" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">");
                    buf.append("<html xmlns=\"http://www.w3.org/1999/xhtml\">");
                    buf.append("<head>");
                    buf.append("<title>My First Document</title>");
                    buf.append("<style type=\"text/css\"> b { color: green; } </style>");
                    buf.append("</head>");
                    buf.append("<body>");
                    buf.append("<p>");
                    buf.append("<b>Hi there!!!</b>");
                    buf.append("</p>");
                    buf.append("</body>");
                    buf.append("</html>");

                    LOG.info("-----------");
                    LOG.info("HTML OUTPUT");
                    LOG.info("-----------");
                    LOG.info(buf.toString());

                    // parse our markup into an xml Document
                    try {
                        DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
                        Document doc = builder.parse(new StringBufferInputStream(buf.toString()));
                        ITextRenderer renderer = new ITextRenderer();
                        renderer.setDocument(doc, null);
                        renderer.layout();
                        renderer.createPDF(output);
                        output.close();
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                } catch (Exception e) {
                    throw new WebApplicationException(e);
                }
            }
        };

        return Response.ok(stream).header("Content-Disposition","attachment; filename=orderingPdfTest.pdf").build();
    }

1 个答案:

答案 0 :(得分:0)

能够使用Firefox篡改数据扩展程序成功获取内容:https://addons.mozilla.org/En-us/firefox/addon/tamper-data/