【问题标题】:Fatal error: Class 'TCPDF' not found in another path致命错误:在另一个路径中找不到类“TCPD”
【发布时间】:2016-12-23 09:34:05
【问题描述】:

当我们点击链接时,我们想要下载 php 页面数据为 Pdf 格式。

所以我们从官方git hub link下载了TCPDF

我们将提取的文件夹复制到路径:“/var/www/html/sbdev2/php/site6

当我们在浏览器中运行示例代码时:http://sbdev2.kidsdial.com:81/php/site6/tcpdf/examples/example_011.php 我们可以下载 pdf。

当我们在另一个路径中尝试相同的代码时:http://sbdev2.kidsdial.com:81/php/site6/example_011.php,我们收到错误为“Fatal error: Class 'TCPDF' not found in”:class MYPDF extends TCPDF {

我检查了link,但这对我不起作用。

我还检查了link2 并通过作曲家安装了 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');

编辑

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;
    }
}

编辑2

仅当我将文件包含在 /var/www/html/sbdev2/php/site6/tcpdf/ 文件夹示例的子文件夹中时它才起作用:

/var/www/html/sbdev2/php/site6/tcpdf/example1
/var/www/html/sbdev2/php/site6/tcpdf/example1

如果我将示例文件夹内容复制到另一个路径,假设: /var/www/html/sbdev2/php/site6/它根本不工作.....

【问题讨论】:

  • 你写的是你改变了你的代码路径,你有正确的TCPDF路径吗?
  • @dimis283 我将tcpdf_include.php 文件复制到/var/www/html/sbdev2/php/site6
  • @dimis283 抱歉,我将tcpdf_config.php 文件复制到/var/www/html/sbdev2/php/site6,因为我在tcpdf_config.php 文件夹下获得了tcpdf_config.php 文件......
  • 似乎在 git hub 链接上还有其他文件,可能所有这些文件都放在一个文件夹中,并从那里“包含”tcpdf_include.php?
  • @dimis283 抱歉,我没有,现在我将文件夹 tcpdf 复制到此路径:/var/www/html/sbdev2/php/site6,请告诉我需要复制哪个路径?

标签: php class tcpdf


【解决方案1】:

example 001 中更改了这一行:

// Include the main TCPDF library (search for installation path).
require_once('tcpdf_include.php');

进入这个:

// Include the main TCPDF library (search for installation path).
require_once('**C:\wamp\www\pdf2\tcpdf\tcpdf.php**');

它在WAMP 上对我有用。

希望对你有帮助!

【讨论】:

  • 这行不通,因为这是针对 Windows 的,而他使用的是 Linux。
【解决方案2】:

我也遇到了这个问题,终于找到了解决方案。 一切都很好,让每个文件都保持在原来的路径,只需要打开 tcpdf_include.php 并包含文件 tcpdf.php,仅此而已! require_once(...安装目录...\tcpdf.php'); 它会工作的,一切顺利

【讨论】:

    猜你喜欢
    • 2015-02-22
    • 2015-05-10
    • 1970-01-01
    • 2014-10-09
    • 1970-01-01
    • 2011-11-27
    • 2014-10-03
    • 2016-12-06
    • 2015-07-01
    相关资源
    最近更新 更多