我正在尝试按照以下链接整合pdf
http://www.yiiframework.com/wiki/529/create-generate-pdf-files-with-tcpdf-plugin-example-to-generate-table-with-tcpdf-plugin/
但是得到了这样的错误:
include(MYPDF.php):无法打开流:没有这样的文件或目录
任何线索?
答案 0 :(得分:1)
一步一步地做:
1)从here下载TCPDF。
2)将文件夹文件夹TCPDF-master从ZIP存档放到protected/extensions/tcpdf
。
3)在MYPDF
中创建protected/extensions
课程:
<?php
/**
* @abstract This Component Class is created to access TCPDF plugin for generating reports.
* @example You can refer http://www.tcpdf.org/examples/example_011.phps for more details for this example.
* @todo you can extend tcpdf class method according to your need here. You can refer http://www.tcpdf.org/examples.php section for
* More working examples.
* @version 1.0.0
*/
Yii::import('ext.tcpdf.*');
class MYPDF extends TCPDF {
// Load table data from file
public function LoadData($file) {
// Read file lines
$lines = file($file);
$data = array();
foreach($lines as $line) {
$data[] = explode(';', chop($line));
}
return $data;
}
// Colored table
public function ColoredTable($header,$data) {
// Colors, line width and bold font
$this->SetFillColor(255, 0, 0);
$this->SetTextColor(255);
$this->SetDrawColor(128, 0, 0);
$this->SetLineWidth(0.3);
$this->SetFont('', 'B');
// Header
$w = array(40, 35, 40, 45);
$num_headers = count($header);
for($i = 0; $i < $num_headers; ++$i) {
$this->Cell($w[$i], 7, $header[$i], 1, 0, 'C', 1);
}
$this->Ln();
// Color and font restoration
$this->SetFillColor(224, 235, 255);
$this->SetTextColor(0);
$this->SetFont('');
// Data
$fill = 0;
foreach($data as $row) {
$this->Cell($w[0], 6, $row[0], 'LR', 0, 'L', $fill);
$this->Cell($w[1], 6, $row[1], 'LR', 0, 'L', $fill);
$this->Cell($w[2], 6, number_format($row[2]), 'LR', 0, 'R', $fill);
$this->Cell($w[3], 6, number_format($row[3]), 'LR', 0, 'R', $fill);
$this->Ln();
$fill=!$fill;
}
$this->Cell(array_sum($w), 0, '', 'T');
}
}
?>
4)在控制器顶部添加MYPDF
类的导入(<?php
之后)。在控制器中创建操作以生成PDF(基于您发布的链接中的教程中的示例):
<?php
Yii::import('ext.MYPDF'); //here is import
class ReportController extends Controller
{
public function action reportPdf()
{
$pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
spl_autoload_register(array('YiiBase','autoload'));
// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetTitle("Selling Report -2013");
$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, "Selling Report -2013", "selling report for Jun- 2013");
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
$pdf->SetFont('helvetica', '', 8);
$pdf->SetTextColor(80,80,80);
$pdf->AddPage();
//Write the html
$html = "<div style='margin-bottom:15px;'>This is testing HTML.</div>";
//Convert the Html to a pdf document
$pdf->writeHTML($html, true, false, true, false, '');
$header = array('Country', 'Capital', 'Area (sq km)', 'Pop. (thousands)'); //TODO:you can change this Header information according to your need.Also create a Dynamic Header.
// data loading
$data = $pdf->LoadData(Yii::getPathOfAlias('ext.tcpdf').DIRECTORY_SEPARATOR.'examples'.DIRECTORY_SEPARATOR.'data'.DIRECTORY_SEPARATOR.'table_data_demo.txt'); //This is the example to load a data from text file. You can change here code to generate a Data Set from your model active Records. Any how we need a Data set Array here.
// print colored table
$pdf->ColoredTable($header, $data);
// reset pointer to the last page
$pdf->lastPage();
//Close and output PDF document
$pdf->Output('filename.pdf', 'I');
Yii::app()->end();
}
}