Example of thinkPHP Framework Integrating tcpdf Plug in Operation
- 2021-10-25 06:08:46
- OfStack
This article illustrates how the thinkPHP framework integrates tcpdf plug-in operations. Share it for your reference, as follows:
I checked some tutorials about the use of tcpdf on the Internet. If I integrate it into TP, I will have some small problems. Because the foundation is not very solid, I took some time to finally integrate OK. Here are the steps:
Environment:
TP version: TP 3.2. 2
tcpdf:tcpdf_6_2_3
Steps:
1. Unzip tcpdf_6_2_3.zip under the Web root directory, copy the file tcpdf_include. php under the examples folder to the tcpdf folder, and replace the contents of tcpdf/config/tcpdf_config. php with the contents of tcpdf/examples/config/tcpdf_config_ES40php
Note here that in tcp_include. php
$tcpdf_include_dirs
To add one more row to the array: "
realpath('./').'/tcpdf/tcpdf.php'
, "
2. New test. php
<?php
// Include the main TCPDF library (search for installation path).
require_once('./tcpdf/tcpdf_include.php');
// 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('Nicola Asuni');
$pdf->SetTitle('TCPDF Example');
$pdf->SetSubject('TCPDF Tutorial');
$pdf->SetKeywords('TCPDF, PDF, example, test, guide');
// set default header data
$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' wisvalley', 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));
// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
// set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
// set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
// 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('helvetica', '', 20);
$pdf->SetFont('stsongstdlight', '', 20);
// add a page
$pdf->AddPage();
$txt = 'your content';
$pdf->Write(0, $txt, '', 0, 'L', true, 0, false, false, 0);
$pdf->Output('/var/www/example_038.pdf', 'I');// Browser preview
//$pdf->Output('example_038.pdf', 'F');// Store files
//$pdf->Output('example_038.pdf', 'D');// Download a file
That's it.
Here are some problems I encountered:
1. I copied these codes into a method of the TP controller and reported an error: Class 'Home\ Controller\ TCPDF' not found
Answer:
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
Change to
$pdf = new \TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
For tp 3.2, namespaces were introduced, and this'
\
'It's important
2.TCPDF ERROR: Unable to create output file: example_038.pdf
Answer:
$pdf->Output('/var/www/example_038.pdf', 'I');
The path should be the right path.
Attachment: tcpdf plug-in click here to download this site.
For more readers interested in thinkPHP related contents, please check the topics of this site: "ThinkPHP Introduction Tutorial", "thinkPHP Template Operation Skills Summary", "ThinkPHP Common Methods Summary", "codeigniter Introduction Tutorial", "CI (CodeIgniter) Framework Advanced Tutorial", "Zend FrameWork Framework Introduction Tutorial" and "PHP Template Technology Summary".
I hope this article is helpful to the PHP programming based on ThinkPHP framework.