TCPDF page rotation
Asked Answered
M

7

13

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.

Mello answered 30/3, 2011 at 10:14 Comment(6)
did you rotate the page on creation already? see tcpdf.org/examples/example_060.phpsBaguio
@Mello interesting question because recently I was thinking about automating my portfolio in PDF and adding a digital barcode on the bottom of each project page. searching for BARCODE and PDF I landed here. Is your problem fixed or not?Quadrille
@Quadrille 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.Mello
@Mello "in the end I had to use another" ... I set a bounty for you hoping that more attention results to a solution. Also, I prefer "elegant solutions" over "compromis solutions" :)Quadrille
@Quadrille ah. Cheers, I hadn't realised you had placed a bounty until I got completion notice this morning.Mello
@Mello :) your welcome, have a sunny weekend, Cheers mate.Quadrille
S
12

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

Shenyang answered 23/5, 2011 at 21:41 Comment(1)
This was the firs thing that I tried but its not just the page I want to rotate. I want the text and page content to read from the bottom of the page to the top.Mello
S
12

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.

Shoot answered 25/8, 2014 at 16:20 Comment(0)
C
5

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.

Campfire answered 26/5, 2011 at 13:40 Comment(4)
Using what you suggested was the closest I got to being able to rotate in TCPDF but I got all sorts of strange anomalies most likely from my complete lack of understand of how these functions work.Mello
My guess is that entire co-ordinate system moves: you'll need to adjust all your X/Y positions accordingly, and that will require some math to get right. Unfortunately there doesn't appear to be a magical rotate everything on this page while moving 0,0 to the top right position method of doing it.Campfire
$page_format= array(55,55,'Rotate'=>-90); $pdf->AddPage('P', $page_format, false, false); // Also possible and you can set $pdf->SetPageUnit("mm"); in case ..Dulce
The anwer @YumYumYum has provided should actually be posted as a real answer. This actually works better than the other solutions posted.Zane
L
1

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.

Lail answered 14/12, 2020 at 21:47 Comment(0)
A
1

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.

Alodi answered 10/11, 2021 at 5:57 Comment(1)
Looks like this answer already suggests that: https://mcmap.net/q/851010/-tcpdf-page-rotationTaw
A
0

The simplest option is to set the page on Landscape mode 'L' if this is what you need. Otherwise, if you need a page in portrait mode but with rotated objects, then you can create an XObject template and put your content there, including graphical transformations. Check the default examples at http://www.tcpdf.org for graphical transformations and XObject templates.

Advisee answered 26/9, 2011 at 15:31 Comment(0)
H
0

This is an old post but if anyone has this issue, I would suggest you first try out the landscape mode for the page.

This works for me:

Here is the code for setting A4 landscape:

$pdf->AddPage('L', 'A4');

Visit https://tcpdf.org/examples/example_028/ to learn more.

Harms answered 1/3, 2022 at 10:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.