Itext fill content with Rest using ByteArrayOutputStream and p:media

Posted: March 20, 2013 in web

Rest Service

@Path("/rest/")
public class DocumentRestService {

@GET
 @Path("/generate/{carId}")
 @Produces("application/pdf")
 public Response getContractDocument(@PathParam("carId") String carId) throws IOException, DocumentException {

//PDF Template
 PdfReader reader = new PdfReader("c:/work/test2.pdf");

ByteArrayOutputStream out = new ByteArrayOutputStream();

PdfStamper pdfStamper = new PdfStamper(reader, out);
 AcroFields acroFields = pdfStamper.getAcroFields();

//Settinge dummy field values on template pdf
 for (String field : acroFields.getFields().keySet()) {
 acroFields.setField(field, field);
 }
 pdfStamper.close();
 reader.close();
 ResponseBuilder response = Response.ok(out.toByteArray());
 return response.build();
 }
}

xhtml

<p:media value="http://localhost/car/rest/generate/12" player="pdf" width="100%" height="300px">
 Your browser can't display pdf.
</p:media>
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