2013-03-05 70 views
2

我正在使用FPDF()方法(来自FPDI_Protection.php)导入现有的PDF并应用密码保护。FPDF - 它能处理混合方向(纵向/横向)的原始PDF吗?

我遇到的问题是,原始PDF混合了纵向和横向页面(8.5“X11”& 11“X8.5”),而导入方法使您可以定义一次。我可以将新创建​​的pdf定义为11“X11”,它可以修复其中一个方向裁剪的问题,但这对于打印目的并不理想,因为PDF会缩放并左对齐,从而导致可读性/打印输出较差。

有没有什么样的例程可以使用,因为原始文档正在循环检测原始大小并设置新的页面方向?

function pdfEncrypt ($origFile, $password, $destFile) // RESPONSIBLE FOR ADDING PASSWORD PROTECTION TO PDF FILES 
{ 
    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('11','11')); 

    //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('print'), $password, ''); 
    $pdf->Output($destFile, 'F'); 

    return $destFile; 
} 

,或者,有没有添加密码来使用PHP现有的PDF更简单的方法?

+0

我想尝试的一件事是对TCPDF运行稍微修改后的代码。它是由同一个人写的,但定期更新。 – mkaatman 2013-03-05 23:19:48

+0

@mkaatman - 我对这个建议非常失望。在我看来,TCPDF要求你在构建时定义一个单一的大小,就像这样。 – 2013-03-06 07:00:55

+0

我希望进口会更优雅。 – mkaatman 2013-03-06 15:12:26

回答

3

好吧,我在这一天拉了几天头发。在不知疲倦地搜索与我的问题有关的术语的每一次迭代后,我能够找到一个实际解决方案的实例(我尝试安装pdflib lite,phpinfo,ghostscript,xpdf等等,来衡量维度无济于事)。什么工作是这样的(你需要的FPDI_Protection package [免费]):

$specs = $pdf->getTemplateSize($tplidx); 
    $pdf->addPage($specs['h'] > $specs['w'] ? 'P' : 'L'); 

完整的功能如下:

function pdfEncrypt ($origFile, $password, $destFile) // RESPONSIBLE FOR ADDING PASSWORD PROTECTION TO PDF FILES 
{ 
    require_once('fpdi/FPDI_Protection.php'); 

    $pdf = new FPDI_Protection(); 
    // set the format of the destinaton file 
    $pdf->FPDF('P', 'in', array('8.5','11')); 

    //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); 

     $specs = $pdf->getTemplateSize($tplidx); 
     $pdf->addPage($specs['h'] > $specs['w'] ? 'P' : 'L'); 
     $pdf->useTemplate($tplidx); 
    } 

    // protect the new pdf file 

    $pdf->SetProtection(array('print'), $password, ''); 
    $pdf->Output($destFile, 'F'); 

    return $destFile; 
} 

添加这些代码两行能够检测是否原装页面是风景画像,并以相同的方式在输出文件中重新创建页面。哈利路亚!

+0

您是否认为可以将横向的页面布局更改为纵向? – Bor 2013-11-19 16:05:02

+1

原始文件长达数百页,包含混合的方向。我想知道,如果在原有的加载和吐出一个安全的版本,它可以使用的回答我上面的时候发现FPDF可以处理这个正确。 – 2013-11-19 23:02:49

0

感谢您的帖子,我可以在一分钟内解决我的定位问题!

无论如何,你不需要特殊的FPDI_“保护”包单独更改方向,一个有效的解决方案只需要“FPDI”包(用于getTemplatesize功能)。 下面是从2012年8月到解决方案的链接: FPDF/FPDI addPage() Orientation

0

在我fpdf_tpl.php(1.6.1)在网页中加入的方法被称为AddPage没有addPage。看看如果你调用addPage方法不执行,你不能改变方向。

相关问题