title: How to insert Image in PDF using iText and download to client machine?
body: <p>I'm using jdbc to fetch data from database and then using iText I create a PDF file which can be downloaded on client machine. The application is coded in html/jsp and runs on Apache Tomcat.</p>
  
  <p>I use the <code>response.getOutputStream</code> to create an output PDF file immediately.</p>
  
  <p>The problem is that now, I cannot insert an image in this document as it gives me and error that  </p>
  
  <blockquote>
    <p>getOutputStream() has already been called for this response</p>
  </blockquote>
  
  <p>I understand that I'm calling <code>Outputstream</code> again while inserting the image and therefore the error</p>
  
  <p>How can I insert an image in the document and still generate a dynamic PDF file which can be downloaded by client machine?</p>
  
  <p>The relevant code:</p>
  
  <pre><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);
  </code></pre>