如何使用 codeigniter 制作 pdf?
how can I make pdf with codeigniter?
所以它看起来像这样example
我应该遵循什么教程?
- 从 here
下载 TCPDF 文件
- 解压文件夹'tcpdf'放在路径“application/third_party”
在文件夹“application/libraries”中创建一个名为'pdf.php'的文件并放入下面的代码
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
require_once APPPATH . "/third_party/tcpdf/tcpdf.php";
class Pdf extends tcpdf {
public function __construct() {
parent::__construct();
}
}
?>
在您的控制器中添加以下函数
public function createPDF($fileName,$html) {
ob_start();
// Include the main TCPDF library (search for installation path).
$this->load->library('Pdf');
// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('TcPdf');
$pdf->SetTitle('TcPdf');
$pdf->SetSubject('TcPdf');
$pdf->SetKeywords('TcPdf');
// set default header data
$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, PDF_HEADER_STRING);
// set header and footer fonts
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
$pdf->SetPrintHeader(false);
$pdf->SetPrintFooter(false);
// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
// set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, 0, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(0);
$pdf->SetFooterMargin(0);
// set auto page breaks
//$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
$pdf->SetAutoPageBreak(TRUE, 0);
// set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
// set some language-dependent strings (optional)
if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
require_once(dirname(__FILE__).'/lang/eng.php');
$pdf->setLanguageArray($l);
}
// set font
$pdf->SetFont('dejavusans', '', 10);
// add a page
$pdf->AddPage();
// output the HTML content
$pdf->writeHTML($html, true, false, true, false, '');
// reset pointer to the last page
$pdf->lastPage();
ob_end_clean();
//Close and output PDF document
$pdf->Output($fileName, 'F');
}
在 pdf 中准确创建一个视图并将其加载到控制器中以制作 pdf
$htmlContent = $this->load->view('views/your-view-file', $data, TRUE);
$createPDFFile = 'your-pdf-name'.'.pdf';
$this->createPDF('location-for-pdf'.$createPDFFile, $htmlContent);
这将在您指定的位置创建一个与您创建的视图完全相同的 pdf 文件。
所以它看起来像这样example
我应该遵循什么教程?
- 从 here 下载 TCPDF 文件
- 解压文件夹'tcpdf'放在路径“application/third_party”
在文件夹“application/libraries”中创建一个名为'pdf.php'的文件并放入下面的代码
<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); require_once APPPATH . "/third_party/tcpdf/tcpdf.php"; class Pdf extends tcpdf { public function __construct() { parent::__construct(); } } ?>
在您的控制器中添加以下函数
public function createPDF($fileName,$html) { ob_start(); // Include the main TCPDF library (search for installation path). $this->load->library('Pdf'); // create new PDF document $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); // set document information $pdf->SetCreator(PDF_CREATOR); $pdf->SetAuthor('TcPdf'); $pdf->SetTitle('TcPdf'); $pdf->SetSubject('TcPdf'); $pdf->SetKeywords('TcPdf'); // set default header data $pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, PDF_HEADER_STRING); // set header and footer fonts $pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN)); $pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA)); $pdf->SetPrintHeader(false); $pdf->SetPrintFooter(false); // set default monospaced font $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED); // set margins $pdf->SetMargins(PDF_MARGIN_LEFT, 0, PDF_MARGIN_RIGHT); $pdf->SetHeaderMargin(0); $pdf->SetFooterMargin(0); // set auto page breaks //$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM); $pdf->SetAutoPageBreak(TRUE, 0); // set image scale factor $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO); // set some language-dependent strings (optional) if (@file_exists(dirname(__FILE__).'/lang/eng.php')) { require_once(dirname(__FILE__).'/lang/eng.php'); $pdf->setLanguageArray($l); } // set font $pdf->SetFont('dejavusans', '', 10); // add a page $pdf->AddPage(); // output the HTML content $pdf->writeHTML($html, true, false, true, false, ''); // reset pointer to the last page $pdf->lastPage(); ob_end_clean(); //Close and output PDF document $pdf->Output($fileName, 'F'); }
在 pdf 中准确创建一个视图并将其加载到控制器中以制作 pdf
$htmlContent = $this->load->view('views/your-view-file', $data, TRUE); $createPDFFile = 'your-pdf-name'.'.pdf'; $this->createPDF('location-for-pdf'.$createPDFFile, $htmlContent);
这将在您指定的位置创建一个与您创建的视图完全相同的 pdf 文件。