title: Image rendering with Flying-Saucer/iText
body: <p>I am trying to generate a pdf document from a Java String containing html
  code. I am using "Freemarker" as a templating engine to generate the html
  content and then "Flying-Saucer" to convert this generated html to pdf.
  My problem is that images aren't rendered in the produced pdf. The exact
  details about how I am generating are as follows:</p>
  
  <pre><code>import java.io.ByteArrayOutputStream;
  import java.io.IOException;
  import java.io.OutputStream;
  import java.io.OutputStreamWriter;
  import java.io.Writer;
  import java.util.List;
  
  import org.xhtmlrenderer.pdf.ITextRenderer;
  
  import com.google.inject.Inject;
  import com.google.inject.Singleton;
  import com.lowagie.text.DocumentException;
  
  import freemarker.template.Configuration;
  import freemarker.template.DefaultObjectWrapper;
  import freemarker.template.SimpleHash;
  import freemarker.template.SimpleSequence;
  import freemarker.template.Template;
  import freemarker.template.TemplateException;
  
  @Singleton
  public class FlyingSaucerTaxInvoicePdfPrinter implements ITaxInvoicePdfPrinter {
      private final Configuration m_cfg;
  
      @Inject
      public FlyingSaucerTaxInvoicePdfPrinter() {
          // TODO: Following should be singletons and injected
          m_cfg = new Configuration();
          m_cfg.setObjectWrapper(new DefaultObjectWrapper());
          m_cfg.setClassForTemplateLoading(this.getClass(), "/");
      }
  
      private Template getTemplate() throws IOException {
          return m_cfg.getTemplate(PdfResources.TAX_INVOICE_TEMPLATE);
      }
  
      @Override
      public void printToPdf(TaxInvoiceUiPb taxInvoice, OutputStream pdfOutputStream) {
          OutputStream htmlOuputStream = null;
          try {
              htmlOuputStream = new ByteArrayOutputStream();
              printHtml(htmlOuputStream, taxInvoice);
              generatePDF(htmlOuputStream, pdfOutputStream);
          } catch (Exception e) {
              throw new LoggedRuntimeException(e);
          } finally {
              try {
                  htmlOuputStream.close();
              } catch (IOException e) {
                  throw new LoggedRuntimeException(e);
              }
          }
      }
  
      private void generatePDF(OutputStream htmlOuputStream, OutputStream pdfOutputStream)
                      throws DocumentException, IOException {
          try {
              ITextRenderer renderer = new ITextRenderer(30.666f, 20);
              String html = htmlOuputStream.toString();
              logHtml(html);
              renderer.setDocumentFromString(html);
              renderer.layout();
              renderer.createPDF(pdfOutputStream);
          } finally {
              pdfOutputStream.close();
          }
      }
  
  // Some methods not shown as irrelevant
  }
  </code></pre>
  
  <p>The generated html (showing only relevant section) is:</p>
  
  <pre><code>&lt;body&gt;
  &lt;div class="main" background="images/invoice-bg.jpg"&gt;
      &lt;img src="images/invoice-bg.jpg"&gt;&lt;/img&gt;
      &lt;div class="header"&gt;
      &lt;div class="logo"&gt;&lt;img src="images/invoice-logo.jpg" alt="" border="0" /&gt;&lt;/div&gt;
      &lt;div class="heading"&gt;booking invoice&lt;/div&gt;
      &lt;/div&gt;
      &lt;div class="clear"&gt;&lt;/div&gt;
  &lt;/div&gt;
  &lt;/body&gt;
  </code></pre>
  
  <p>This code runs as a War deployed on Tomcat. The location of the images in the War as output of 
  tree command (run inside WEB-INF) is:</p>
  
  <pre><code>|-- classes
  |   |-- com
  |   |   `-- ilodge
  |   |       `-- pmsServerWar
  |   |           |-- PmsServerWarListener.class
  |   |           `-- PmsServerWarServletModule.class
  |   |-- images
  |   |   |-- invoice-bg.jpg
  |   |   |-- rupees-icon-total.png
  |   |   |-- thank-you.jpg
  |   |   |-- total-bold-rupee.png
  |   |   `-- ul-bor.jpg
  |   |-- taxInvoice.css
  |   |-- taxInvoiceFooter.ftl
  |   |-- taxInvoice.ftl
  |   `-- test.ftl
  |-- lib
  |   |-- addressServer-1.0-SNAPSHOT.jar
  |   |-- addressUiProtobuf-1.0-SNAPSHOT.jar
  |   `-- xml-apis-1.3.03.jar
  `-- web.xml
  </code></pre>
  
  <p>I have truncated the output for brevity. Please help.</p>
  
  <p>Thanks and regards,
  Rohit</p>