【问题标题】:Is it possible to remove a Password from a PDF file using PHP?是否可以使用 PHP 从 PDF 文件中删除密码?
【发布时间】:2023-03-28 09:30:01
【问题描述】:

我想问是否可以使用 PHP 从我已经知道密码的受密码保护的 PDF 文件中删除密码?我见过this page,它提供了许多选项,但使用的是 bash 脚本。 :( 我被要求尽可能多地使用 PHP。任何建议表示赞赏!

【问题讨论】:

标签: php pdf passwords


【解决方案1】:

当然有可能,您只需对加密和压缩进行逆向工程,并在 PHP 中实现逆向操作 - 但何必费心呢:

<?php
   `gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=unencrypted.pdf -c .setpdfwrite -f encrypted.pdf`;
?>

C.

【讨论】:

  • 嗨!谢谢回复。是的,我也在上面发布的链接上找到了这个。不过,我遇到了与 cyberciti.biz/faq/removing-password-from-pdf-on-linux/… 相同的错误。
  • 如果您知道密码,您可以在 gs 命令中添加-sPDFPassword=yourpassword
  • 您需要在 exec 中使用选项 -sPDFPassword=yourpassword 正如@SimonSobisch 建议的那样编写
【解决方案2】:

我在 linux 上使用qpdf 来删除 pdf 密码。安装运行:

sudo apt install qpdf

这是从 php 调用 qpdf 的函数:

function removePdfPassword($inFilePath, $password, $outFilePath)
{
    if (empty($inFilePath) || empty($password) || !file_exists($inFilePath)) {
        return false;
    }

    $cmd = 'qpdf -password=' . escapeshellarg($password) . ' -decrypt ' . escapeshellarg($inFilePath) . ' ' . escapeshellarg($outFilePath);

    exec($cmd, $output, $retcode);
    $success = $retcode == 0;

    if (!$success) {
        @unlink($outFilePath);
    }

    return $success;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-26
    相关资源
    最近更新 更多