在codeigniter中将html转换为pdf

时间:2013-11-21 13:26:10

标签: codeigniter dompdf

我正在使用dompdf library将html内容转换为pdf。我遇到了一些问题。

1)我能够生成pdf但是在我的HTML内容上有一些图像,但是当我生成pdf图像时,不会出现PDF格式。

2)PDF也没有显示完整的数据意味着数据肯定会出现但是看不到完整的宽度。

请帮助!!!

2 个答案:

答案 0 :(得分:1)

以下是我将HTML格式化为pdf的示例...我希望我能给你一些想法

库:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Dompdf_gen {

    public function __construct() {

        require_once APPPATH.'third_party/dompdf/dompdf_config.inc.php';

        $pdf = new DOMPDF();

        $CI =& get_instance();
        $CI->dompdf = $pdf;

    }

}

控制器:

function pdf($id){

            $data['contract_items_all']     = $this->contract_items->get_serial_infos($this->uri->segment(4));
            $data['child_info']             = $this->contract_items->get_children_info($cid);

            $this->load->library('dompdf_gen');
            $html = $this->load->view('contracts/equipment_pdf', $data, true);//this equipment_pdf contain the html that you will convert to pdf
            $this->load->library('dompdf_gen');
            $dompdf = new DOMPDF();

            // Convert to PDF
            $this->dompdf->load_html($html);
            $this->dompdf->render();
            $this->dompdf->stream("Contract.pdf",array("Attachment"=>0)); // VIEW IN OTHER TAB
            //$this->dompdf->stream("Contract.pdf"); // SAVE FILE TO DISK
    }

答案 1 :(得分:0)

为什么要加载两次库?

// ...

$this->load->library('dompdf_gen');
$html = $this->load->view('contracts/equipment_pdf', $data, true);//this equipment_pdf contain the html that you will convert to pdf
$this->load->library('dompdf_gen');
$dompdf = new DOMPDF();

// ...