0

我可以在通过 Chrome Rest Client 插件测试 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();
    }
4

1 回答 1

0

能够通过使用 Firefox Tamper Data 扩展成功获取内容:https ://addons.mozilla.org/En-us/firefox/addon/tamper-data/

于 2013-09-20T17:53:09.363 回答