douzhangkui2467 2012-06-22 16:09
浏览 76

Zend PHP - 将Google Chart保存为PDF

I am developing a site using Zend PHP framework. On a page I have a chart created with Google Chart API - https://developers.google.com/chart/. I would like to add a button to allow the user to download the chart as a PDF file (essentially convert the chart to an image and embed it in a PDF).

I have been able to convert the google chart to an image using a javascript function that generates the image data string - http://www.battlehorse.net/attach/topics/charts/google_charts_to_image.html

function getImgData(chartId) {
    var chartContainer = document.getElementById(chartId);
    var chartArea = chartContainer.getElementsByTagName('iframe')[0].
    contentDocument.getElementById('chartArea');
    var svg = chartArea.innerHTML;
    var doc = chartContainer.ownerDocument;
    var canvas = doc.createElement('canvas');
    canvas.setAttribute('width', chartArea.offsetWidth);
    canvas.setAttribute('height', chartArea.offsetHeight);   
    canvas.setAttribute(
        'style',
        'position: absolute; ' +
        'top: ' + (-chartArea.offsetHeight * 2) + 'px;' +
        'left: ' + (-chartArea.offsetWidth * 2) + 'px;');
    doc.body.appendChild(canvas);
    canvg(canvas, svg);
    var imgData = canvas.toDataURL("image/png");
    canvas.parentNode.removeChild(canvas);
    return imgData;
}

I have also downloaded a free PHP class to handle generating the PDF - http://www.fpdf.org/

I have been able to use an ajax function to pass the image data string to a zend controller. I can then save the image locally and use it to build a PDF file and save that locally too.

//store the image locally
    $data = substr($image,strpos($image,",")+1); //removing the "data:image/png;base64," part
    file_put_contents ('downloads/'.$title.'.png', base64_decode($data));

    // create PDF document
    $pdf = new FPDF();
    $pdf->AliasNbPages();
    $pdf->AddPage();
    $pdf->Image('downloads/'.$title.'.png',10,6,30);

    //destroy the local copy of the image
    fclose('downloads/'.$title.'.png');
    unlink('downloads/'.$title.'.png');

    //save the PDF document
    $pdf->Output('downloads/'.$title.'.pdf');

I am now stuck because I cannot find a way to present the PDF file for downloading. I need to use an Ajax request to generate the PDF as the image data string is too long for a standard URL. So I think I need to redirect the user to the download link for the file after it has been generated (and then delete it again afterwards).

Does anyone know how I can serve the file for download, if I enter the file path into my browser I get a path not found error (e.g. "MyApp/public/downloads/myChart.pdf"). I have read in a few places how to set the header and body of the response in the controller but nothing has worked so far.

  • 写回答

1条回答 默认 最新

  • dongyue1988 2012-06-22 16:13
    关注

    Whe you are generating the download link. Why don't you replace yourpath/public with domain.com? Assuming that ur PDF is generated In the directory public/downloads/blahblah

    Possibly the next step would be lik

    $path = 'downloads/'.$title.'.pdf';

    Header("Location: http://domain.com.$path");

    Now when the user calls this script file , using Ajax, the PDF will be generated and the location will be transferred to pdf's path!

    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮