PHP错误:Class 'DOMPDF'在第30行没有找到


PHP Fatal error : Class 'DOMPDF' not found on line 30

我使用DOMPDF从HTML生成pdf。我已经从github(编码分支)复制了所有所需的文件。但是它说的是类DOMPDF,而不是下面的错误。

gitbub中dompdf_config.inc.php的链接:https://github.com/dompdf/dompdf/tree/encoding

下面是我的代码:
require_once("APIs/dompdf-encoding/dompdf_config.inc.php");     
$cart_body='<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>New Order Placed</title></head><body><p>Test Printing...</p></body></html>';
        $dompdf = new DOMPDF();
        $dompdf->load_html($cart_body);//body -> html content which needs to be converted as pdf..
        $dompdf->render();
        $dompdf->stream("sample.pdf"); //To popup pdf as download

实际输出为:

致命错误:在/home/web/www/test_dompdf.php中找不到类'DOMPDF'第30行

第30行是$dompdf = new DOMPDF();

注意:其他主分支工作正常。我需要这个编码分支,因为它解决了编码字体相关问题。

我已经测试了你的代码,它对我来说很好- sample.pdf文件正在浏览器中下载。我已经从https://github.com/dompdf/dompdf/releases/tag/v0.6.1 url下载了库(不仅仅是编码分支)

可能您还没有将整个项目移动到选定目录,或者您还没有下载整个库。我将所有下载的目录内容移动到APIs/dompdf-encoding目录,我这里有文件dompdf_config.inc.php和目录lib, includewww

编辑

当您编辑时,您希望只使用编码分支,您必须做的是在文件开头添加以下代码:

use Dompdf'Adapter'CPDF;
use Dompdf'Dompdf;
use Dompdf'Exception;

EDIT2

整个工作代码:

<?php
use Dompdf'Adapter'CPDF;
use Dompdf'Dompdf;
use Dompdf'Exception;

        require_once("APIs/dompdf-encoding/dompdf_config.inc.php");     
$cart_body='<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>New Order Placed</title></head><body><p>Test Printing...</p></body></html>';
        $dompdf = new Dompdf();
        $dompdf->load_html($cart_body);//body -> html content which needs to be converted as pdf..
        $dompdf->render();
        $dompdf->stream("sample.pdf"); //To popup pdf as download

我也把DOMPDF改成了Dompdf,以防万一(在Windows中都可以工作)

此解决方案适用于2.0.2版本。

<?php
    require __DIR__ . '/vendor/autoload.php';
    
    use Dompdf'Dompdf;
    
    $html =
        '<html><body>' .
        '<p>Put your html here, or generate it with your favourite ' .
        'templating system.</p>' .
        '</body></html>';
    
    $dompdf = new Dompdf();
    $dompdf->loadHtml($html);
    $dompdf->render();
    $output = $dompdf->output();
    file_put_contents('/tmp/Brochure.pdf', $output);
相关文章: