mpdf for Codeignitor with chinese

参考Generating a PDF in Codeigniter using mPDF

1.Download and extract mPDF

Download mPDF from : http://www.mpdf1.com/mpdf/index.php and Extract it to your application/third_party/ folder of your CodeIgniter.

Create a new file in your application/libraries/ name it M_pdf.php and copy past the blow PHP Script.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?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 = '"UTF-8","A4","","",10,10,10,10,6,3')
{
$this->param =$param;
//$this->pdf = new mPDF($this->param);
$this->pdf = new mPDF('+aCJK','A4','','',32,25,27,25,16,13);
$this->pdf->autoLangToFont = true;
}
}

That is it. You had successfully integrated mPDF in CodeIgniter. Now let’s use it.

2.Edit config.php

edit the application/third_party/mpdf/config.php
line44:

1
$this->useAdobeCJK = true;

line67:

1
$this->autoLangToFont = true;

3.How to Use

Load the mPDF library just like your load other CI’s other libraries and then call pdf property. See the blow example code for better understanding.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MyPrintPdf extends CI_Controller {
public function __construct()
{
parent::__construct();
}

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

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

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

//generate the PDF from the given html
//$this->m_pdf->pdf->WriteHTML($html);
$content='<table border="1" cellpadding="2" cellspacing="1"><tr><td style="border:1mm solid green">this is 什么</td></tr></table>';

$this->m_pdf->pdf->useAdobeCJK = true;
$this->m_pdf->pdf->autoScriptToLang=true;
$this->m_pdf->pdf->WriteHTML($content);
//download it.
$this->m_pdf->pdf->Output($pdfFilePath, "D");
}
}