【问题标题】:Fatal error: Class 'TCPDF' not found致命错误:找不到类“TCPD”
【发布时间】:2015-02-22 14:21:02
【问题描述】:

我正在生成 PDF 文件,但遇到了麻烦。谁能告诉我这个错误的解决方案。下面是我用于此的代码。我为此包含了 tcpdf,但是有一个致命错误,说 tcpdf 文件不可用或者我们可以说找不到。

<?php
    $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;
        }
    }

    $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); // Create New PDF Doc
    $pdf->SetCreator(PDF_CREATOR); //Setup a document information

    include("db_connection.php");       

    $isql = mysqli_query($con, "SELECT * FROM `cpdf`") OR die("Data Select problem: ".mysqli_error($con));
    $num = mysqli_num_rows($isql);

    $pdf->AddPage(); //Add new page

    while($row = mysqli_fetch_array($isql)){
        $firstname = $row['fname'];
        $lastname = $row['lname'];
        $fullname = $firstname ." ".$lastname;
        ?>
<html>
    <head>
        <title>PDF Page</title>
    </head>
    <body>
        <form>
            <div>
                <div><img style="width: 100%; height: 20%" src="image/FlZJRBAXRlweb design.idevtechnolabs.socialsigna.l.JPG"/></div>
                <div style="width: 100%; height: 30%">
                    <table style="float: right; border: 2;">
                        <tr style="width: 10%;"><td style="text-align: left;"><?php echo $fullname; ?></td></tr>
                        <tr style="width: 20%;"><td style="text-align: left;"><?php echo $row['add1']; ?></td></tr>
                        <tr style="width: 20%;"><td style="text-align: left;"><?php echo $row['add2']; ?></td></tr>
                        <tr style="width: 20%;"><td style="text-align: left;"><?php echo $row['city']; ?></td></tr>
                        <tr style="width: 20%;"><td style="text-align: left;"><?php echo $row['state']; ?></td></tr>
                        <tr style="width: 20%;"><td style="text-align: left;"><?php echo $row['date']; ?></td></tr>
                    </table>
                </div>
                <div style="float: left; height: 10%;">
                    <b>Subject:</b> <?php echo $row['subject']; ?>
                </div>
                <div style="height: 40%; width: 100%;">
                    <p style="text-align: justify;">Hey, <br /><br /> Mr. <?php echo $fullname; ?>. This is to inform you that a day after tomorrow is the last date of your invoice payment. This is a reminder notice to pay your <?php echo $row['bill']; ?> invoice amount our nearest store and takes advantage of our services.</p>
                    <p style="text-align: justify; text-indent: 4em;">We request you to pay your <?php echo $row['bill']; ?> Bill as soon as possible.</p>
                    <span style="float: right;">Thank you,<br /><img src="#"/></span>
                </div><?php $pdf->Write(0, $txt, '', 0, 'C', true, 0, false, false, 0); 
                } 
                $pdf->Output('example_002.pdf', 'I'); ?>
            </div>
        </form>
    </body>
</html>

【问题讨论】:

  • 您的 tcpdf.php 中的类实际上是否称为 TCPDF?您也可能有相对路径问题。这是您得到的唯一错误还是得到堆栈跟踪?
  • 是的,我只遇到这个错误。
  • $pdf = new TCPDF( 之前,您应该使用if(!class_exists('TCPDF')){die('TCPDF could not be loaded. Abort!');},这样您的致命错误就会消失。

标签: php pdf-generation tcpdf


【解决方案1】:

作为第一步,注释掉前七行代码并使用文件的完整路径添加包含 tcpdf.php 的新代码行。如果这不起作用,那么您应该检查 tcpdf.php 文件的内容,以确保它在途中的某个地方没有被损坏。也就是说,确保它仍然包含声明 TCPDF 类的代码。

如果可行,则注释掉硬编码的 include 语句,取消注释之前注释掉的七行,并添加 echo 语句以在该循环的每次迭代期间打印出 $tcpdf_include_path 变量。只要其中一个路径与之前可能工作的硬编码路径匹配,那么您的代码应该可以正常工作。

最后,我非常怀疑情况是否如此,但如果您碰巧有一个区分大小写的文件系统,并且碰巧将 tcpdf.php 文件保存为 TCPDF.PHP,那么这可能有助于问题。我只是提到了答案完整性的可能性,但这是极不可能的,尤其是根据您提供的信息,这就是您的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-03
    • 1970-01-01
    • 2011-09-08
    • 2011-12-17
    • 2018-12-21
    • 2012-11-21
    • 2015-03-20
    • 2015-03-28
    相关资源
    最近更新 更多