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.

  • PDF has quite an interesting syntax - it has a direct dependence on the number of characters, as well as a double compression system. This is at least something I came across. Do not forget that + everything else you will encounter many versions of this format. There are libraries that will help you pull out text and pictures, but this is not enough. The only library that can handle this - from ADobe - paid - did not test it. - Mcile
  • here is the proper documentation This documentation was actually studied for a deeper understanding of the format. In general, the editor is a very hot topic - if you find a solution - put a solution here, a way to edit a file using a library or documentation. All that I came across was the reading of a document, either generation from scratch, or conversion from one format to another - Mcile

2 answers 2

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(); 
    • As usual, the devil is in the details. The code seems logical, but it didn’t work. Obviously, this code works on a specific version. I have PHP 5.6 and compositor I installed "setasign / fpdi-fpdf": "1.5.4" and "setasign / fpdi-fpdf": "1.6.1" ... - Ron Barhash
    • Debug and see where it falls. - Daniel Protopopov
    • It was also alarmed by the fact that when the script was executed, a terrible sound was made (like a Morsean: D) - Ron Barhash
    • And what made the sound? :) - Daniel Protopopov
    • $pdf->Output(); with removal .... the problem was just in line $pdf->Output(); there must be parameters in it. $pdf->Output($filename,'F') - Ron Barhash