Traian Tatic
Traian Tatic

Reputation: 691

Keep only the first page of a PDF document with PHP

I have many PDFs that are generated and uploaded to my server. The problem is they contain the same page three times (3 pages in total with the same content). My goal is to edit the PDF with PHP so that it contains only one page. Is there any library that allows me to simply load a PDF and keep only the first page?

Thank you!

Upvotes: 1

Views: 6715

Answers (2)

Yoan Tournade
Yoan Tournade

Reputation: 713

Using FPDI, you can create a function to extract the first page of a PDF file:

function first_page ($path) {
    $pdf = new FPDI();
    $pdf->AddPage();
    $pdf->setSourceFile($path);
    $pdf->useTemplate($pdf->importPage(1));

    return $pdf;
}

Then output the extracted PDF as you would do with FPDF:

// Extract first page from /path/to/my.pdf
// and output it to browser with filename "MyPDF".
first_page('/path/to/my.pdf')->Output('MyPDF', 'I');

Upvotes: 2

zdeniiik
zdeniiik

Reputation: 232

FPDF (http://www.fpdf.org/) or MDPF (http://www.mpdf1.com/mpdf/index.php) are great libraries for work with PDF files. I have experiences only with creating PDF; but I assume that one of those libraries can solve your problem.

Edit: Here is some example with FPDF

https://gist.github.com/maccath/3981205

Upvotes: 2

Related Questions