【问题标题】:PDF Rotate with FPDF and Laravel使用 FPDF 和 Laravel 旋转 PDF
【发布时间】:2016-02-13 17:04:57
【问题描述】:

如何使用 FPDF 旋转 PDF 文件?从我看到的文档中它只能生成/创建pdf然后旋转它,而不是来自外部源..

【问题讨论】:

    标签: php pdf fpdf laravel-5.2


    【解决方案1】:
     Rotate(float angle [, float x [, float y]])
    

    角度:角度,以度为单位。 x:旋转中心的横坐标。默认

    :当前位置。 y:旋转中心的纵坐标。默认

    值:当前位置。

    旋转会影响方法调用后打印的所有元素(可点击区域除外)。

    • 仅更改显示。 GetX() 和 GetY() 方法不受影响,自动分页机制也不受影响。
    • 不会在页面之间保持旋转。每个页面都以空旋转开始。

    这是一个示例,它定义了实用方法 RotatedText() 和 RotatedImage() 并使用它们打印文本和旋转到 45° 的图像。

    <?php
     require('rotation.php');
    
     class PDF extends PDF_Rotate
     {
       function RotatedText($x,$y,$txt,$angle)
      {
       //Text rotated around its origin
       $this->Rotate($angle,$x,$y);
       $this->Text($x,$y,$txt);
       $this->Rotate(0);
      }
    
      function RotatedImage($file,$x,$y,$w,$h,$angle)
      {
       //Image rotated around its upper-left corner
       $this->Rotate($angle,$x,$y);
       $this->Image($file,$x,$y,$w,$h);
       $this->Rotate(0);
      }
    }
    
    $pdf=new PDF();
    $pdf->AddPage();
    $pdf->SetFont('Arial','',20);
    $pdf->RotatedImage('circle.png',85,60,40,16,45);
    $pdf->RotatedText(100,60,'Hello!',45);
    $pdf->Output();
    ?>
    

    更新::

    FPDI - 从现有 PDF 文档中导入页面并将其用作 FPDF 中的模板。

    FPDI 是 PHP 类的集合,方便开发人员阅读 来自现有 PDF 文档的页面并将其用作 FPDF 中的模板, 由 Olivier Plathey 开发。除了 FPDF 的副本, FPDI 不需要任何特殊的 PHP 扩展。

     <?php
       require_once('fpdf.php');
       require_once('fpdi.php');
    
       $pdf = new FPDI();
    
       $pageCount = $pdf->setSourceFile("Fantastic-Speaker.pdf");
       $tplIdx = $pdf->importPage(1, '/MediaBox');
    
       $pdf->addPage();
       $pdf->useTemplate($tplIdx, 10, 10, 90);
    
       $pdf->Output();
       ?>
    

    你可以看一个演示here

    更新 2 :: 示例代码

    这里是使用FPDF AND FPDFI旋转外部pdf文件的示例代码。

        <?php
        require_once('fpdf/fpdf.php');
        require_once('fpdi/fpdi.php');
    
        $pdf =& new FPDI();
        $pdf->AddPage();
    
       //Set the source PDF file
       $pagecount = $pdf->setSourceFile("existing_pdf.pdf");
    
       //Import the first page of the file
       $tpl = $pdf->importPage(1);
    
     //Use this page as template
     // use the imported page and place it at point 20,30 with a width of     170 mm
      $pdf->useTemplate($tpl, 20, 30, 170);
    
     #Print Hello World at the bottom of the page
    
      //Select Arial italic 8
      $pdf->SetFont('Arial','I',8);
      $pdf->SetTextColor(0,0,0);
      $pdf->SetXY(90, 160);
      $pdf->Rotate(90);
    
      $pdf->Write(0, "Hello World");
    
      $pdf->Output("modified_pdf.pdf", "F");
      ?>
    

    【讨论】:

    • 如果你想生成pdf,我想要的是从外部源旋转一个pdf文件,例如我有一个文件“user-manuals.pdf”,如何旋转它带有 FPDF 的 pdf 源文件 ??
    • 你可以使用 FPDI。 FPDI 是 PHP 类的集合,便于开发人员从现有 PDF 文档中读取页面并将其用作 FPDF 中的模板,该模板由 Olivier Plathey 开发。除了 FPDF 的副本,FPDI 不需要任何特殊的 PHP 扩展。
    • 我尝试programmingbulls.com/manipulating-pdf-files-php-using-fpdf 中的示例 2,我收到此错误“调用未定义方法 FPDI::Rotate()”
    • 我是从那个网站得到的。 如果你看一下代码,你会发现他将 $tpl 定义为从 pdf 带来的模板页面,但是当他向 fpdf 发送请求时,他使用了 $template。只需更改变量名称即可使用 =)
    • 调用未定义的方法 FPDI::Rotate()
    【解决方案2】:

    我正在使用 Laravel 和 FDPI(通过 composer require setasign/fpdi 安装)。

    我需要顺时针旋转 A4 纵向 PDF 以适合 A4 横向 PDF,其中源是使用 FDPI 导入的 PDF。

    A4 = 297 毫米 x 210 毫米

    我的纵向 PDF 原始代码:

    $file = public_path() . '/pdf/file.pdf';
    $pdf = new FPDI('P', 'mm', [210, 297]);
    $pdf->AddPage();
    $pdf->setSourceFile($file);
    $tplIdx = $pdf->importPage(1);
    $pdf->useTemplate($tplIdx, 0, 0, 210, 297);
    $pdf->Output();
    

    现在添加旋转,在这里使用代码:

    FPDF 旋转 http://www.fpdf.org/en/script/script2.php

    我做了以下课程:

    app/Pdf/RFPDI.php(R 代表旋转)

    <?php
    
    namespace App\Pdf;
    
    use FPDI;
    
    class RFPDI extends FPDI
    {
        public $angle = 0;
    
        public function Rotate($angle, $x = -1, $y = -1)
        {
            $this->setPrintHeader(false);
            $this->setPrintFooter(false);
    
            if ($x == -1) {
                $x = $this->x;
            }
    
            if ($y == -1){
                $y = $this->y;
            }
    
            if ($this->angle != 0){
                $this->_out('Q');
            }
    
            $this->angle = $angle;
    
            if ($angle != 0) {
                $angle *= M_PI / 180;
                $c = cos($angle);
                $s = sin($angle);
                $cx = $x*$this->k;
                $cy = ($this->h - $y) * $this->k;
                $this->_out(
                    sprintf('q %.5F %.5F %.5F %.5F %.2F %.2F cm 1 0 0 1 %.2F %.2F cm', $c, $s, -$s, $c, $cx, $cy, -$cx, -$cy)
                );
            }
        }
    
        public function RotateClockWise()
        {
            $this->Rotate(270, (297/2), (297/2));
        }
    
        public function RotateCounterClockWise()
        {
            $this->Rotate(90, (210/2), (210/2));
        }
    
        function _endpage()
        {
            if ($this->angle != 0) {
                $this->angle = 0;
                $this->_out('Q');
            }
            parent::_endpage();
        }
    }
    

    并更改了我的代码以利用旋转类将图像旋转为横向 PDF:

    $file = public_path() . '/pdf/file.pdf';
    $pdf = new RFPDI('L', 'mm', [297, 210]); // use fpdi\FPDI;
    $pdf->AddPage();
    $pdf->RotateClockWise();
    $pdf->setSourceFile($file);
    $tplIdx = $pdf->importPage(1);
    $pdf->useTemplate($tplIdx, 0, 0, 210, 297);
    $pdf->Output();
    

    我希望这对其他人有所帮助!

    【讨论】:

      猜你喜欢
      • 2023-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-23
      • 2022-12-13
      • 1970-01-01
      相关资源
      最近更新 更多