首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用fpdi编辑和保护pdf

用fpdi编辑和保护pdf
EN

Stack Overflow用户
提问于 2014-06-02 09:20:33
回答 1查看 2.9K关注 0票数 0

我有两个密码,一个用来编辑已经上传的pdf,另一个用来用密码保护已经上传的pfd。

下面是代码片段

1)供pdf编辑

代码语言:javascript
复制
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)密码保护

代码语言:javascript
复制
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的功能,如下所示:

代码语言:javascript
复制
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 );

此代码将文件保存为受密码保护的文件,但根本不编辑它。没有给出任何错误或警告。我做错什么了??

请建议一下。谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-06-03 06:41:24

首先,您应该将所有使用过的类更新到它们的当前版本:FPDFFPDI和FPDI_Protection (参见前面的链接)。

在此之后,您只需需要所需的文件,最后一个脚本就可以正常工作了:

代码语言:javascript
复制
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);
    ...
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23991145

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档