I have a problem regarding report generation (pdf)in playframework app.I installed the pdf 0.7 module.
I need to show to the user a list of Products,their price and total amount from sales in a web page.If the user desires so,he should be able to generate a pdf report of the same.I thought of providing the user a link on the webpage clicking on which, a pdf document containing the formatted data opens up .The user can save it in any desired location.
I sent a HashMap containing the name Product instances and their quantities to template.From this info, I can calculate individual prices,total price etc and render as a table.
Now ,this has to be put in a pdf.For that I call up a controller method(say createReport(...)) and pass the same HashMap.In the controller method ,I have to format the info somehow and render using renderPDF().
So,I am sending the HashMap back and forth and I am wondering if this is the proper way..
Please advise.
Crtlr.java code snippet:
public static void showListings(){
HashMap<Product,Integer> map = createProductQtyMap();
render(map);
}
showListings template snippet:
<table>
#{list items:map.keySet() , as:'product'}
<tr>
<td>${product.getName()} </td>
<td>${map.get(product)} </td>
<td>${product.price} </td>
<td>${product.price * map.get(product)} </td>
</tr>
#{/list}
</table>
...
<a href="@{Ctrlr.createReport(map)}">create report</a>
Ctrlr.java
public static void createReport(HashMap<Product,Integer> map){
StringBuffer html=new StringBuffer("<html><body>");
//iterate thru map and create table rows as done in template
html.append("</body></html>");
String title= "my report";
play.modules.pdf.PDF.renderPDF(html.toString(),title);
}
in createReport template
...
<div id="report">
${html.raw()}
</div>
Unless I have misunderstood your requirements, this is not the right approach. I believe you should be doing the following.
and in the createReport template, you just render like you would any other HTML view, so you can largely copy and paste your code from showListings. If you want to simply reuse the same view, then ensure that you make your links, images, css etc absolute paths, and also make sure the CSS is not set up from SCREEN only.