dompdf使用curl而不是allow_url_fopen

时间:2013-04-18 13:57:58

标签: php codeigniter curl dompdf

我一直在解决DOMPDF的问题,我们使用CodeIgniter构建了一个应用程序,并且需要一个允许打印到PDF的功能。

在本地,DOMPDF运作良好。但是,在我的实时服务器上,所有图像都被破坏,无法找到,即使它提供了破碎图像的链接,如果复制/粘贴到网址工具栏中也能正常工作。

我做了一些挖掘并发现如果在我的php.ini设置中我启用了allow_url_fopen它会解决问题,它确实做到了!

但是,当我发现允许此设置使我们的服务器不太安全时,我的所有成功都崩溃了,这是不可能发生的。我们还有其他安全应用程序。

我看到人们暗示使用名为“Curl”的编程语言来完成这项工作,而不是启用allow_url_fopen。

有人可以就如何将CURL实现到DOMPDF给我任何建议吗?有人曾经这样做过吗?

这是我生成PDF的函数:

public function create_pdf($hash = false)
{

    //hash might be full 12-12, or only front 12 chars. must be 12 or invalid
    if (empty($hash) || (strlen($hash) < 12)) 
    {
        redirect('view_proposal/error/invalid');
    }


    $this->data['display_buttons'] = false;

    $proposal = $this->proposal_model->get_by_hash($hash);

    if (empty($proposal)) 
    {
        redirect('view_proposal/error/notfound');
    }

    $page_break  = '<p style="page-break-after: always"></p>';

    //from proposal hash, get:

    // cover page
    $html = $this->_get_page($hash, 'cover', $proposal);
    $html .= $page_break;

    // contents
    $this->load->model('proposal_block_model');
    $blocks = $this->proposal_block_model->get_many_by('proposal_id', $proposal->id);

    if (!empty($blocks))
    {
        foreach ($blocks as $block) {
            $html .= $this->_get_page($hash, $block->block_id, $proposal);
        }           
    }   

    $html .= $page_break;


    // pricing page
    $html .= $this->_get_page($hash, 'pricing', $proposal);
    $html .= $page_break;

    // terms & conditions
    $html .= $this->_get_page($hash, 'terms', $proposal);
    $html .= $page_break;

    //return $html;

    //status info
    //$html .= $this->_get_page($hash, 'status_info', $proposal);
    //$html .= $page_break;

    //create a pdf version of the output, and set to $html

    $path = 'file';

    $this->load->library('MY_dompdf');
    $this->my_dompdf->pdf_create($html, $path, true);
}

您将看到此函数引用另一个名为“_get_page”的函数。这会收集您创建的工作提案中的所有页面,然后create_pdf函数正在使用DOMPDF创建它的PDF输出。

如果它有帮助,这是“_get_page”函数:

    function _get_page($hash, $block_id, $proposal)
{
    $this->data['proposal'] = $proposal;

    $this->data['primary'] = (count(explode('-', $hash)) > 1);

    $proposal->sent_date = (empty($activity)) ? '' : 'Proposal Sent Date '. show_local_time((int)$activity->cdate);
    $proposal->end_date = (empty($proposal->end_date)) ? '' : 'Good Until '. show_local_time((int)$proposal->end_date);

    $proposal->accent_color = ($proposal->header_color == 'FFFFFF') ? '000000' : $proposal->accent_color;

    $blocks = $this->block_model->get_toplevel_by_proposal($proposal->id);

    $block_toc = $this->_block_toc($blocks, $block_id);

    $this->data['prev_page'] = $block_toc['prev'];
    $this->data['next_page'] = $block_toc['next'];




    //render substitution variables
    $this->load->library('proposals_lib');
    $proposal->introduction = $this->proposals_lib->render_string($proposal->introduction, $proposal->id);
    $proposal->terms_conditions = $this->proposals_lib->render_string($proposal->terms_conditions, $proposal->id);
    $proposal->conclusion = $this->proposals_lib->render_string($proposal->conclusion, $proposal->id);


    $this->data['proposal_open'] = in_array($proposal->status, array(PROPOSAL_STATUS_DRAFT, PROPOSAL_STATUS_SENT, PROPOSAL_STATUS_REVIEWED));

    $this->data['css'] = 'css/public/external/pdf_proposal.css';

    switch ($block_id)
    {
        case 'cover':
            $this->data['client'] = $this->client_model->get($proposal->client_id);
            $html = $this->load->view('public/external/proposal_pages/cover_page', $this->data, true);
            break;
        case 'pricing':
            $this->load->model('proposal_pricing_items_model');
            $this->data['price_items'] = $this->proposal_pricing_items_model->order_by('order', 'asc')->order_by('id', 'asc')->as_object()->get_many_by('proposal_id',$proposal->id);
            $html = $this->load->view('public/external/proposal_pages/pricing_page', $this->data, true);
            break;
        case 'terms': //only for pdf
            $this->data['terms'] = $proposal->terms_conditions;
            $html = $this->load->view('public/external/proposal_pages/terms_page', $this->data, true);
            break;
        case 'status': //only for pdf
            $html = $this->load->view('public/external/proposal_pages/status_info_page', $this->data, true);
            break;              

        default:

            $contents = $this->block_model->get_by_proposal($proposal->id);
            $this->data['blocks'] = $this->_get_content($contents, $block_id, $block_toc['next']);

            $this->data['blocks'] = $this->proposals_lib->render_sections($this->data['blocks'], $proposal->id);

            $html = $this->load->view('public/external/proposal_pages/internal_page', $this->data, true);
    }

    return $html;
}

另一个很小的函数,名为“get_page”,用于获取提案的任何页面:

    //ajax call to get any page 
public function get_page()
{

    $hash       = $this->input->post('proposal_hash', true);
    $block_id   = $this->input->post('page_id', true);

    $proposal = $this->proposal_model->get_by_hash($hash);

    //confirm the page_id (block_id) is a part of the proposal hashed, then get content
    if(is_numeric($block_id) && !$this->block_model->block_in_proposal($block_id, $proposal->id))
        return $this->ajax_output(array('message'=>'The page requested is not part of this proposal.') , FALSE);


    $html = $this->_get_page($hash, $block_id, $proposal);

    $this->ajax_output(array('html'=>$html), TRUE);
}

以下是一个如何调用其中一个图像的示例:

 <img src="<?php echo base_url();?>uploads/clients/<?= $client->image ?>">

很抱歉,如果我在这里寻求帮助,我花了很长时间才发现该修复程序启用了“allow_url_fopen”,但现在这不是解决方案我回到原点。如果唯一的解决方案是使用CURL,那么我需要一些帮助......

谢谢!

EDIT ***

这是我第一次使用CURL的蹩脚尝试:

class MY_dompdf

{

function pdf_create($html, $filename='', $stream=TRUE) 
{


$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://hidden.com/view_proposal/vFgXKJcNSRns');

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
//$st = curl_getinfo($ch, CURLINFO_HTTP_CODE);  
$html = curl_exec($ch);
curl_close($ch);

    $dompdf = new DOMPDF();
    $dompdf->load_html($html);
    $dompdf->render();
    if ($stream) {
        $dompdf->stream($filename.".pdf");
    } else {
        return $dompdf->output();
    }
}

}

哪个吐出错误:

致命错误:在第231行的C:\ UniServer \ www \ clientsky \ application \ libraries \ dompdf \ include \ frame_tree.cls.php中的非对象上调用成员函数prepend_child()

0 个答案:

没有答案