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

发布于 2024-08-27 10:53:15 字数 80 浏览 8 评论 0原文

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

I'm using html2fpdf for creating PDF documents. Now once I have created that, I want to make sure that the PDF file is password protected. How can this be done in PHP?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

转瞬即逝 2024-09-03 10:53:15

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

Download the library I am using from 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 );
?>
温柔戏命师 2024-09-03 10:53:15

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

它接受这样的语法:

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

I was never able to find a direct php solution to this problem. I ended up using pdftk and using shell_exec() to call the binary once the pdf file was generated/uploaded.

It accepts a syntax like this:

pdftk 'inputfile.pdf' output 'outputfile.pdf' user_pw pass1234 owner_pw pass4321
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文