Faced a problem: you need a library to work with existing PDF files. You need to create a mini PDF editor with the function of turning and deleting pages.
There are third-party console applications. But it's interesting to implement this using PHP.
Faced a problem: you need a library to work with existing PDF files. You need to create a mini PDF editor with the function of turning and deleting pages.
There are third-party console applications. But it's interesting to implement this using PHP.
To work with PDF using PHP, there is the FPDF library ( http://fpdf.org ).
FPDF is a PHP class that allows you to generate PDF files. For your needs.
For correct work with Cyrillic I used the UFPDF - Unicode-extension for this library ( http://acko.net/blog/ufpdf-unicode-utf-8-extension-for-fpdf/ ).
From the answer it is clear that FPDI and TCPDI are the same.
To rotate ( source ):
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 To delete ( source ):
$pdf = new FPDI(); $pageCount = $pdf->setSourceFile('document.pdf'); // Array of pages to skip -- modify this to fit your needs $skipPages = [3,15,17,22]; // Add all pages of source to new document for( $pageNo=1; $pageNo<=$pageCount; $pageNo++ ) { // Skip undesired pages if( in_array($pageNo,$skipPages) ) continue; // Add page to the document $templateID = $pdf->importPage($pageNo); $pdf->getTemplateSize($templateID); $pdf->addPage(); $pdf->useTemplate($templateID); } $pdf->Output(); $pdf->Output(); with removal .... the problem was just in line $pdf->Output(); there must be parameters in it. $pdf->Output($filename,'F') - Ron BarhashSource: https://ru.stackoverflow.com/questions/637180/
All Articles