aerohit
unread,Apr 28, 2012, 3:17:51 AM4/28/12Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to Flying Saucer Users
Hello,
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:
------------------Java code starts here-------------
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.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 singleton and injected
m_cfg = new Configuration();
m_cfg.setObjectWrapper(new DefaultObjectWrapper());
m_cfg.setClassForTemplateLoading(this.getClass(), "/");
}
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();
}
}
private Template getTemplate() throws IOException {
return m_cfg.getTemplate("taxInvoice.ftl");
}
// Some methods not shown as irrelevant
}
------------------Java code ends here-------------
The generated html (showing only relevant section) is:
<body>
<div class="main" background="images/invoice-bg.jpg">
<img src="images/invoice-bg.jpg"></img>
<div class="header">
<div class="logo"><img src="images/invoice-logo.jpg" alt=""
border="0" /></div>
<div class="heading">booking invoice</div>
</div>
<div class="clear"></div>
</div>
</body>
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:
|-- 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
I have truncated the output for brevity. Please help.
Thanks and regards,
Rohit