TCPDF page rotation
Solution 1
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.
Solution 2
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
Solution 3
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.
Solution 4
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.
Related videos on Youtube
James
Updated on November 10, 2021Comments
-
James about 2 years
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.
-
konsolenfreddy over 12 yearsdid you rotate the page on creation already? see tcpdf.org/examples/example_060.phps
-
James over 12 years@Sam in the end I had to use another program to rotate the page. The PDF generation and barcode rendering works fine though. With having no other alternative to use, I'd probably use TCPDF again.
-
-
Vega about 2 yearsLooks like this answer already suggests that: stackoverflow.com/a/6103397/5468463