3

我正在使用 html2fpdf 创建 PDF 文档。现在,一旦我创建了它,我想确保 PDF 文件受密码保护。如何在 PHP 中做到这一点?

4

2 回答 2

5

从ID Security Suite 网站上的博客文章下载我正在使用的库:

<?php
    function pdfEncrypt ($origFile, $password, $destFile){
        require_once('FPDI_Protection.php');
        $pdf =& new FPDI_Protection();
        $pdf->FPDF('P', 'in');
        //Calculate the number of pages from the original document.
        $pagecount = $pdf->setSourceFile($origFile);
        //Copy all pages from the old unprotected pdf in the new one.
        for ($loop = 1; $loop <= $pagecount; $loop++) {
            $tplidx = $pdf->importPage($loop);
            $pdf->addPage();
            $pdf->useTemplate($tplidx);
        }

        //Protect the new pdf file, and allow no printing, copy, etc. and
        //leave only reading allowed.
        $pdf->SetProtection(array(), $password);
        $pdf->Output($destFile, 'F');
        return $destFile;
    }

    //Password for the PDF file (I suggest using the email adress of the purchaser).
    $password = "testpassword";
    //Name of the original file (unprotected).
    $origFile = "sample.pdf";
    //Name of the destination file (password protected and printing rights removed).
    $destFile ="sample_protected.pdf";
    //Encrypt the book and create the protected file.
    pdfEncrypt($origFile, $password, $destFile );
?>
于 2010-03-31T05:29:42.213 回答
1

我从来没有找到解决这个问题的直接 php 解决方案。我最终使用pdftkshell_exec()在生成/上传 pdf 文件后使用来调用二进制文件。

它接受这样的语法:

pdftk 'inputfile.pdf' output 'outputfile.pdf' user_pw pass1234 owner_pw pass4321
于 2010-03-30T15:07:32.767 回答