如何将数据从网页转换和下载到PDF

时间:2016-07-25 05:27:19

标签: php codeigniter pdf

我正在使用PHP Codeigniter开发一个Web应用程序,我想要一个代码来显示和从网页下载数据到PDF格式。

1 个答案:

答案 0 :(得分:0)

下载mPDF并将其解压缩到application/third_party/文件夹

application/libraries/中创建一个名为M_pdf.php

的新文件

M_pdf.php档案

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

include_once APPPATH.'/third_party/mpdf/mpdf.php';

class M_pdf {

    public $param;
    public $pdf;

    public function __construct($param = '"en-GB-x","A4","","",10,10,10,10,6,3')
    {
        $this->param =$param;
        $this->pdf = new mPDF($this->param);
    }
}

然后在CodeIgniter中成功集成了mPDF。

如何使用, 在控制器中加载mPDF库作为下面的控制器,

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

class Cont_letter_pdf extends CI_Controller {

    public function __construct() {
        parent::__construct();

        $this->load->library('m_pdf'); //load mPDF library      
    }

    public function pdf_download()
    {
        $data = [];
        //load the view and saved it into $html variable
        $html=$this->load->view('welcome_message', $data, true);

        //this the the PDF filename that user will get to download
        $pdfFilePath = "output_pdf_name.pdf";

        //load mPDF library
        $this->load->library('m_pdf');

       //generate the PDF from the given html
        $this->m_pdf->pdf->WriteHTML($html);

        //download it.
        $this->m_pdf->pdf->Output($pdfFilePath, "D"); 

    }

我从以下网址了解到这一点。您也可以从这里查看教程。

https://arjunphp.com/generating-a-pdf-in-codeigniter-using-mpdf/