jsp - How to insert Image in PDF using iText and download to client machine? -
i'm using jdbc fetch data database , using itext create pdf file can downloaded on client machine. application coded in html/jsp , runs on apache tomcat.
i use response.getoutputstream
create output pdf file immediately.
the problem now, cannot insert image in document gives me , error
getoutputstream() has been called response
i understand i'm calling outputstream
again while inserting image , therefore error
how can insert image in document , still generate dynamic pdf file can downloaded client machine?
the relevant code:
response.setcontenttype("application/pdf"); response.setheader("content-disposition","attachment; filename=\"licenseinfo.pdf\""); // code 1 document document = new document(); pdfwriter.getinstance(document, response.getoutputstream()); // code 2 image image = image.getinstance("logo.jpg"); document.open(); document.add(image);
i'm sorry, aren't showing relevant code, code copy/pasted isn't responsible exception mention.
the relevant part you're using jsp, , didn't read important warnings concerning jsp listed in chapter 9 of my book.
when write jsp, white space , indentation, instance:
<% //a line of code %> <% // more code %> <% // line of code %> <% response.getoutputstream(); %>
this cause exception "getoutputstream() has been called response"
regardless if you're using itext or not. getoutputstream()
method called moment introduced first white space character in jsp script.
to fix this, need remove white space:
<% //a line of code %><% // more code %><% // line of code %><% response.getoutputstream(); %>
not single character accepted outside <%
, %>
markers. explained in better jsp manuals, shouldn't use jsp create binary files. why not? because jsp introduces white space characters @ arbitrary places in binary file. results in corrupt files. use servlets instead!
Comments
Post a Comment