setasign/fpdf 和 setasign/fpdi 用于在 PHP 中对 PDF 进行密码保护。

huangapple go评论73阅读模式
英文:

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

{
    &quot;require&quot;: {
        &quot;phpmailer/phpmailer&quot;: &quot;^6.8&quot;,
        &quot;fpdf/fpdf&quot;: &quot;^1.85&quot;,
        &quot;setasign/fpdf&quot;: &quot;1.8.*&quot;,
        &quot;setasign/fpdi&quot;: &quot;^2.0&quot;,
        &quot;setasign/fpdi-protection&quot;: &quot;^2.0&quot;
    }
}

and this on the page that handles the php.

&lt;?php
require_once(&#39;vendor/autoload.php&#39;);
use setasign\Fpdi\Fpdi;
$pdf = new Fpdi();
$pageCount = $pdf-&gt;setSourceFile(&#39;book.pdf&#39;);
$tplIdx = $pdf-&gt;importPage(1);
$pdf-&gt;AddPage();
$pdf-&gt;useTemplate($tplIdx);
$permissions = array(
    &#39;print&#39; =&gt; false,
    &#39;copy&#39; =&gt; false,
    &#39;modify&#39; =&gt; false,
    &#39;annot-forms&#39; =&gt; false
);
$pdf-&gt;SetProtection($permissions);
$pdf-&gt;Output(&#39;protected_pdf.pdf&#39;, &#39;D&#39;);

but then I get the following error:
Fatal error: Uncaught Error: Class &#39;Fpdi&#39; not found in /home/

If i omit $pdf-&gt;SetProtection($permissions); then the script runs normally and the pdf is outputted.

require_once(&#39;vendor/autoload.php&#39;);

use setasign\Fpdi\Fpdi;

require_once(&#39;vendor/setasign/fpdf/fpdf.php&#39;);
require_once(&#39;vendor/setasign/fpdi/src/autoload.php&#39;);
require_once(&#39;vendor/setasign/fpdi-protection/src/autoload.php&#39;);

$pdf = new Fpdi();
$pageCount = $pdf-&gt;setSourceFile(&#39;book.pdf&#39;);
$tplIdx = $pdf-&gt;importPage(1);
$pdf-&gt;AddPage();
$pdf-&gt;useTemplate($tplIdx);
$permissions = array(
    &#39;print&#39; =&gt; false,
    &#39;copy&#39; =&gt; false,
    &#39;modify&#39; =&gt; false,
    &#39;annot-forms&#39; =&gt; false
);
$pdf-&gt;SetProtection($permissions);
$pdf-&gt;Output(&#39;protected_pdf.pdf&#39;, &#39;D&#39;);

also gives the same error.

&lt;?php

require_once(&#39;vendor/autoload.php&#39;);
require_once(&#39;vendor/fpdf/fpdf/src/Fpdf/Fpdf.php&#39;);
require_once(&#39;vendor/setasign/fpdf/fpdf.php&#39;);
require_once(&#39;vendor/setasign/fpdi/src/autoload.php&#39;);
require_once(&#39;vendor/setasign/fpdi-protection/src/FpdiProtection.php&#39;);
require_once(&#39;vendor/tecnickcom/tcpdf/tcpdf.php&#39;);

use setasign\Fpdi\Fpdi;

$sourceFile = &#39;book.pdf&#39;;
$outputFile = &#39;book2.pdf&#39;;

$password = &#39;your_password&#39;;

$pdf = new FPDI();

$pageCount = $pdf-&gt;setSourceFile($sourceFile);

for ($pageNumber = 1; $pageNumber &lt;= $pageCount; $pageNumber++) {
    $importedPage = $pdf-&gt;importPage($pageNumber);
    $pdf-&gt;AddPage();
    $pdf-&gt;useTemplate($importedPage);
}

$pdf-&gt;SetProtection(array(), $password);

$pdf-&gt;Output($outputFile, &#39;F&#39;);

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:

&lt;?php
use setasign\FpdiProtection\FpdiProtection;

// setup the autoload function
require_once(&#39;vendor/autoload.php&#39;);

$pdf = new FpdiProtection();
$ownerPassword = $pdf-&gt;setProtection(
    FpdiProtection::PERM_PRINT | FpdiProtection::PERM_COPY,
    &#39;the user password&#39;,
    &#39;the owner password&#39;
);

huangapple
  • 本文由 发表于 2023年5月29日 06:47:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76353870.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定