/** * {@inheritDoc} */ public function combine($path, array $files = null) { $files = is_array($files) ? $files : array_slice(func_get_args(), 1); if (!$files or 0 === count($files)) { return false; } if (1 === count($files)) { return $files[0]; } $combined = new FPDI(); foreach ($files as $file) { if (!$file instanceof File) { $file = new File($file); } // Get each page of each file $pageCount = $combined->setSourceFile($file->getRealPath()); for ($i = 1; $i <= $pageCount; $i++) { $page = $combined->importPage($i); $size = $combined->getTemplateSize($page); $combined->AddPage($size['w'] > $size['h'] ? 'L' : 'P', array($size['w'], $size['h'])); $combined->useTemplate($page); } } $dest = new File($path); $combined->Output($dest->getRealPath(), 'F'); return $dest; }
/** * Merge it. * @param $outputmode * @param $outputname * @return PDF */ public function merge($outputmode = 'browser', $outputpath = 'newfile.pdf') { if (!isset($this->_files) || !is_array($this->_files)) { throw new Exception("No PDFs to merge."); } $fpdi = new FPDI(); // merger operations /* @var $file PdfObject */ foreach ($this->_files as $file) { $filename = $file->path; $filepages = $file->pages; $count = $fpdi->setSourceFile($filename); //add the pages if ($filepages == 'all') { for ($i = 1; $i <= $count; $i++) { $template = $fpdi->importPage($i); $size = $fpdi->getTemplateSize($template); $fpdi->AddPage($file->getOrientationCode(), array($size['w'], $size['h'])); $fpdi->useTemplate($template); } } else { foreach ($filepages as $page) { if (!($template = $fpdi->importPage($page))) { throw new Exception("Could not load page '{$page}' in PDF '{$filename}'. Check that the page exists."); } $size = $fpdi->getTemplateSize($template); $fpdi->AddPage($file->getOrientationCode(), array($size['w'], $size['h'])); $fpdi->useTemplate($template); } } } //output operations $mode = $this->_switchmode($outputmode); if ($mode == 'S') { return $fpdi->Output($outputpath, 'S'); } else { if ($fpdi->Output($outputpath, $mode) == '') { return true; } else { throw new Exception("Error outputting PDF to '{$outputmode}'."); return false; } } }
/** * @function aggregate pdf files * * @param $files array Array with absolute path to pdf files */ public function aggregatePdf($files, $path) { $fpdi = new \fpdi\FPDI(); foreach ($files as $file) { if (file_exists($file)) { $pageCount = $fpdi->setSourceFile($file); for ($pageNo = 1; $pageNo <= $pageCount; $pageNo++) { $tplIdx = $fpdi->ImportPage($pageNo); $s = $fpdi->getTemplatesize($tplIdx); // Landscape/Portrait? $fpdi->AddPage($s['w'] > $s['h'] ? 'L' : 'P', array($s['w'], $s['h'])); $fpdi->useTemplate($tplIdx); } } } $pathDir = dirname($path); if (!is_dir($pathDir)) { mkdir($pathDir, 0777, true); } return $fpdi->Output($path, 'F'); }