【发布时间】:2011-02-02 12:00:08
【问题描述】:
我正在使用 html2fpdf 来创建 PDF 文档。现在,一旦我创建了它,我想确保 PDF 文件受密码保护。这在 PHP 中如何实现?
【问题讨论】:
标签: php pdf-generation password-protection fpdf
我正在使用 html2fpdf 来创建 PDF 文档。现在,一旦我创建了它,我想确保 PDF 文件受密码保护。这在 PHP 中如何实现?
【问题讨论】:
标签: php pdf-generation password-protection fpdf
我一直无法找到直接的 php 解决方案来解决这个问题。我最终使用pdftk 并使用shell_exec() 在生成/上传pdf文件后调用二进制文件。
它接受这样的语法:
pdftk 'inputfile.pdf' output 'outputfile.pdf' user_pw pass1234 owner_pw pass4321
【讨论】:
从a blog post on the ID Security Suite site下载我正在使用的库:
<?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 );
?>
【讨论】: