In this example will create a exporting server for Highchart in Java. Since the default example is in PHP, and I'm using Java and tomcat as my web server.
I convert the PHP exporting script example to Java servlet code.
public class ReportExporter extends HttpServlet {
protected void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
ServletContext application = this.getServletContext();
String type = request.getParameter("type");
String svg = request.getParameter("svg");
String filename = request.getParameter("filename");
System.out.println("type: " + type);
System.out.println("svg: " + svg);
System.out.println("filename: " + filename);
System.out.println("width: " + request.getParameter("width"));
if (filename == null && filename.equals("")) {
filename = "chart";
}
String typeString = "";
String ext = "";
String outFile = "";
String width = null;
String tempName = null;
try {
tempName = SecurityUtil.md5(String.valueOf(System
.currentTimeMillis()));
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
if (type.equals("image/png")) {
typeString = "-m image/png";
ext = "png";
} else if (type.equals("image/jpeg")) {
typeString = "-m image/jpeg";
ext = "jpg";
} else if (type.equals("application/pdf")) {
typeString = "-m application/pdf";
ext = "pdf";
} else if (type.equals("image/svg+xml")) {
ext = "svg";
}
outFile = "temp/" + tempName + "." + ext;
if (!typeString.equals("")) {
width = request.getParameter("width");
if (width != null && !width.equals(""))
width = "-w " + width;
}
// Generate temporary file
File f = new File(application.getRealPath("/temp/" + tempName
+ ".svg"));
IOUtil.writeToFile(f, svg);
String batikPath = application.getRealPath("/WEB-INF/lib/batik-rasterizer.jar");
String classPath = "-cp " + application.getRealPath("/WEB-INF/lib");
String outFileFullPath = application.getRealPath(outFile);
String tempFileFullPath = application.getRealPath("temp/"+tempName+".svg");
try {
// Convert SVG file into specified type
SystemUtil.runCommand("java",classPath, "-jar",batikPath, typeString,"-d",outFileFullPath,width,tempFileFullPath);
// Show the file to user as download
response.setHeader("Content-disposition", "attachment; filename=" + filename + "." +ext);
response.setHeader("Content-type", type);
ServletOutputStream out = response.getOutputStream();
IOUtil.fileToStream(new File(outFileFullPath), out);
out.flush();
out.close();
} catch (Throwable e) {
e.printStackTrace();
response.getOutputStream().print("Error on exporting");
}
}
}
You will need these classes
SystemUtil
IOUtil
This example use Batik rasterizer. Please download it here. Then place it your web application folder
WEB-INF/lib/batik-rasterizer.jar
WEB-INF/lib/batik-slideshow.jar
WEB-INF/lib/batik-squiggle.jar
WEB-INF/lib/batik-svgpp.jar
WEB-INF/lib/batik-ttf2svg.jar
WEB-INF/lib/lib -> contains lib for batik (see batik distribution batik-version/lib)
Tag Cloud
accordion
(1)
admin
(1)
administration
(1)
android
(2)
angel love
(2)
answer
(1)
app
(1)
archangel
(2)
auto complete
(1)
axis2 json
(1)
backup
(1)
barcode
(3)
bb
(1)
binary decimal convert c
(1)
blackberry
(9)
boylevantz
(1)
browser error
(1)
c
(1)
C#
(1)
career
(1)
certification
(1)
ci
(1)
codeigniter
(1)
color
(1)
connection
(1)
contenga
(1)
create
(1)
csharp
(1)
css
(1)
database
(2)
db
(1)
demo
(1)
dev
(2)
developer
(2)
download
(1)
eclipse
(1)
enterprise deploy
(1)
eval
(1)
evo
(1)
expression
(2)
facelets
(1)
free
(1)
golden ratio
(1)
grant
(1)
hash md5 encrypt encryption
(1)
heap size
(1)
hibernate
(1)
html
(1)
ibm
(3)
index.php
(1)
installation manager
(1)
ios
(1)
java
(14)
javascript
(3)
jawab
(1)
job
(1)
jquery
(1)
jquery mobile
(2)
js
(2)
jsf
(2)
juel
(1)
kerjaan
(1)
kernel
(1)
language
(1)
launchpad
(1)
linux
(1)
list
(1)
listfield
(1)
location
(1)
log
(1)
log4j
(1)
logging
(1)
lowongan
(1)
magang
(1)
maven
(1)
memory
(1)
message
(1)
mobile
(3)
mysql
(3)
native
(1)
opportunity
(1)
orion
(1)
out of memory
(1)
php
(2)
pin
(1)
pom.xml maven pom sort sortpom mvn
(1)
postgre
(1)
program
(1)
programmer
(2)
programming
(1)
psql
(1)
Q&A
(1)
qrcode barcode java
(1)
question
(1)
remote
(1)
remove index.php
(1)
servlet
(1)
simple
(1)
solusi
(1)
spring
(1)
swap
(1)
tab
(1)
tanya
(1)
tcp rinetd linux suse redirection
(1)
tips
(1)
Tomcat
(1)
tooltip
(1)
training
(1)
ui
(1)
url
(1)
user
(1)
user interface
(1)
vacancy
(3)
web
(4)
webworks
(1)
worklight
(3)
zxing
(1)
Friday, October 28, 2011
Subscribe to:
Post Comments (Atom)
5 comments:
Thanks for your great effort but since I'm a beginner in the J2EE realm, could you please provide more detailed configuration info like how to set the exporting.url option to match your servlet location in my chart options.
When using Highchart you will have such options
var chart = new Highcharts.Chart(
{
chart : {
renderTo: 'some_container'
},
exporting : {
url : 'http://www.myweb.com/exportingServlet'
}
}
);
A much simpler way of doing it. check it out
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.batik.transcoder.Transcoder;
import org.apache.batik.transcoder.TranscoderInput;
import org.apache.batik.transcoder.TranscoderOutput;
import org.apache.batik.transcoder.image.JPEGTranscoder;
import org.apache.batik.transcoder.image.PNGTranscoder;
import org.apache.fop.svg.PDFTranscoder;
public class ExportServlet extends HttpServlet {
private static final long serialVersionUID = -5921270712374007555L;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
try
{
String type = request.getParameter("type");
String svg = request.getParameter("svg");
String filename = request.getParameter("filename");
if (filename == null && filename.equals(""))
{
filename = "chart";
}
String ext = "";
Transcoder transcoder = null;
ServletOutputStream out = response.getOutputStream();
if (!type.equals("image/svg+xml"))
{
InputStream svgInputStream = new ByteArrayInputStream(svg.getBytes());
if (type.equals("image/png"))
{
ext = "png";
transcoder = new PNGTranscoder();
}
else if (type.equals("image/jpeg"))
{
ext = "jpg";
transcoder = new JPEGTranscoder();
}
else if (type.equals("application/pdf"))
{
ext = "pdf";
transcoder = new PDFTranscoder();
}
response.setHeader("Content-disposition", "attachment; filename=" + filename + "." + ext);
response.setHeader("Content-type", type);
TranscoderInput tInput = new TranscoderInput(svgInputStream);
TranscoderOutput lOutput = new TranscoderOutput(out);
transcoder.transcode(tInput, lOutput);
}
else
{
ext = "svg";
response.setHeader("Content-disposition", "attachment; filename=" + filename + "." + ext);
response.setHeader("Content-type", type);
out.write(svg.getBytes());
}
out.flush();
out.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
doPost(request, response);
}
}
Hi, I came across your website and I would like to inform to everybody who's reading this blog, that Highcharts recently released a java bases export-server.
Find it here; https://github.com/highslide-software/highcharts.com/tree/master/exporting-server/java
Highsoft Solutions
Great information, thanks
Post a Comment