英文:
setasign/fpdf and setasign/fpdi to password protect a PDF in PHP
问题
我在使用setasign/fpdf和setasign/fpdi与fpdf/fpdf一起对上传的PDF文件进行密码保护时遇到了一些问题。
到目前为止,我在我的composer.json文件中有以下内容:
{
"require": {
"phpmailer/phpmailer": "^6.8",
"fpdf/fpdf": "^1.85",
"setasign/fpdf": "1.8.*",
"setasign/fpdi": "^2.0",
"setasign/fpdi-protection": "^2.0"
}
}
并且在处理PHP的页面上有以下内容:
<?php
require_once('vendor/autoload.php');
use setasign\Fpdi\Fpdi;
$pdf = new Fpdi();
$pageCount = $pdf->setSourceFile('book.pdf');
$tplIdx = $pdf->importPage(1);
$pdf->AddPage();
$pdf->useTemplate($tplIdx);
$permissions = array(
'print' => false,
'copy' => false,
'modify' => false,
'annot-forms' => false
);
$pdf->SetProtection($permissions);
$pdf->Output('protected_pdf.pdf', 'D');
但是然后我收到以下错误:
致命错误: 未找到类 'Fpdi' 在 /home/
如果我省略 $pdf->SetProtection($permissions);
那么脚本就正常运行并且PDF文件会输出。
下面这段代码:
require_once('vendor/autoload.php');
use setasign\Fpdi\Fpdi;
require_once('vendor/setasign/fpdf/fpdf.php');
require_once('vendor/setasign/fpdi/src/autoload.php');
require_once('vendor/setasign/fpdi-protection/src/autoload.php');
$pdf = new Fpdi();
$pageCount = $pdf->setSourceFile('book.pdf');
$tplIdx = $pdf->importPage(1);
$pdf->AddPage();
$pdf->useTemplate($tplIdx);
$permissions = array(
'print' => false,
'copy' => false,
'modify' => false,
'annot-forms' => false
);
$pdf->SetProtection($permissions);
$pdf->Output('protected_pdf.pdf', 'D');
也会产生相同的错误。
以下这段代码:
<?php
require_once('vendor/autoload.php');
require_once('vendor/fpdf/fpdf/src/Fpdf/Fpdf.php');
require_once('vendor/setasign/fpdf/fpdf.php');
require_once('vendor/setasign/fpdi/src/autoload.php');
require_once('vendor/setasign/fpdi-protection/src/FpdiProtection.php');
require_once('vendor/tecnickcom/tcpdf/tcpdf.php');
use setasign\Fpdi\Fpdi;
$sourceFile = 'book.pdf';
$outputFile = 'book2.pdf';
$password = 'your_password';
$pdf = new FPDI();
$pageCount = $pdf->setSourceFile($sourceFile);
for ($pageNumber = 1; $pageNumber <= $pageCount; $pageNumber++) {
$importedPage = $pdf->importPage($pageNumber);
$pdf->AddPage();
$pdf->useTemplate($importedPage);
}
$pdf->SetProtection(array(), $password);
$pdf->Output($outputFile, 'F');
也尝试了,但仍然出现问题。
英文:
I am having some trouble using the setasign/fpdf and setasign/fpdi with fpdf/fpdf to password protect an uploaded pdf file.
So far I have this in my composer.json
{
"require": {
"phpmailer/phpmailer": "^6.8",
"fpdf/fpdf": "^1.85",
"setasign/fpdf": "1.8.*",
"setasign/fpdi": "^2.0",
"setasign/fpdi-protection": "^2.0"
}
}
and this on the page that handles the php.
<?php
require_once('vendor/autoload.php');
use setasign\Fpdi\Fpdi;
$pdf = new Fpdi();
$pageCount = $pdf->setSourceFile('book.pdf');
$tplIdx = $pdf->importPage(1);
$pdf->AddPage();
$pdf->useTemplate($tplIdx);
$permissions = array(
'print' => false,
'copy' => false,
'modify' => false,
'annot-forms' => false
);
$pdf->SetProtection($permissions);
$pdf->Output('protected_pdf.pdf', 'D');
but then I get the following error:
Fatal error: Uncaught Error: Class 'Fpdi' not found in /home/
If i omit $pdf->SetProtection($permissions);
then the script runs normally and the pdf is outputted.
require_once('vendor/autoload.php');
use setasign\Fpdi\Fpdi;
require_once('vendor/setasign/fpdf/fpdf.php');
require_once('vendor/setasign/fpdi/src/autoload.php');
require_once('vendor/setasign/fpdi-protection/src/autoload.php');
$pdf = new Fpdi();
$pageCount = $pdf->setSourceFile('book.pdf');
$tplIdx = $pdf->importPage(1);
$pdf->AddPage();
$pdf->useTemplate($tplIdx);
$permissions = array(
'print' => false,
'copy' => false,
'modify' => false,
'annot-forms' => false
);
$pdf->SetProtection($permissions);
$pdf->Output('protected_pdf.pdf', 'D');
also gives the same error.
<?php
require_once('vendor/autoload.php');
require_once('vendor/fpdf/fpdf/src/Fpdf/Fpdf.php');
require_once('vendor/setasign/fpdf/fpdf.php');
require_once('vendor/setasign/fpdi/src/autoload.php');
require_once('vendor/setasign/fpdi-protection/src/FpdiProtection.php');
require_once('vendor/tecnickcom/tcpdf/tcpdf.php');
use setasign\Fpdi\Fpdi;
$sourceFile = 'book.pdf';
$outputFile = 'book2.pdf';
$password = 'your_password';
$pdf = new FPDI();
$pageCount = $pdf->setSourceFile($sourceFile);
for ($pageNumber = 1; $pageNumber <= $pageCount; $pageNumber++) {
$importedPage = $pdf->importPage($pageNumber);
$pdf->AddPage();
$pdf->useTemplate($importedPage);
}
$pdf->SetProtection(array(), $password);
$pdf->Output($outputFile, 'F');
also tried.
答案1
得分: 1
我认为在所有示例中你都没有得到相同的错误消息,而它们是不同的。setProtection()
方法在默认的FPDI类中不可用。请使用FpdiProtection类并保持Composer安装不变:
<?php
use setasign\FpdiProtection\FpdiProtection;
// 设置自动加载函数
require_once('vendor/autoload.php');
$pdf = new FpdiProtection();
$ownerPassword = $pdf->setProtection(
FpdiProtection::PERM_PRINT | FpdiProtection::PERM_COPY,
'用户密码',
'所有者密码'
);
英文:
I don't think that you get the same error message in all examples but they are different. The method setProtection()
is not available in the default FPDI class. Stay at the composer installation and use the class of FpdiProtection:
<?php
use setasign\FpdiProtection\FpdiProtection;
// setup the autoload function
require_once('vendor/autoload.php');
$pdf = new FpdiProtection();
$ownerPassword = $pdf->setProtection(
FpdiProtection::PERM_PRINT | FpdiProtection::PERM_COPY,
'the user password',
'the owner password'
);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论