开发者

TCPDF page rotation

开发者 https://www.devze.com 2023-02-20 13:59 出处:网络
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

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 t开发者_开发技巧his with TCPDF? I have tried the Rotate function but can't seem to get it working. Any examples of code would be helpful.


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.


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


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.


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.


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消