php TCPDF 页面旋转
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5484960/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TCPDF page rotation
提问by James
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.
我正在尝试生成一个包含 202 毫米宽 x 50 毫米高标签的 PDF 文件。我设法做到了这一点并添加了所需的文本和条形码,但我的问题是标签首先打印出窄边,因此整个页面需要旋转 90 度。
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.
我可以通过简单地在页面上单击鼠标右键并选择顺时针旋转(Shift+Ctrl++)来轻松地在 Adobe Reader 中执行此操作,但我确实需要在代码中执行此操作。
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.
有谁知道如何用 TCPDF 做到这一点?我试过旋转功能,但似乎无法让它工作。任何代码示例都会有所帮助。
回答by LawrenceGS
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):
$orientation (string) 页面方向。可能的值是(不区分大小写):
- P or Portrait (default)
- L or Landscape
- '' (empty string) for automatic orientation
- P 或纵向(默认)
- L 或横向
- ''(空字符串)用于自动定位
http://www.tcpdf.org/doc/classTCPDF.html#a5420ac8b0726a604260780d8f4185fc1
http://www.tcpdf.org/doc/classTCPDF.html#a5420ac8b0726a604260780d8f4185fc1
回答by jaddawyn
What I've done with version 1.5
我对 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.
这运行良好。
回答by Femi
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:
Rotate
很奇怪。文档没有告诉你的是,你必须先做 a StartTransform
,然后做 a Rotate
,然后再做 a StopTransform
。您只能StartTransform
在以某种方式设置 X/Y 位置后进行调用(例如,我SetXY
用来初始定位页面,然后您可以调用StartTransform
)。所以尝试这样做:
$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.
当你完成时。看看这对你有什么作用。
回答by Nicola Asuni
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.orgfor graphical transformations and XObject templates.
如果您需要,最简单的选择是将页面设置为横向模式“L”。否则,如果您需要纵向模式但带有旋转对象的页面,那么您可以创建一个 XObject 模板并将您的内容放在那里,包括图形转换。查看http://www.tcpdf.org 上的默认示例,了解图形转换和 XObject 模板。