Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何创建受密码保护的pdf文件_Php_Pdf Generation_Password Protection_Fpdf - Fatal编程技术网

Php 如何创建受密码保护的pdf文件

Php 如何创建受密码保护的pdf文件,php,pdf-generation,password-protection,fpdf,Php,Pdf Generation,Password Protection,Fpdf,我正在使用html2fpdf创建PDF文档。现在,一旦我创建了它,我想确保PDF文件受密码保护。如何在PHP中实现这一点?我一直无法找到直接的PHP解决方案来解决这个问题。在生成/上传pdf文件后,我使用并使用shell\u exec()调用二进制文件 它接受如下语法: pdftk 'inputfile.pdf' output 'outputfile.pdf' user_pw pass1234 owner_pw pass4321 从以下位置下载我正在使用的库: @Mark我知道了..试试这个

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

我一直无法找到直接的PHP解决方案来解决这个问题。在生成/上传pdf文件后,我使用并使用
shell\u exec()
调用二进制文件

它接受如下语法:

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

从以下位置下载我正在使用的库:



@Mark我知道了..试试这个,我在这里发布的是answer.blee,windows软件我们可以在windows 8中查看这个加密文件,无需密码。这是Windows8中的一个失败。有什么解决办法吗。谢谢
<?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 );
?>