Reputation: 163
I have PDF documents uploaded by users that need to be processed by auditors. Sometimes the scanned pages in a PDF are sideways or upside down. How do I rotate the pages using php?
I'm using TCPDF and TCPDI, which I believe are the same as 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?
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
Upvotes: 0
Views: 6199
Reputation: 163
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
Upvotes: 2