Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TCPDF page rotation

I'm trying to generate a PDF file containing labels which are 202mm wide by 50mm heigh. I have managed to do this and added the required text and a barcode but my problem is that the labels print out narrow edge first so the whole page need rotating 90 degrees.

I can do this in Adobe Reader with ease by simple right clicking on the page and selecting Rotate Clockwise (Shift+Ctrl++) but I really need to do it in the code.

Does anyone know how to do this with TCPDF? I have tried the Rotate function but can't seem to get it working. Any examples of code would be helpful.

like image 762
James Avatar asked Mar 30 '11 10:03

James


5 Answers

The simplest and easy way to Landscape is: First go to your tcpdf.config file then go to the line

 * Page orientation (P=portrait, L=landscape).
 */
define ('PDF_PAGE_ORIENTATION', 'L','P');

Just change the "P" to 'L' save and run it.

like image 87
Ilyas Kacho Avatar answered Nov 15 '22 18:11

Ilyas Kacho


The format argument in constructor has wide range of optional parameters, including Rotate and support for arbitrary page width and height - example:

// page A4 rotated clockwise 90 deg.
$pdf = new TCPDF('L', 'pt', ['format' => 'A4', 'Rotate' => 90]); 

// page with custom size rotated clockwise 270 deg.
$pdf = new TCPDF('L', 'pt', ['format' => [$width, $height], 'Rotate' => 270]);

Documentation here.

like image 35
lubosdz Avatar answered Nov 15 '22 20:11

lubosdz


What I've done with version 1.5

    $pdf->AddPage(); // Orientation for the first page is defined into configuration file.

    $pdf->writeHTML("Portrait 1");

    $pdf->AddPage('L');

    $pdf->writeHTML("Landscape !");

    $pdf->AddPage('P');

    $pdf->writeHTML("Portrait 2");

    $pdf->Output();

And this is working well.

like image 26
jaddawyn Avatar answered Nov 15 '22 18:11

jaddawyn


How about setting it to landscape when building the page?

TCPDF::__construct($orientation = 'L',
$   unit = 'mm',
$   format = 'A4',
$   unicode = true,
$   encoding = 'UTF-8',
$   diskcache = false)

$orientation (string) page orientation. Possible values are (case insensitive):

  • P or Portrait (default)
  • L or Landscape
  • '' (empty string) for automatic orientation

http://www.tcpdf.org/doc/classTCPDF.html#a5420ac8b0726a604260780d8f4185fc1

like image 38
LawrenceGS Avatar answered Nov 15 '22 19:11

LawrenceGS


Rotate is odd. What the docs don't tell you is that you have to do a StartTransform first and then do a Rotate, then do a StopTransform afterwards. You can only do the StartTransform call after you have somehow set the X/Y position (so for example, I use SetXY to initially position the page, then you can call StartTransform). So try to do:

  $this->pdfinvoice->StartTransform();
  $this->pdfinvoice->Rotate(-90);

then add your content, then call

  $this->pdfinvoice->StopTransform();

when you're done. See how that works for you.

like image 41
Femi Avatar answered Nov 15 '22 19:11

Femi