REST java itext produces pdf from url template

Posted: April 29, 2014 in web

Open pdf inside browser

    @GET
    public Response getJson() throws IOException, DocumentException {
        PdfReader reader = new PdfReader("http://someserver/true.pdf");
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        PdfStamper pdfStamper = new PdfStamper(reader, out);
        AcroFields acroFields = pdfStamper.getAcroFields();
        acroFields.setField("field_title", "test");
        pdfStamper.close();
        reader.close();
        return Response.ok(out.toByteArray()).type("application/pdf").build();
    }

Download as pdf

    @GET
    @Produces("application/pdf")
    public Response getJson() throws IOException, DocumentException {
        PdfReader reader = new PdfReader("http://someserver/true.pdf");
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        PdfStamper pdfStamper = new PdfStamper(reader, out);
        AcroFields acroFields = pdfStamper.getAcroFields();
        acroFields.setField("field_title", "test");
        pdfStamper.close();
        reader.close();
        ResponseBuilder response = Response.ok((Object) out.toByteArray());
        response.header("Content-Disposition",
                "attachment; filename=yourfilename.pdf");
        return response.build();
    }
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s