Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split PDF documents into separate pages using PHP (or possibly perl) [closed]

Tags:

php

split

pdf

perl

Can anybody point me towards a PHP library or script that would allow me to split a pdf consisting of multiple pages into separate files, each containing 1 page. The PDFLib documentation doesn't appear to allow this and Google hasn't been particularly helpful.

I could possibly also use Perl, but it would be very inconvenient to do so.

like image 577
Simon Stevens Avatar asked Apr 06 '10 10:04

Simon Stevens


5 Answers

I have used PDF::Reuse. It makes it so easy it is not even funny. Here is the relevant snippet from one of my scripts:

while ( my $line = <$page_list> ) {
    chomp $line;
    my ($class, $drug, $page) = split ' ', $line;

    my $dir = canonpath( catfile $OUTPUT_DIR, $class );
    mkdir $dir unless -e $dir;

    my $target = canonpath( catfile $dir, "$drug.pdf" );

    prFile( $target );
    prCompress( 1 );
    prDoc( $INPUT_PDF, $page, $page + 1 );
    prEnd();
}
like image 154
Sinan Ünür Avatar answered Nov 17 '22 05:11

Sinan Ünür


It's not perl or php, but pdftk easily does this and much more.

like image 43
runrig Avatar answered Nov 17 '22 06:11

runrig


See PDF::Extract CPAN module for Perl.

like image 3
Alexandr Ciornii Avatar answered Nov 17 '22 05:11

Alexandr Ciornii


Does TCPDF do what you want? It is native PHP.

like image 1
dawg Avatar answered Nov 17 '22 06:11

dawg


I would vote for using PDFTk even if you choose to call it PDFTk from PHP. It has excellent track record of how to do this. The following example shows how to split your PDF with PDFTk

pdf2json.exe Paper.pdf -enc UTF-8 -compress -split 10 Paper.pdf_%.js

(example taken from this url)

like image 1
FlowPaper Team Avatar answered Nov 17 '22 05:11

FlowPaper Team