PDF 使用 FPDF 和 Laravel 旋转


PDF Rotate with FPDF and Laravel

如何使用

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

 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();
   ?>

您可以在此处查看演示

更新 2 :: 示例代码

这是使用 FPDF 和 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");
  ?>

我正在使用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();

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

谢谢你的回答哈基姆。

但是在我的代码中,这 2 行会给出错误,所以我注释了这些行。

$this->setPrintHeader(false);
$this->setPrintFooter(false);

一切都运行顺利。