/** * Merge PDF files together in the order they were entered to the $files array. * * @return string|bool * @see http://stackoverflow.com/questions/19855712/merge-multiple-pdf-files-into-one-in-php#answer-19856524 * @uses FPDI_Protection::setSourceFile() * @uses FPDI_Protection::importPage() * @uses FPDI_Protection::addPage() * @uses FPDI_Protection::useTemplate() * @uses FPDI_Protection::SetAuthor() * @uses FPDI_Protection::SetCreator() * @uses FPDI_Protection::SetTitle() * @uses FPDI_Protection::SetSubject() * @uses FPDI_Protection::SetKeywords() * @uses FPDI_Protection::Output() */ public function merge() { $this->Log->write(__METHOD__, Log::LOG_LEVEL_SYSTEM_INFORMATION); // input validation if (!Helpers::is_array_ne($this->files)) { $this->Log->write('Files is not an array. Enter at least 1 file before attempting to combine.', Log::LOG_LEVEL_WARNING); return false; } try { $pdf = new \FPDI_Protection(); foreach ($this->files as $file) { $page_count = $pdf->setSourceFile($this->directory . $file); for ($j = 0; $j < $page_count; $j++) { $template_index = $pdf->importPage($j + 1, '/MediaBox'); $pdf->addPage($this->orientation, $this->paper_size); $pdf->useTemplate($template_index, 0, 0, 0, 0, true); } // CAUTION: Delete the PDF input file if not debugging. if (!$this->debug) { unlink($file); } } $pdf->SetAuthor($this->author); $pdf->SetCreator($this->creator); $pdf->SetTitle($this->title); $pdf->SetSubject($this->subject); $pdf->SetKeywords(implode(', ', $this->keywords)); if (Helpers::is_string_ne($this->user_password)) { $pdf->SetProtection(array('print', 'copy'), $this->user_password, $this->owner_password); } $output_path = $this->directory . $this->output_file; $this->Log->write('output file path', Log::LOG_LEVEL_USER, $output_path); $output = $pdf->Output('F', $output_path); if ($output === '') { // file was created return $output_path; } else { $this->Log->write('Error writing file to output', Log::LOG_LEVEL_WARNING, $output); return $output; } } catch (\Exception $ex) { $this->Log->exception($ex); return false; } }