/** * Merges your provided PDFs and outputs to specified location. * @param $outputmode * @param $outputname * @param $orientation * @return PDF */ public function merge($outputmode = 'browser', $outputpath = 'newfile.pdf', $orientation = 'P') { if (!isset($this->_files) || !is_array($this->_files)) { throw new Exception("No PDFs to merge."); } $fpdi = new FPDI(); // merger operations foreach ($this->_files as $file) { $filename = $file[0]; $filepages = $file[1]; $fileorientation = !is_null($file[2]) ? $file[2] : $orientation; $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($fileorientation, 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($fileorientation, 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; } } }
/** * {@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; }
/** * @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'); }