[英]How do I rotate all or specific pages in a PDF using PHP?
I have PDF documents uploaded by users that need to be processed by auditors. 我有用户上传的PDF文档,需要审计员处理。 Sometimes the scanned pages in a PDF are sideways or upside down. 有时,PDF中的扫描页面是横向的或上下颠倒的。 How do I rotate the pages using php? 如何使用php旋转页面?
I'm using TCPDF and TCPDI, which I believe are the same as fpdf/fpdi 我正在使用TCPDF和TCPDI,我相信它们与fpdf / fpdi相同
My code, below, will rotate all pages just fine, but when I specify just one page, let's say page 3 of 5. It will leave pages 1 and 2 alone, rotate page 3 and will continue to rotate page 4 and 5. Why? 下面的代码将旋转所有页面,但是当我仅指定一页时,假设5的第3页。它将保留第1和2页,旋转第3页并继续旋转第4和5页。为什么?
Also, does this code make sense? 另外,此代码是否有意义? Is this the proper way to do this or is there an easier way? 这是执行此操作的正确方法还是更简单的方法?
function rotatePDF($file, $degrees, $page = 'all'){
$pdf = new TCPDI(); // new object
$pdf->setPrintHeader(false); // no headers
$pdf->setPrintFooter(false); // no footers
$pagecount = $pdf->setSourceFile($file); //the original file
// rotate all - THIS WORKS FINE
if($page=="all"){
for ($i = 1; $i <= $pagecount; $i++) {
$pageformat = array('Rotate'=>$degrees);
$tpage = $pdf->importPage($i);
$size = $pdf->getTemplateSize($tpage);
// get original page orientation
$orientation = $size['w'] > $size['h'] ? 'L' : 'P';
$pdf->AddPage($orientation,$pageformat);
$pdf->useTemplate($tpage);
}
}else{
for ($i = 1; $i <= $pagecount; $i++) {
if($page == $i){
$pageformat = array('Rotate'=>$degrees);
$tpage = $pdf->importPage($i);
$size = $pdf->getTemplateSize($tpage);
// get original page orientation
$orientation = $size['w'] > $size['h'] ? 'L' : 'P';
$pdf->AddPage($orientation,$pageformat);
$pdf->useTemplate($tpage);
}else{
$tpage = $pdf->importPage($i);
$pdf->AddPage();
$pdf->useTemplate($tpage);
}
}
}
$out = realpath($file);
if(rename($file,"files/1/file.bak")){
$result = $pdf->Output($out, "F");
if($result == "" ){
echo "ok";
}
}else{
echo "Failed to rename old PDF";
die;
}
}
$file = "files/1/1.pdf";
rotatePDF($file,90); // rotating all works fine
rotatePDF($file,90,3); // rotates page 3 AND all following
After many trial and error rotations I figured it out. 经过多次试错之后,我发现了这一点。 Here's the code: 这是代码:
function rotatePDF($file, $degrees, $page = 'all'){
$pdf = new TCPDI();
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
$pagecount = $pdf->setSourceFile($file);
// rotate each page
if($page=="all"){
for ($i = 1; $i <= $pagecount; $i++) {
$pageformat = array('Rotate'=>$degrees);
$tpage = $pdf->importPage($i);
$size = $pdf->getTemplateSize($tpage);
//$info = $pdf->getPageDimensions();
$orientation = $size['w'] > $size['h'] ? 'L' : 'P';
$pdf->AddPage($orientation,$pageformat);
$pdf->useTemplate($tpage);
}
}else{
$rotateFlag = 0;
for ($i = 1; $i <= $pagecount; $i++) {
if($page == $i){
$pageformat = array('Rotate'=>$degrees);
$tpage = $pdf->importPage($i);
$size = $pdf->getTemplateSize($tpage);
//$info = $pdf->getPageDimensions();
$orientation = $size['w'] > $size['h'] ? 'L' : 'P';
$pdf->AddPage($orientation,$pageformat);
$pdf->useTemplate($tpage);
$rotateFlag = 1;
}else{
if($rotateFlag==1){
// page after rotation; restore rotation
$rotateFlag = 0;
$pageformat = array('Rotate'=>0);
$tpage = $pdf->importPage($i);
$pdf->AddPage($orientation,$pageformat);
$pdf->useTemplate($tpage);
}else{
// pages before rotation and after restoring rotation
$tpage = $pdf->importPage($i);
$pdf->AddPage();
$pdf->useTemplate($tpage);
}
}
}
}
$out = realpath($file);
if(rename($file,"files/1/file.bak")){
$result = $pdf->Output($out, "F");
if($result == "" ){
echo "ok";
}
}else{
echo "Failed to rename old PDF";
die;
}
}
$file = "files/1/1.pdf";
rotatePDF($file,90); // rotating all works fine
rotatePDF($file,180,3); // rotates only page 3
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.