致命错误:在另一个路径中找不到“TCPDF”类

时间:2016-12-23 09:34:06

标签: php class tcpdf

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

所以我们从官方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”in the line: class MYPDF扩展TCPDF {

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

我还检查了link2&通过composer安装TCPDF如下图所示。但仍然存在错误。

enter image description here

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

EDIT2

仅当我在 / 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 / 它根本不起作用.....

2 个答案:

答案 0 :(得分:6)

我也遇到了这个问题,并且得到了解决方案。 一切都很好,让每个文件只保留在原来的路径上,只需要打开tcpdf_include.php并包含文件tcpdf.php,就是这样! require_once(... INSTALED DIRECTORY ... \ tcpdf.php&#39;); 它会工作,一切顺利

答案 1 :(得分:1)

示例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

希望它有所帮助!