如何在PHP中对PDF进行密码保护

4
我有一个网络应用程序,用户可以上传pdf文档。有没有一个php库可以用来保护pdf文件的密码?我需要该库保留原始pdf的所有方面(即大小、字体、分辨率等)。

你想要对文件的网络访问进行密码保护还是对实际文件进行保护? - Louis Huppenbauer
1
如何在PHP中为PDF文件设置密码的第一个谷歌搜索结果:http://www.idsecuritysuite.com/blog/password-protect-a-pdf-document-in-php - Peter
1
我想为文件本身设置密码。 - Favourite Onwuemene
1个回答

13

下载用于保护PDF的PHP库

<?php

function pdfEncrypt ($origFile, $password, $destFile){
//include the FPDI protection http://www.setasign.de/products/pdf-php-solutions/fpdi-protection-128/
require_once('fpdi/FPDI_Protection.php');

$pdf =& new FPDI_Protection();
// set the format of the destinaton file, in our case 6×9 inch
$pdf->FPDF('P', 'in', array('6','9'));

//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
$password = 'info@domain.com';

//name of the original file (unprotected)
$origFile = 'book.pdf';

//name of the destination file (password protected and printing rights removed)
$destFile ='book_protected.pdf';

//encrypt the book and create the protected file
pdfEncrypt($origFile, $password, $destFile );
?>

2
如果我想通过编程方式重新打开使用FPDF加密的文件,这是可能的吗? - Marcello Verona
我们如何保护PDF文件免受二进制字符串的攻击,而不是真实文件?并且使用生成的字符串在PHP页面上生成PDF:header(“Content-type:application/pdf;”);谢谢! - user3768564

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接