namespace Fluidbook\Tools\Jobs;
+use Cubist\PDF\PDFTools;
use Cubist\Util\Files\Files;
use Fluidbook\Tools\Document;
use Illuminate\Bus\Queueable;
/**
* @var bool
*/
- protected $debug=true;
+ protected $debug = true;
/**
* @param $document Document
* @param $page integer
* @param ProcessFile[] $files
*/
- public function __construct($document, $page, $files = [],$debug=true)
+ public function __construct($document, $page, $files = [], $debug = true)
{
$this->document = $document;
$this->page = $page;
$this->files = $files;
- $this->debug=$debug;
+ $this->debug = $debug;
}
/**
public function handle()
{
- if($this->debug) {
+ if ($this->debug) {
start_measure('Process page ' . $this->page);
}
foreach ($this->files as $file) {
$this->getFile($this->page, $file, false);
}
- if($this->debug) {
+ if ($this->debug) {
stop_measure('Process page ' . $this->page);
}
}
public function splitDoc()
{
- $lock = $this->getPDFInput() . '.split.lock';
- $returnAfterSleep = false;
-
- usleep(rand(100000, 2000000));
-
- while (file_exists($lock)) {
- $returnAfterSleep = true;
- sleep(5);
- }
- if ($returnAfterSleep) {
- return;
- }
-
- if($this->debug) {
+ if ($this->debug) {
start_measure('Split PDF');
}
- touch($lock);
-
- Files::mkdir($this->getOut() . '/pdf');
- $pdftk = new CommandLine('pdftk');
- $pdftk->setArg(null, $this->getPDFInput());
- $pdftk->setArg(null, 'burst');
- $pdftk->setArg(null, 'uncompress');
- $pdftk->setArg(null, 'output');
- $pdftk->setArg(null, $this->getOut() . 'pdf/p%d.pdf');
- $pdftk->execute();
-
-
- for ($i = 1; $i <= $this->getPagesNumber(); $i++) {
- // Remove annotations : https://gist.github.com/stefanschmidt/5248592
- $file = sprintf($this->getOut() . 'pdf/p%d.pdf', $i);
- $to = sprintf($this->getOut() . 'pdf/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);
- if($this->debug) {
+ PDFTools::split($this->getPDFInput(), $this->getOut() . '/pdf');
+ if ($this->debug) {
stop_measure('Split PDF');
}
}