php FPDF / FPDI addPage() 方向
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12185777/
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
FPDF / FPDI addPage() Orientation
提问by mmackh
I'm using the following code to add a new page to my existing PDF document and save it.
我正在使用以下代码向我现有的 PDF 文档添加一个新页面并保存它。
require('addons/fpdf.php');
require('addons/fpdi.php');
$pdf = new FPDI();
$pagecount = $pdf->setSourceFile($orgpdfpath);
for($i = 1; $i <= $pagecount; $i++){
$pdf->addPage();
$tplidx = $pdf->importPage($i);
$pdf->useTemplate($tplidx);
}
$pdf->addPage($pdforientation);
$pdf->Image($imgpath,$pdfxaxis,$pdfyaxis,$pdfwith,$pdfheight);
$pdf->Output($orgpdfpath,'F');
It works fine if I have a document that is A4, Page 1: portrait, Page 2: portrait, Page 3: portrait, etc.
如果我有一个 A4 的文档,第 1 页:肖像,第 2 页:肖像,第 3 页:肖像等,它工作正常。
It also works if I add a landscape A4 Page. However, after I have added a landscape page and try to add a portrait, the landscape is shifted back to a portrait and the whole formatting of the document breaks.
如果我添加横向 A4 页面,它也有效。但是,在我添加横向页面并尝试添加纵向后,横向移回纵向并且文档的整个格式中断。
I suspect that this has to do something with addPage() inside the loop. Why does does it not rotate appropriately when applying ->useTemplate?
我怀疑这与循环内的 addPage() 有关系。为什么在应用->useTemplate 时它没有正确旋转?
回答by mmackh
I oversaw that there was a function called ->getTemplateSize(). Here's a working snippet:
我看到有一个名为 ->getTemplateSize() 的函数。这是一个工作片段:
$pdf = new FPDI();
$pagecount = $pdf->setSourceFile($orgpdfpath);
for($i = 1; $i <= $pagecount; $i++){
$tplidx = $pdf->importPage($i);
$specs = $pdf->getTemplateSize($tplidx);
$pdf->addPage($specs['h'] > $specs['w'] ? 'P' : 'L');
$pdf->useTemplate($tplidx);
}
$pdf->addPage($pdforientation);
$pdf->Image($imgpath,$pdfxaxis,$pdfyaxis,$pdfwith,$pdfheight);
$pdf->Output($orgpdfpath,'F');
回答by sunixzs
Maybe this helps the one or other, if you define den orientation and it won't work in pdf generation. I changed the width and height in landscape mode on AddPage(). Probably this should be done automatically, but in my case in combination with PDFmerger, a wrapper class for fpdf/fpdi, it doesn't.
如果您定义了 den 方向并且它在 pdf 生成中不起作用,那么这可能会有所帮助。我在 AddPage() 上更改了横向模式下的宽度和高度。可能这应该自动完成,但在我的情况下,结合 PDFmerger(fpdf/fpdi 的包装类),它不会。
$fpdi = new FPDI;
$count = $fpdi->setSourceFile($filename);
for($i=1; $i<=$count; $i++) {
$template = $fpdi->importPage($i);
$size = $fpdi->getTemplateSize($template);
$orientation = ($size['h'] > $size['w']) ? 'P' : 'L';
if ($orientation == "P") {
$fpdi->AddPage($orientation, array($size['w'], $size['h']));
} else {
$fpdi->AddPage($orientation, array($size['h'], $size['w']));
}
$fpdi->useTemplate($template);
}
回答by Jeremy Wadhams
BTW, if you can't guarantee that all your documents will be A4 (this isn't your problem, but it was my problem that led me to this Q) you can also use the size of your template to set the size of your generated file's pages, by passing the sizes as an array in the second arg:
顺便说一句,如果你不能保证你所有的文件都是 A4(这不是你的问题,但这是我的问题导致我这个 Q)你也可以使用你的模板的大小来设置你的通过在第二个参数中将大小作为数组传递来生成文件的页面:
$pdf->AddPage(
( $size['w'] > $size['h'] ) ? 'L' : 'P',
[ $size['w'], $size['h'] ]
);

