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

标签 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;
}

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

最佳答案

好吧,我在这个头发上拔了几天。在不知疲倦地谷歌搜索与我的问题相关的术语的每次迭代后,我能够找到一个实际有效的解决方案实例(我尝试安装 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;
}

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

关于php - FPDF - 它可以处理混合方向(纵向/横向)的原始 PDF 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15235935/

相关文章:

php - TCPDF 错误 : Unable to find object (8, 0) 在预期位置

php - FPDF 背景颜色不变

php - 未定义的字体 : In Fpdf

php - 使用 MySQL 或 PHP 防止重复条目的正确方法

php - 正则表达式包含一个字段

php - 在 FPDF 中按基线对齐文本

php - php 合并 pdf 后缺少可填写的表单值

php - FPDF 错误 : This document (testcopy. pdf) 可能使用了 FPDI 附带的免费解析器不支持的压缩技术

php - 在本地主机外运行 PHP 5.4 内置 Web 服务器

php - Laravel 将两个模型加入用户模型