0

I'm able to save a PDF while testing a JAX-RS service through a Chrome Rest Client plugin but it has no content. Any ideas why this code produces a PDF that has no content?

@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();
    }
Zack Macomber
  • 6,682
  • 14
  • 57
  • 104
  • 1
    Strange - it produces the correct PDF for me when just hitting it with the browser. Is it something to do with the Chrome Rest Client plugin? Have you tried just `wget`ing the PDF? Are there any exceptions? – condit Sep 19 '13 at 17:13
  • Works fine when I strip the auth filter down and request it directly through the browser. Guess it's an issue with the Chrome Rest plugin...it's not allowing me to save the pdf through that client – Zack Macomber Sep 19 '13 at 19:09

1 Answers1

0

Was able to successfully get the content by using the Firefox Tamper Data extension: https://addons.mozilla.org/En-us/firefox/addon/tamper-data/

Zack Macomber
  • 6,682
  • 14
  • 57
  • 104