forked from joeltinez/Highcharts-Exporting-Server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exportChart-perl.pl
76 lines (64 loc) · 1.73 KB
/
exportChart-perl.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/perl
use CGI qw(:standard);
use strict;
use warnings;
#Set this to your batik jar path
my $batikPath = '/usr/global/tools/lorenz/batik/batik-rasterizer.jar';
my $type = param('type');
my $svg = param('svg');
my $filename = param('filename') || 'chart';
my $tmpName = time;
my $typeString = '';
my $ext = '';
my $width = '';
if ($type eq 'image/png') {
$typeString = '-m image/png';
$ext = 'png';
} elsif ($type eq 'image/jpeg') {
$typeString = '-m image/jpeg';
$ext = 'jpg';
} elsif ($type eq 'application/pdf') {
$typeString = '-m application/pdf';
$ext = 'pdf';
} elsif ($type eq 'image/svg+xml') {
$ext = 'svg';
}
my $outfile = "/tmp/$tmpName.$ext";
if ($typeString) {
if (param('width')) {
$width = param('width');
if ($width) {
$width = "-w $width";
}
}
# generate the temporary file
open(OUT, ">/tmp/$tmpName.svg") or die "Couldn't open tmp svg file /tmp/$tmpName.svg: $!";
print OUT $svg;
close OUT;
# do the conversion
my $output = `java -jar $batikPath $typeString -d $outfile $width /tmp/$tmpName.svg`;
# catch error
if (!-e $outfile || -s $outfile < 10) {
die "$outfile was not successfully written during batik conversion: $?";
}
# stream it
else {
print "Content-Disposition: attachment; filename=\"$filename.$ext\"\n";
print "Content-Type: $type\n\n";
open(IN, $outfile) or die "Couldn't open $outfile for writing: $!";
print join('', <IN>);
close IN;
}
# delete it
unlink "/tmp/$tmpName.svg";
unlink $outfile;
}
# SVG can be streamed directly back
elsif ($ext eq 'svg') {
print "Content-Disposition: attachment; filename=\"$filename.$ext\"\n";
print "Content-Type: $type\n\n";
print $svg;
}
else {
die "No image type specified to script";
}