【问题标题】:How to create a password protected pdf file如何创建受密码保护的 pdf 文件
【发布时间】:2011-02-02 12:00:08
【问题描述】:

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

【问题讨论】:

    标签: php pdf-generation password-protection fpdf


    【解决方案1】:

    我一直无法找到直接的 php 解决方案来解决这个问题。我最终使用pdftk 并使用shell_exec() 在生成/上传pdf文件后调用二进制文件。

    它接受这样的语法:

    pdftk 'inputfile.pdf' output 'outputfile.pdf' user_pw pass1234 owner_pw pass4321
    

    【讨论】:

    • @Mark 我明白了。试试这个我在这里发布的答案。
    • bleee,windows软件
    【解决方案2】:

    a blog post on the ID Security Suite site下载我正在使用的库:

    <?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 );
    ?>
    

    【讨论】:

    • 我们可以在没有密码的情况下在windows 8中查看这个加密文件。这是Windows 8中的失败。有什么解决方案吗?谢谢
    猜你喜欢
    • 1970-01-01
    • 2010-09-22
    • 2011-12-23
    • 2012-09-09
    • 2021-03-28
    • 1970-01-01
    • 1970-01-01
    • 2010-10-18
    • 1970-01-01
    相关资源
    最近更新 更多