1

How can I rotate PDF file with FPDF ? from the docs I saw it can only generate/create the pdf and then rotate it, and not from outside source ..

Hendrik Eka
  • 145
  • 3
  • 14

3 Answers3

1
 Rotate(float angle [, float x [, float y]])

angle: angle in degrees. x: abscissa of the rotation center. Default

value: current position. y: ordinate of the rotation center. Default

value: current position.

The rotation affects all elements which are printed after the method call (with the exception of clickable areas).

  • Only the display is altered. The GetX() and GetY() methods are not affected, nor the automatic page break mechanism.
  • Rotation is not kept from page to page. Each page begins with a null rotation.

Here's an example which defines the utility methods RotatedText() and RotatedImage() and uses them to print a text and an image rotated to 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();
?>

UPDATE::

FPDI - Import pages from existing PDF documents and use them as templates in FPDF.

FPDI is a collection of PHP classes facilitating developers to read pages from existing PDF documents and use them as templates in FPDF, which was developed by Olivier Plathey. Apart from a copy of FPDF, FPDI does not require any special PHP extensions.

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

You can see a demo here

UPDATE 2 :: Sample Code

And here is the sample code by using FPDF AND FPDFI to rotate the external pdf file.

    <?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");
  ?>
Renjith V R
  • 2,981
  • 2
  • 22
  • 32
  • That if you want to generate the pdf,, what I want is to rotate a pdf file from outside source,, for example I had a file "user-manuals.pdf", how to rotate that pdf source with FPDF ?? – Hendrik Eka Feb 13 '16 at 18:01
  • the u can use FPDI. FPDI is a collection of PHP classes facilitating developers to read pages from existing PDF documents and use them as templates in FPDF, which was developed by Olivier Plathey. Apart from a copy of FPDF, FPDI does not require any special PHP extensions. – Renjith V R Feb 13 '16 at 18:07
  • I try the example 2 from http://programmingbulls.com/manipulating-pdf-files-php-using-fpdf, and I get this error " Call to undefined method FPDI::Rotate()" – Hendrik Eka Feb 13 '16 at 18:23
  • I got this from that website. **If you look at the code, you'll see that he defines $tpl as the template page brought from the pdf, but when he sends the request to fpdf he uses $template. just change the variable name and it works =)** – Renjith V R Feb 13 '16 at 18:27
  • Call to undefined method FPDI::Rotate() – Hendrik Eka Feb 13 '16 at 18:51
1

I'm using Laravel and FDPI (installed via composer require setasign/fpdi).

I needed to rotate an A4 Portrait PDF clockwise to fit A4 landscape PDF where the source is an imported PDF using FDPI.

A4 = 297mm x 210mm

My original code for portrait PDFs:

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

Now to add the rotation, using code here:

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

I made the following class:

app/Pdf/RFPDI.php (R is for Rotate)

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

and altered my code to utilise the rotation class to rotate images into a landscape 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();

I hope this helps someone else out!

haakym
  • 12,050
  • 12
  • 70
  • 98
0

thank you for your answer haakym.

but in my code, these 2 lines give error, so I commented those lines.

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

and everything run smoothly.