【发布时间】:2021-10-07 20:53:24
【问题描述】:
所以我想做的很简单,我想修改现有的 pdf 文档。
它不会写入我添加的现有 pdf 文件,而是写入一个空白文件。
这里是代码。
<?php
require('vendor/autoload.php');
$mpdf = new mPDF();
$mpdf->AddPage();
// set the sourcefile
$mpdf->setSourceFile('hs.pdf');
// import page 1
$tplIdx = $mpdf->importPage(1);
// use the imported page and place it at point 10,10 with a width of 200 mm (This is the image of the included pdf)
$mpdf->useTemplate($tplIdx, 10, 10, 200);
// now write some text above the imported page
$mpdf->SetTextColor(0,0,255);
$mpdf->SetFont('Arial','B',8);
$mpdf->SetXY(95, 16);
$mpdf->Write(0, "Mindfire");
$mpdf->Output('newpdf.pdf');
这是我要写入的图像。 enter image description here
添加这是它输出的图像 enter image description here
正如您所看到的,它似乎只是每次都写入一个空白文档,而不是写入第一个 pdf。
有什么想法吗?
更新:
这是我的 composer.json 文件
{
"require": {
"mpdf/mpdf": "v5.5.1"
}
}
我尝试了所有不同版本的 mpdf,但仍然存在相同的错误。
Uncaught Error: Class 'Mpdf\Mpdf' not found in
【问题讨论】:
-
从您的代码来看,这应该是错误的。因为它应该是
$mpdf = new \Mpdf\Mpdf(),除非您使用的是 mPDF 版本<= 6.0,其中该类名是有效的,在这种情况下,您将错过对$mpdf->SetImportUse()或 full example 的调用 -
使用 $mpdf = new \Mpdf\Mpdf() 给我“未捕获的错误:找不到类 'Mpdf\Mpdf'”,当我使用 $mpdf = new \mPDF('utf-8 ', 'A4-L');我用composer安装了mpdf,不确定我在这里缺少什么..
-
我将我的版本切换到 ^v8.0.13 并且我仍然收到 Uncaught Error: Class 'Mpdf\Mpdf' not found in,无论版本如何,它仍然找不到,我想如果我解决了剩下的就好了,我复制你给我看的例子。
-
听起来 PHP 在 OPcache 中有原始源。您可能需要在运行
composer dump-autoload之前和/或之后在您的环境中使用 clear it,以便在版本 7.0+ 中使用new \Mpdf\Mpdf()。需要查看 composer.json 和可能的 phpinfo() 以确定可能导致失败的原因。 -
刚刚更新了我在 composer.json 文件中的内容,我尝试使用 composer dump-autoload 但仍然没有任何变化......