From: Vincent Vanwaelscappel Date: Tue, 17 May 2022 15:34:55 +0000 (+0200) Subject: wip #5220 @0.5 X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=b19f2c77de6b67f4a1ebb5e43b06334f74a6396e;p=cubist_pdf.git wip #5220 @0.5 --- diff --git a/.idea/php.xml b/.idea/php.xml index 241dbb9..c5d8e07 100644 --- a/.idea/php.xml +++ b/.idea/php.xml @@ -65,7 +65,7 @@ - + \ No newline at end of file diff --git a/src/PDFTools.php b/src/PDFTools.php index 9bf4bb2..02f590a 100644 --- a/src/PDFTools.php +++ b/src/PDFTools.php @@ -370,4 +370,52 @@ class PDFTools } + public static function split($pdf, $out) + { + + $lock = $pdf . '.split.lock'; + $returnAfterSleep = false; + + usleep(rand(100000, 2000000)); + + while (file_exists($lock)) { + $returnAfterSleep = true; + sleep(5); + } + if ($returnAfterSleep) { + return; + } + + touch($lock); + + Files::mkdir($out); + $pdftk = new CommandLine('pdftk'); + $pdftk->setArg(null, $pdf); + $pdftk->setArg(null, 'burst'); + $pdftk->setArg(null, 'uncompress'); + $pdftk->setArg(null, 'output'); + $pdftk->setArg(null, $out . '/p%d.pdf'); + $pdftk->execute(); + + + for ($i = 1; true; $i++) { + // Remove annotations : https://gist.github.com/stefanschmidt/5248592 + $file = sprintf($out . '/p%d.pdf', $i); + if (!file_exists($file)) { + break; + } + $to = sprintf($out . '/s%d.pdf', $i); + `LANG=C LC_CTYPE=C sed -n '/^\/Annots/!p' $file > $to`; + if (file_exists($to)) { + if (filesize($to) > 0) { + unlink($file); + rename($to, $file); + } else { + unlink($to); + } + } + } + unlink($lock); + } + }