在项目报告部分,我们在每个HTML页面上都有大约10个highcharts。我们必须给用户一个选择下载这些网页作为PDF。这个应用程序运行在PHP 5.4.16上,我们使用的是codeigniter框架。为了生成pdf,我们使用了mPDF。
当前方法(我们现在是如何生成它的)
1)当报表页面加载highcharts页面时,每个highcharts都会被推入一个SVG格式的数组中,其他图表详细信息也会通过此代码推送到数组中。
charts.push({title:chartData[key]['analytics_type'],text:key,svg:chart.getSVG()});2)单击“另存为PDF”,将此数组和其他所需数据一起提交给控制器。
3)控制器首先使用以下代码将每个SVG图表转换为图像文件。
private function svgToJpg($item) {
$filename = isset($_POST['filename']) ? $_POST['filename'] : 'chart';
$width = isset($_POST['width']) ? $_POST['width'] : 800;
$svg=$item->svg;
if (get_magic_quotes_gpc()) {
$svg = stripslashes($svg);
}
$tempName = md5(rand());
$typeString = '-m image/jpeg';
$ext = '.jpg';
$outfile = TEMP_PATH.$tempName.$ext;
if (isset($typeString)) {
$width = "-w $width";
// generate the temporary file
if (!file_put_contents(TEMP_PATH.$tempName.".svg", $svg)) {
die("Couldn't create temporary file. Check that the directory permissions for
the /temp directory are set to 777.");
}
// do the conversion
try {
shell_exec("chmod 777 ".TEMP_PATH.$tempName.".svg");
$output = shell_exec("java -jar ". BATIK_PATH ." $typeString -d $outfile $width ".TEMP_PATH.$tempName.".svg");
} catch(Exception $e) {
die("Could not create the image. Seems like Java is not installed.");
}
// catch error
if (!is_file($outfile) || filesize($outfile) < 10) {
echo "<pre>$output</pre>";
echo "Error while converting to JPG from SVG. ";
if (strpos($output, 'SVGConverter.error.while.rasterizing.file') !== false) {
echo "SVG code for debugging: <hr/>";
echo htmlentities($svg);
}
}这种方法工作得很好,但生成PDF需要很长时间。可能是在将SVG转换为图像时消耗的时间最多。
有没有其他更快的方法来用highchart生成pdf?我已经看到了另一种方法,但答案中提到的链接不起作用:- how to auto create pdf using highchart graphs
请给出解决的方法。
发布于 2014-04-26 11:12:32
通过使用phantomjs,一个无头浏览器,我已经成功地完成了复杂的布局。它生成pdf,以及加载其他格式。基本上,您可以从php脚本加载html,然后调用phantomjs来截取屏幕截图并将其栅格化为pdf。
https://stackoverflow.com/questions/23285271
复制相似问题