【问题标题】:FPDF - Can it handle original PDF with mixed orientations (portrait/landscape)?FPDF - 它可以处理混合方向(纵向/横向)的原始 PDF 吗?
【发布时间】:2013-03-05 23:10:52
【问题描述】:

我正在使用 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 添加密码?

【问题讨论】:

  • 我会尝试的一件事是针对 TCPDF 运行稍微修改过的代码。它是由同一个人编写的,但会定期更新。
  • @mkaatman - 我对这个建议很迷茫。在我看来,TCPDF 要求您在构造时以与此相同的方式定义单一尺寸。
  • 我希望导入会更优雅。

标签: php fpdf fpdi


【解决方案1】:

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

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

【讨论】:

  • 您认为可以将页面布局从横向更改为纵向吗?
  • 原始文档长达数百页,包含混合方向。我想知道 FPDF 在加载原始版本并吐出安全版本时是否可以正确处理这个问题,它可以使用我在上面找到的答案。
【解决方案2】:

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

无论如何,您不需要单独的方向更改特殊的 FPDI_"Protection" 包,一个可行的解决方案只需要 "FPDI" 包(用于 getTemplatesize 函数)。 以下是 2012 年 8 月解决方案的链接: FPDF / FPDI addPage() Orientation

【讨论】:

    【解决方案3】:

    在我的 fpdf_tpl.php (1.6.1) 中,添加页面的方法称为 AddPage 而不是 addPage。看一下,如果您调用 addPage 该方法不会执行并且您无法更改方向。

    【讨论】:

      【解决方案4】:

      要使用 getTemplateSize 你只需要setasign/fpdi 包。 对于这个包的当前版本(v2.3.6),height 和 width 键不再是 'w' 和 'h' 而是 'width' 和 'height':

      (来自 setasign\Fpdi\Fpdi.php)

      /**
       * Get the size of an imported page or template.
       *
       * Give only one of the size parameters (width, height) to calculate the other one automatically in view to the
       * aspect ratio.
       *
       * @param mixed $tpl The template id
       * @param float|int|null $width The width.
       * @param float|int|null $height The height.
       * @return array|bool An array with following keys: width, height, 0 (=width), 1 (=height), orientation (L or P)
       */
      public function getTemplateSize($tpl, $width = null, $height = null)
      {
          ...
      }
      

      【讨论】:

        猜你喜欢
        • 2012-08-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-01-13
        • 1970-01-01
        • 2019-12-02
        相关资源
        最近更新 更多