php+batik highchartピクチャのエクスポート機能を実現
最近、highchartのピクチャをデータ・レポートとともに生成する必要があるエクスポート要件があります.
何日か振り回して,やっとやった.
1、まずJAVA環境をインストールする必要があります.環境変数などを設定する必要はありません.
2、関連するbatik jarパッケージをダウンロードして、これは何度もダウンロードしても間違いです.添付ファイルの中に私が整理したものがあります.
3、公式に書かれたPHPプログラムを呼び出して、関数にまとめました.
ファイルをローカルに保存して、勝手にできます.
JAVA環境をインストールするのが面倒であれば、提出要求公式のエクスポートアドレスを直接シミュレートすることができます.画像の名前が返されます.ダウンロードできます.サーバーは30時間保存されます.具体的には参考にしてください.
http://www.highcharts.com/docs/export-module/export-module-overview
http://jsfiddle.net/6h8o16g0/
彼のフォーマットでSVGパラメータを渡すだけでいいです.
何日か振り回して,やっとやった.
1、まずJAVA環境をインストールする必要があります.環境変数などを設定する必要はありません.
2、関連するbatik jarパッケージをダウンロードして、これは何度もダウンロードしても間違いです.添付ファイルの中に私が整理したものがあります.
3、公式に書かれたPHPプログラムを呼び出して、関数にまとめました.
function svgToImg($type, $svg, $filename){
clearFile(SAVE_PATH);//
ini_set('magic_quotes_gpc', 'off');
$svg = (string) $svg;
$filename = (string) $filename;
// prepare variables
if (!$filename or !preg_match('/^[A-Za-z0-9\-_ ]+$/', $filename)) {
$filename = 'chart';
}
if (get_magic_quotes_gpc()) {
$svg = stripslashes($svg);
}
// check for malicious attack in SVG
if(strpos($svg,"<!ENTITY") !== false || strpos($svg,"<!DOCTYPE") !== false){
exit("Execution is stopped, the posted SVG could contain code for a malicious attack");
}
$tempName = md5(rand());
// allow no other than predefined types
if ($type == 'image/png') {
$typeString = '-m image/png';
$ext = 'png';
} elseif ($type == 'image/jpeg') {
$typeString = '-m image/jpeg';
$ext = 'jpg';
} elseif ($type == 'application/pdf') {
$typeString = '-m application/pdf';
$ext = 'pdf';
} elseif ($type == 'image/svg+xml') {
$ext = 'svg';
} else { // prevent fallthrough from global variables
$ext = 'txt';
}
$outfile = SAVE_PATH.$tempName.'.'.$ext;
if (isset($typeString)) {
// size
$width = '';
if ($_POST['width']) {
$width = (int)$_POST['width'];
if ($width) $width = "-w $width";
}
// generate the temporary file
if (!file_put_contents(SAVE_PATH."$tempName.svg", $svg)) {
die("Couldn't create temporary file. Check that the directory permissions for
the /temp directory are set to 777.");
}
if(IS_WIN){
$output = shell_exec("java -jar ". BATIK_PATH ." $typeString -d $outfile $width ".SAVE_PATH."$tempName.svg");
}else{
$output = shell_exec("/usr/local/jdk1.8.0_66/bin/java -jar ". BATIK_PATH ." $typeString -d $outfile $width ".SAVE_PATH."$tempName.svg");
}
// catch error
if (!is_file($outfile)) {
echo "<pre>$output</pre>";
echo "Error while converting SVG. ";
}
else {
return $outfile;
}
} else if ($ext == 'svg') {
header("Content-Disposition: attachment; filename=\"$filename.$ext\"");
header("Content-Type: $type");
echo $svg;
} else {
echo "Invalid type";
}
}
ファイルをローカルに保存して、勝手にできます.
JAVA環境をインストールするのが面倒であれば、提出要求公式のエクスポートアドレスを直接シミュレートすることができます.画像の名前が返されます.ダウンロードできます.サーバーは30時間保存されます.具体的には参考にしてください.
http://www.highcharts.com/docs/export-module/export-module-overview
http://jsfiddle.net/6h8o16g0/
彼のフォーマットでSVGパラメータを渡すだけでいいです.