首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在PHP中创建一个加密的PDF,可以在PDF阅读器中下载后解密?

如何在PHP中创建一个加密的PDF,可以在PDF阅读器中下载后解密?
EN

Stack Overflow用户
提问于 2014-02-05 21:16:19
回答 2查看 9K关注 0票数 2

我想用PHP加密PDF文件(我已经在网上找到了一些解决方案),但是我想让它在用户桌面上的PDF应用程序中直接解密(而不是从PHP)。

这个是可能的吗?

设想情况:

  1. 用户填写表单并提交
  2. 服务器创建加密的PDF并将其发送给某人。
  3. 接收者收到新邮件并打开加密PDF附件
  4. PDF在他们的PDF阅读器中打开,并请求一个私钥(最好)或密码来解密
EN

回答 2

Stack Overflow用户

发布于 2014-02-05 21:25:22

执行以下操作:(取自:如何创建受密码保护的pdf文件)

http://www.idsecuritysuite.com/blog/wp-content/uploads/fpdi.zip

代码语言:javascript
运行
复制
<?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 );
?>
票数 4
EN

Stack Overflow用户

发布于 2014-02-05 21:24:21

这不是一个好主意,因为这样你应该:

  1. 在生成的PDF文件中存储一个明显不可靠的密钥或密码;
  2. 或者:将输入的密钥发送到某些服务器,但是PDF格式不允许这样做;
  3. 或者:使用一些自定义的非标准扩展来实现您自己的PDF阅读器,这也不是最好的决定。

我认为最好是使用加密和密钥或其他一些基于web的文档查看器。

UPD:专有PDFlib允许兼容加密

UPD2:开源iSafePDF也允许它。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21588687

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档