【问题标题】:PhantomJS not working with codeigniter + exec()PhantomJS 不能与 codeigniter + exec() 一起使用
【发布时间】:2016-02-07 09:41:19
【问题描述】:

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

控制器

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;
        }
    }

Rasterize.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 和文本。我正在本地系统中获取所有数据。?

标签: php linux codeigniter pdf-generation phantomjs


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2013-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多