FPDF - 它可以处理混合方向(纵向/横向)的原始 PDF 吗?

Jam*_*one 5 php fpdf fpdi

我正在使用 FPDF() 方法(来自 FPDI_Protection.php)导入现有的 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;
}
Run Code Online (Sandbox Code Playgroud)

或者,是否有一种更简单的方法可以使用 php 向现有 pdf 添加密码?

Jam*_*one 6

好吧,我在这个头发上拔了几天。在不知疲倦地谷歌搜索与我的问题相关的每一次迭代后,我找到了一个实际有效的解决方案实例(我尝试安装 pdflib lite、phpinfo、ghostscript、xpdf 等,以测量尺寸无济于事)。有效的是这个(你需要FPDI_Protection 包[免费]):

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

完整的功能如下:

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;
}
Run Code Online (Sandbox Code Playgroud)

这两行代码的添加能够检测原始页面是否为横向肖像,并以相同的方式在输出文件中重新创建页面。哈利路亚。