You are not logged in.
Here is how did using an opesource library called IText. I have taken the code of exporting the charts to image for this forum and modified it to get the image as PDF using IText.
www.lowagie.com/iText/
This is a working code. Create a servlet add this POST method to it and you are done.
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
String heightStr = request.getParameter("height")==null?"0":request.getParameter("height");
String widthStr = request.getParameter("width")==null?"0":request.getParameter("width");
int height=Integer.parseInt(heightStr.indexOf(".")==-1?heightStr:heightStr.substring(0,heightStr.indexOf(".")));
int width=Integer.parseInt(widthStr.indexOf(".")==-1?widthStr:widthStr.substring(0,widthStr.indexOf(".")));
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
String current_row="";
Image docImage=null;
try{
for(int i=0;i<height;i++){
current_row=request.getParameter("r"+i)==null?"":request.getParameter("r"+i);
String current_col[]=current_row.split(",");
int current_width=1;
for(int m=0;m<current_col.length;m++){
String pixel[]=current_col[m].split(":");
if(pixel.length>1){
g.setColor(this.charColor(pixel[0]));
g.drawLine(current_width, i, current_width+Integer.parseInt(pixel[1]), i);
current_width=current_width+Integer.parseInt(pixel[1]);
}else if(pixel.length==1){
g.setColor(this.charColor(pixel[0]));
g.drawLine(current_width, i, current_width+1, i);
current_width++;
}
}
}
docImage = Image.getInstance(image, null);
}catch(Exception e){
e.printStackTrace();
}
g.create();
Document doc = new Document();
ByteArrayOutputStream baosPDF = new ByteArrayOutputStream();
PdfWriter docWriter = null;
try {
docWriter = PdfWriter.getInstance(doc, baosPDF);
doc.open();
Table table = new Table(1, 1);
table.setWidth(100);
table.setTableFitsPage(true);
table.setAlignment(Table.ALIGN_LEFT);
table.setBorder(Table.NO_BORDER);
Cell cell = new Cell();
cell.setBorder(Cell.NO_BORDER);
cell.setVerticalAlignment(Cell.ALIGN_TOP);
cell.add(docImage);
table.addCell(cell);
doc.add(table);
doc.close();
} catch (DocumentException e) {
e.printStackTrace();
}
response.setContentType("application/pdf");
response.setContentLength(baosPDF.size());
ServletOutputStream out = response.getOutputStream();
baosPDF.writeTo(out);
out.flush();
}
public Color charColor(String hex){
for(int i=0;i<6;i++){
if(hex.length()<6)hex="0"+hex;
}
int r=this.HtoD(hex.substring(0,2));
int g=this.HtoD(hex.substring(2,4));
int b=this.HtoD(hex.substring(4,6));
Color color = new Color(r,g,b);
return color;
}
public int HtoD(String hex){
return Integer.valueOf(hex, 16);
}
------------------ at JSP/Html end add this JS--------------------------------------------:
function exportColumnPDF()
{
flashMovie = document.getElementById("amcolumn");
flashMovie.exportImage('/webapp/Export'); // it is not required to pass filename here if you set it in settings file)
}
and call this method against your chart. In this case am using it for my columnchart i.e. "amcolumn"
/Export is my servlet name in web.xml
-----------------------------------------------------------------------------------------------
Enjoy :) !!!
Thanks & Regards,
Kapil
Offline
hi,
thx for this code it helps me a lot, but there is one thing i want to ask you about it is possible to export many images and how to detect the streaming of each image in the http request??
Offline
I need the complete implementation for do this, please somebody have this?
Offline
kaouther74 wrote:
hi,
how to detect the streaming of each image in the http request??
with StreamReader. Look more at stackoverflow.com/questions/1271701/reading-image-from-web-server-in-c-sharp-proxy
Last edited by Trivian (2012-06-20 01:13:27)
Offline
I have an error and i can't resolve this, anyone can help me please?, this is my error:
INFO: Error : PWC3990: getWriter() has already been called for this response
in this line
ServletOutputStream out = response.getOutputStream();
The response is this : HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
I am using JSF in glassfish server, and in my code i use onlye one time the response .
Sorry by my enlgish.
Any help it's welcome thanks!
Offline
I am resolve this, the solution to JSF is in the file export.xhtml put this
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<h:body>
<h:form>
<f:event listener="#{pdfMB.doPost()}" type="preRenderView" />
</h:form>
</h:body>
</html>
Offline
DonVidela Thank you verry much, i had a similar problem and reading your solution i resolved.
modificare poze
Offline
maestrosteve It's a pleasure help you, this problem was a very bastards xD
Greetings!
Offline
© amCharts | Forum engine: PunBB