我有两个密码,一个用来编辑已经上传的pdf,另一个用来用密码保护已经上传的pfd。
下面是代码片段
1)供pdf编辑
require_once('fpdf.php');
require_once('fpdi.php');
$pdf = new FPDI();
$filen="upload/json_tutorial.pdf";
$pageCount = $pdf->setSourceFile($filen);
// iterate through all pages
for ($pageNo = 1; $pageNo <= $pageCount; $pageNo++) {
// import a page
$tplidx = $pdf->importPage($pageNo);
$pdf->addPage();
$pdf->useTemplate($tplidx, 0, 0, 220,270);
$pdf->SetFont('Helvetica');
$pdf->SetTextColor(0, 0, 255);
$pdf->SetXY(5, 5);
$cur_page_no=$pdf->PageNo();
$min=2;
$max=10;
if((($pdf->PageNo())>=$min) && (($pdf->PageNo())<=$max))
{
$author="AuthorName";
$pdf->Cell(320,10,$author,0,0,'C');
}
}
$pdf->Output('newpdf.pdf', 'D');2)密码保护
function pdfEncrypt ($origFile, $password, $destFile)
{
require_once('FPDI_Protection.php');
$pdf =& new FPDI_Protection();
$pagecount = $pdf->setSourceFile($origFile);
for ($loop = 1; $loop <= $pagecount; $loop++)
{
$tplidx = $pdf->importPage($loop);
$pdf->addPage();
$pdf->useTemplate($tplidx);
}
$pdf->SetProtection(array(), $password,'');
$pdf->Output($destFile, 'D');
return $destFile;
}
$password = "pass123";
$origFile = "json_tutorial.pdf";
$destFile ="pd_protected.pdf";
pdfEncrypt($origFile, $password, $destFile );这两种代码都能正常工作。但当我试图把两者结合起来。它们中的任何一个从不像代码1使用更新的库那样工作,代码2使用旧的库。我试着用新的库处理代码2,但是给出的文件不能加载一些错误。
我在代码2中添加了代码1的功能,如下所示:
function pdfEncrypt ($origFile, $password, $destFile)
{
require_once('FPDI_Protection.php');
$pdf =& new FPDI_Protection();
$pagecount = $pdf->setSourceFile($origFile);
for ($pageNo = 1; $pageNo <= $pagecount; $pageNo++)
{
$tplidx = $pdf->importPage($pageNo);
$pdf->addPage();
$pdf->useTemplate($tplidx, 0, 0, 220,270);
$pdf->SetFont('Helvetica');
$pdf->SetTextColor(0, 155, 255);
$pdf->SetXY(5, 5);
$min=2;
$max=10;
if((($pdf->PageNo())>=$min) && (($pdf->PageNo())<=$max))
{
$author="KomalD";
$pdf->Cell(320,10,$author,0,0,'C');
}
}
$pdf->SetProtection(array(), $password,'');
$pdf->Output($destFile, 'D');
return $destFile;
}
$password = "pass123";
$origFile = "json_tutorial.pdf";
$destFile ="pd_protected.pdf";
pdfEncrypt($origFile, $password, $destFile );此代码将文件保存为受密码保护的文件,但根本不编辑它。没有给出任何错误或警告。我做错什么了??
请建议一下。谢谢
发布于 2014-06-03 06:41:24
首先,您应该将所有使用过的类更新到它们的当前版本:FPDF、FPDI和FPDI_Protection (参见前面的链接)。
在此之后,您只需需要所需的文件,最后一个脚本就可以正常工作了:
function pdfEncrypt ($origFile, $password, $destFile)
{
require_once('fpdf.php');
require_once('fpdi.php');
require_once('FPDI_Protection.php');
$pdf = new FPDI_Protection(); // <-- remove the "&"
$pagecount = $pdf->setSourceFile($origFile);
...https://stackoverflow.com/questions/23991145
复制相似问题