开发者

Is it possible to remove a Password from a PDF file using PHP?

开发者 https://www.devze.com 2023-01-01 14:01 出处:网络
I would like to ask if it\'s possible to use PHP in removing a password from a password-protected PDF file in which I already know the password? I\'ve seen this page which provides many options but us

I would like to ask if it's possible to use PHP in removing a password from a password-protected PDF file in which I already know the password? I've seen this page which provides many options but using bash script. :( I was requi开发者_StackOverflowred to use PHP as much as possible. Any suggestions appreciated!


Of course it's possible, all you need to do is reverse engineer the encryption and compression and implement the reverse operations in PHP - but why bother:

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

C.


I use qpdf on linux to remove pdf password. To install run:

sudo apt install qpdf

Here is the function which invokes qpdf from php:

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;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号