当我们点击链接时,我们想以Pdf格式下载php页面数据.
所以我们从官方的git hub链接下载了TCPDF
我们将提取的文件夹复制到路径:" / var/www/html/sbdev2/php/site6 "
当我们在浏览器中运行示例代码时:http://sbdev2.kidsdial.com:81 / php/site6/tcpdf/examples/example_011.php我们可以下载pdf.
当我们在另一条路径中尝试相同的代码时:httpFatal error: Class 'TCPDF' not found://sbdev2.kidsdial.com:81/php/site6/examples_011.php,我们收到错误为" in"in class:class MYPDF extends TCPDF {
我检查了链接,但这对我不起作用.
我也检查了link2并通过composer安装了TCPDF,如下图所示.但仍然存在错误.
example_011.php
require_once('tcpdf_config.php');
// extend TCPF with custom functions
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);
// 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);
$fill=!$fill;
}
$this->Cell(array_sum($w), 0, '', 'T');
}
}
// create new PDF document
$pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('Nicola Asuni');
// set default header data
$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' 011', 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);
// 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', '', 12);
// add a page
$pdf->AddPage();
// column titles
$header = array('Country', 'Capital', 'Area (sq km)', 'Pop. (thousands)');
// data loading
$data = $pdf->LoadData('data/table_data_demo.txt');
// print colored table
$pdf->ColoredTable($header, $data);
// ---------------------------------------------------------
// close and output PDF document
$pdf->Output('example_011.pdf', 'I');
Run Code Online (Sandbox Code Playgroud)
编辑
tcpdfconfig.php
require_once('config/tcpdf_config_alt.php');
// Include the main TCPDF library (search the library on the following directories).
$tcpdf_include_dirs = array(
realpath('../tcpdf.php'),
'/usr/share/php/tcpdf/tcpdf.php',
'/usr/share/tcpdf/tcpdf.php',
'/usr/share/php-tcpdf/tcpdf.php',
'/var/www/tcpdf/tcpdf.php',
'/var/www/html/tcpdf/tcpdf.php',
'/usr/local/apache2/htdocs/tcpdf/tcpdf.php'
);
foreach ($tcpdf_include_dirs as $tcpdf_include_path) {
if (@file_exists($tcpdf_include_path)) {
require_once($tcpdf_include_path);
break;
}
}
Run Code Online (Sandbox Code Playgroud)
EDIT2
只有当我在/ var/www/html/sbdev2/php/site6/tcpdf /文件夹示例的子文件夹中包含文件时才能工作:
/var/www/html/sbdev2/php/site6/tcpdf/example1
/var/www/html/sbdev2/php/site6/tcpdf/example1
Run Code Online (Sandbox Code Playgroud)
如果我将示例文件夹内容复制到另一个路径,请说: / var/www/html/sbdev2/php/site6 /它根本不工作.....
小智 8
我也遇到了这个问题,并得到了解决方案.一切都很好,让每个文件只保留在原来的路径上,只需要打开tcpdf_include.php并包含文件tcpdf.php,就是这样! require_once(... INSTALED DIRECTORY ...\tcpdf.php'); 它会工作,一切顺利
在示例 001中更改了这一行:
// Include the main TCPDF library (search for installation path).
require_once('tcpdf_include.php');
Run Code Online (Sandbox Code Playgroud)
进入这个:
// Include the main TCPDF library (search for installation path).
require_once('**C:\wamp\www\pdf2\tcpdf\tcpdf.php**');
Run Code Online (Sandbox Code Playgroud)
它对我有用WAMP。
希望能帮助到你!