Question

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();
    }
Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top