php - PhantomJS 不适用于 codeigniter + exec()

标签 php linux codeigniter pdf-generation phantomjs

我正在尝试使用 Codeigniter、PhantomJS 创建网页的 pdf,我正在使用 Linux 操作系统。我有一个带有 exec() 的 phantom_helper 文件。我从 here 得到了一些代码.当我运行 phantomjs/var/www/PhantomJS/js/rasterize.js http://google.com/var/www/PhantomJS/pdf/Test.pdf 时,我能够创建 pdf 文件在 linux 终端窗口。我在这里添加我拥有的代码,

Controller :

public function index()
{
    $this->load->helper('phantomjs');
    $this->load->helper('url');
    $viewbox['generationStatus'] = 'PDF Generation successful';
    $url = 'http://google.com';
    $filename = strtotime(date('Y-m-d H:i:s')).'.pdf';
    $resp = rasterize_wrapper($url,$filename);
    $viewbox['filename'] = $resp;
    if($resp == 0)
    {
        $viewbox['filename'] = '';
        $viewbox['generationStatus'] = 'PDF Generation failed';
    }
    $this->load->view('welcome_message',$viewbox);
}

Phanthom_helper:

 if ( ! function_exists('rasterize_wrapper'))
    {
        function rasterize_wrapper($url='', $output=''){
          if($url=='' || $output=='')
            {
              show_error('URL or Output file name not defined correctly');  
              log_message('error','rasterize_wrapper: not initialized');
              exit;
            }
            $url = escapeshellcmd($url);
            exec('phantomjs '.realpath('js/rasterize.js').' '.$url.' '.realpath('pdf').'/'.$output,$output_status, $return_status);
            if($return_status == '0'){ return $output;}
            return 0;
        }
    }

光栅化.js

var page = new WebPage();
if (phantom.args.length < 2 || phantom.args.length > 3) {
    console.log('Usage: rasterize.js URL filename');
    phantom.exit();
} else {
    address = phantom.args[0];

    output = phantom.args[1];
    page.viewportSize = { width: 600, height: 600 };
    page.open(address, function (status) {
        if (status !== 'success') {
            console.log('Unable to load the address!');
        } else {
            window.setTimeout(function () {
                page.render(output);
                phantom.exit();
            }, 200);
        }
    });
}

最佳答案

我的问题已经解决了。这段代码确实有效。问题是因为我使用 Linux,所以我必须为 pdf 保存文件夹设置文件夹权限。我已经设置好并获得了pdf。 Css 问题也可以通过执行 MEDIA QUERY @media print {....} Ty 来解决。

关于php - PhantomJS 不适用于 codeigniter + exec(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33563465/

相关文章:

php - 代码点火器 $this->db->reconnect();用法

php - 仅选择页面上链接的相关新闻项(多一二多关系)

php - $.ajax 发送文件 formdata 和变量

linux - 错误 :13 - Permission Denied android studio

linux - wxPython中的非轮询文件监控

c - 在哪里更改 LD_LIBRARY_PATH

php - 如何在 Codeigniter 中防止 ASCII 编码/二进制字符串 SQL 注入(inject)

php - Azure 应用服务 - Wordpress 的糟糕性能

php - 如何使用 Facebook Graph API 为事件上传图片?

php - Codeigniter 回调函数不工作