Reputation: 11
I have a system that generates PDF files, each file consists of two pages. For some reason I need to write a PHP code which can delete the second page from each PDF file. Herer is what I've done and what was the results:
1- I used TCPDF library to write the folowing code:
require_once('TCPDF-master/examples/tcpdf_include.php');
require_once('TCPDF-master/tcpdf_import.php');
$pdf = new TCPDF_IMPORT( 'test.pdf' );
if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
require_once(dirname(__FILE__).'/lang/eng.php');
$pdf->setLanguageArray($l);
}
$pdf->deletePage(2);
$pdf->Output('test_output.pdf', 'I');
The result: Blank one page PDF file. In other words, the content from the original file was not available in the new file
2- I used FPDF and FPDI libraries to read only one page from the original file.
require_once('FPDF-master/fpdf.php');
require_once('FPDI-1.6.1/fpdi.php');
$pdf = new FPDI();
$pageCount = $pdf->setSourceFile('test.pdf');
$tplIdx = $pdf->importPage(1, '/MediaBox');
$pdf->addPage();
$pdf->useTemplate($tplIdx, 10, 10, 90);
$pdf->Output();
The result is this error message:
Uncaught exception 'Exception' with message 'This document (test.pdf) probably uses a compression technique which is not supported by the free parser shipped with FPDI. (See https://www.setasign.com/fpdi-pdf-parser for more details)' in C:\wamp\www\FPDI-1.6.1\pdf_parser.php on line 322
Anybody knows how to help please??
Upvotes: 0
Views: 2860
Reputation: 799
I would try using a tool like pdftk. You can invoke it from PHP if you prefer. Command line below to extract a page.
pdftk in.pdf cat 1 output out.pdf
Upvotes: 0