所以我想做的很簡單,我想修改現有的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');
這是我想寫入的影像。 在此處輸入圖片說明
添加這是它輸出的 影像 在此處輸入影像描述
正如您所看到的,它似乎只是每次都寫入一個空白檔案,而不是寫入第一個 pdf。
有任何想法嗎 ?
更新:
這是我的 composer.json 檔案
{
"require": {
"mpdf/mpdf": "v5.5.1"
}
}
我嘗試了所有不同版本的 mpdf,但仍然存在相同的錯誤。
Uncaught Error: Class 'Mpdf\Mpdf' not found in
uj5u.com熱心網友回復:
似乎圍繞 mPDF 版本的語法和作曲家的用法存在一些混淆。
由于您嘗試了一些不明智的解決方法,我建議重置 composer 環境并重新安裝 mPDF。
將專案目錄設定為您的 CWD
cd /path/to/project
洗掉 Composer 管理的檔案
作業系統
rm -rf ./vendor
rm ./composer.json
rm ./composer.lock
Windows 作業系統 cmd
rmdir /Q /S .\vendor
del .\composer.json
del .\composer.lock
Windows 作業系統 PowerShell
Remove-Item -Recurse -Force .\vendor
Remove-Item .\composer.json
Remove-Item .\composer.lock
重新安裝 mPDF 庫檔案
composer require mpdf/mpdf
您的專案目錄應包含以下內容:
其中pdf_creator.php是用于生成 PDF 的腳本。
project/
composer.json
hs.pdf
pdf_creator.php
vendor/
mpdf/
autoload.php
...
檢查 composer.json 檔案中的 mPDF 版本
根據版本使用以下示例之一。
{
"require": {
"mpdf/mpdf": "^8.0"
}
}
mPDF 4.3 到 6.x
方法名稱使用pascal-cased模式
沒有命名空間
Classname 是mPDF()
Example: example41_MPDFI_template.php
<?php
/* pdf_creator.php */
require_once __DIR__ . '/vendor/autoload.php';
$mpdf = new mPDF();
// set the sourcefile
$mpdf->SetImportUse(); // <--- required for mPDF versions < 8.0
$mpdf->SetSourceFile(__DIR__ . '/hs.pdf'); // absolute path to pdf file
// 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');
mPDF 7.x
Method names use pascal-cased pattern
Introduced the \Mpdf namespace
Classname is Mpdf()
Example Importing Files & Templates
<?php
/* pdf_creator.php */
require_once __DIR__ . '/vendor/autoload.php';
$mpdf = new \Mpdf\Mpdf();
// set the sourcefile
$mpdf->SetImportUse(); // <--- required for mPDF versions < 8.0
$mpdf->SetSourceFile(__DIR__ . '/hs.pdf'); // absolute path to pdf file
// 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');
mPDF 8.x
Method names use camel-cased pattern
Introduced the \Mpdf namespace
Classname is Mpdf()
Method Mpdf::SetImportUse() was removed
Example Importing Files & Templates
<?php
/* pdf_creator.php */
require_once __DIR__ . '/vendor/autoload.php';
$mpdf = new \Mpdf\Mpdf();
// set the sourcefile
// $mpdf->SetImportUse(); // <--- not needed for mPDF version 8.0
$mpdf->setSourceFile(__DIR__ . '/hs.pdf'); // absolute path to pdf file
// 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');
Now run your script from the CLI to see it emits any errors.
cd /path/to/project
php pdf_creator.php
Note
$mpdf->AddPage();is not needed for editing a PDF file, unless adding another page to the resulting output PDF.
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/311210.html
