使用dompdf codeigniter进行批量操作以将html转换为pdf

时间:2017-09-08 07:54:12

标签: php codeigniter pdf dompdf

我有一个问题。我可以使用DOMPDF批量操作将HTML转换为PDF吗? 我已经尝试了这个批量但它不起作用。

Task<SampleResult>

此PDF生成器

function bulk() {
    $action = $this->input->post('action');

    if ($action == "export") {
        $export = $this->input->post('msg');

        for ($i=0; $i < count($export) ; $i++) { 
            $this->load->library('Pdfgenerator');
            $users = $this->model_csv->getDatabyDescription($export[$i]);
            $desc = $users['description'];
            $name =  substr((strrchr($desc, '-')), 2);
            $html = $this->load->view('formulir_new2', $users, true);

            $this->pdfgenerator->generate($html,"E-Tax" . time() . $name);
        }
    }

这是我的单一生成

public function export(){     $ data_id = $ this-&gt; uri-&gt; segment(3);

class PdfGenerator {

    public function generate($html,$filename) {
        define('DOMPDF_ENABLE_AUTOLOAD', false);
        require_once("./vendor/dompdf/dompdf/autoload.inc.php");


        $dompdf = new dompdf();
        $options = new Options();
        $options->setIsRemoteEnabled(true);

        $dompdf->setOptions($options);
        $dompdf->set_paper(array(0,0, 612, 936), 'portrait');
        $dompdf->load_html($html);
        $dompdf-enter code here>render();
        $dompdf->stream($filename.'.pdf',array("Attachment"=>0));
   }
}

}

$users = $this->model_csv->getDatabyDescription($data_id);
// echo $users['amount'];
$this->load->view('formulir_new2.php', $users);

2 个答案:

答案 0 :(得分:0)

如果您能够生成一个pdf,请尝试此操作,然后只有此代码可以帮助您批量生成。否则忽略

public function generate($html,$filename)
  {
    ob_start();
    $filename='Ex-'.time().'test.pdf';
    define('DOMPDF_ENABLE_AUTOLOAD', false);
    require_once("./vendor/dompdf/dompdf/autoload.inc.php");


    $dompdf = new dompdf();
    $options = new Options();
    $options->setIsRemoteEnabled(true);

    $dompdf->setOptions($options);
    $dompdf->set_paper(array(0,0, 612, 936), 'portrait');
    $html="<html><body>test".rand(0,99)."</body></html>";
    $dompdf->load_html($html);
    $dompdf-enter code here>render();
    $dompdf->stream($filename.'.pdf',array("Attachment"=>0));
    ob_end_clean();
  }

答案 1 :(得分:0)

尝试此代码,放在您的控制器上

public function test_pdf(){
    $test_data=array('test1','test2','test3');
    foreach($test_data as $key=>$value){
      $html="<html><body>".$value."</body></html>";
      $this->create_pdf($html,$value);
      //echo "vjiay<br>";
    }
    echo "done";
  }
  public function create_pdf($html,$filename) {
    ob_start();
   // $html = $this->load->view('view_file', $this->data, true);
    $this->load->library('M_pdf');
    ob_end_flush();
    $this->m_pdf->pdf->WriteHTML($html);

    $filename .='.pdf' ;//= "test_" . time() . ".pdf";
    //download it D save F.

    $this->m_pdf->pdf->Output(FCPATH . 'uploads/' . $filename, "F");
  }

使用MPDF库链接https://github.com/phpclicks/mpdf正在下载