简体   繁体   English

PDF已生成,但没有内容

[英]PDF is generated but has no content

I'm able to save a PDF while testing a JAX-RS service through a Chrome Rest Client plugin but it has no content. 通过Chrome Rest Client插件测试JAX-RS服务时,我能够保存PDF,但其中没有内容。 Any ideas why this code produces a PDF that has no content? 有什么想法为什么这段代码会产生不包含内容的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();
    }

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

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

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