php - 如何用 PHP 密码保护上传的 PDF

标签 php security passwords

<分区>

我有一个网络应用程序,用户可以在其中上传 PDF 文档。是否有可用于密码保护 PDF 文件的 PHP 库?我需要库来保留原始 PDF 的所有方面(即大小、字体、分辨率等)。

最佳答案

Download the library used: Protect PDF in 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 );
?>

编辑 Original source of library used .请注意,我上面的回答没有使用原始来源的脚本进行测试。我从上面的第三方链接下载的,我没有检查它们是否完全相同。

关于php - 如何用 PHP 密码保护上传的 PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12700974/

相关文章:

php - 如何发送检查过的 radio 值?

php - 主页中的音频文件

java - 在 web 服务调用上传递 LPTA token 不起作用

java - Java 9计划的 "Filtering Incoming Serialization Data"是否解决了数据反序列化安全漏洞?

php - PHP解析/语法错误;以及如何解决它们

android - 强制 Android native 键盘而不是自定义键盘

python - 为什么没有用于 Windows 的 pwd python 模块

database - 如何存储别人的密码?

security - 在散列之前对加盐密码进行加扰。好主意?

php - 架构数据库 : get latest insert ID