PHP 致命错误:第 30 行找不到“DOMPDF”类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24497216/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:20:40  来源:igfitidea点击:

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

phppdfdompdf

提问by logan

I am using DOMPDF to generate PDFs from HTML. I have copied all required files from github (encoding branch). But it says class DOMPDF not error as below.

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

link for dompdf_config.inc.php in gitbub : https://github.com/dompdf/dompdf/tree/encoding

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

Here is my code :

这是我的代码:

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

Actual Output is :

实际输出是:

Fatal error: Class 'DOMPDF' not found in /home/web/www/test_dompdf.php on line 30

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

Line 30 is $dompdf = new DOMPDF();

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

Note:Other Master Branch is working fine. I need this encoding branch as it solves encoding font related issues.

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

回答by Marcin Nabia?ek

I've tested your code and it works fine for me - sample.pdf file is being downloaded in browser. I've downloaded library from https://github.com/dompdf/dompdf/releases/tag/v0.6.1url (not only the encoding branch(

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

Probably you haven't moved the whole project to selected directory or you haven't downloaded the whole library. I moved the whole downloaded directory content to APIs/dompdf-encodingdirectory and I have here files dompdf_config.inc.phpand directories lib, includeand www.

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

EDIT

编辑

As you edited you want to use only encoding branch, what you have to do is adding the following code at the beginning of your file:

当您编辑时,您只想使用编码分支,您需要做的是在文件开头添加以下代码:

use Dompdf\Adapter\CPDF;
use Dompdf\Dompdf;
use Dompdf\Exception;

EDIT2

编辑2

The whole working code:

整个工作代码:

<?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

I have also changed DOMPDFto Dompdfjust in case (in Windows both are working)

我也改变了DOMPDFDompdf以防万一(在Windows都是工作)