首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在PHP中使用mPDF生成包含10个highcharts的HTML页面的不同方法

在PHP中使用mPDF生成包含10个highcharts的HTML页面的不同方法
EN

Stack Overflow用户
提问于 2014-04-25 14:02:32
回答 1查看 845关注 0票数 1

在项目报告部分,我们在每个HTML页面上都有大约10个highcharts。我们必须给用户一个选择下载这些网页作为PDF。这个应用程序运行在PHP 5.4.16上,我们使用的是codeigniter框架。为了生成pdf,我们使用了mPDF。

当前方法(我们现在是如何生成它的)

1)当报表页面加载highcharts页面时,每个highcharts都会被推入一个SVG格式的数组中,其他图表详细信息也会通过此代码推送到数组中。

代码语言:javascript
复制
charts.push({title:chartData[key]['analytics_type'],text:key,svg:chart.getSVG()});

2)单击“另存为PDF”,将此数组和其他所需数据一起提交给控制器。

3)控制器首先使用以下代码将每个SVG图表转换为图像文件。

代码语言:javascript
复制
      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

请给出解决的方法。

EN

回答 1

Stack Overflow用户

发布于 2014-04-26 11:12:32

通过使用phantomjs,一个无头浏览器,我已经成功地完成了复杂的布局。它生成pdf,以及加载其他格式。基本上,您可以从php脚本加载html,然后调用phantomjs来截取屏幕截图并将其栅格化为pdf。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23285271

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档