如何使用PHP旋转PDF中的所有页面或特定页面?

时间:2016-06-29 23:43:52

标签: php pdf tcpdf fpdf fpdi

我有需要由审核员处理的用户上传的PDF文档。有时,PDF中扫描的页面是横向或颠倒的。如何使用php旋转页面?

我正在使用TCPDF和TCPDI,我认为它与fpdf / fpdi相同

下面的代码会将所有页面旋转得很好,但是当我只指定一个页面时,让我们说第3页的第3页。它将单独留下第1页和第2页,旋转第3页并继续旋转第4页和5.为什么?

此外,这段代码有意义吗?这是正确的方法吗?或者有更简单的方法吗?

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

1 个答案:

答案 0 :(得分:1)

经过多次反复试验后,我发现了它。这是代码:

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