iio\libmergepdf\Merger::merge PHP Method

merge() public method

Merges your provided PDFs and get raw string
public merge ( ) : string
return string
    public function merge()
    {
        if (empty($this->files)) {
            throw new Exception("Unable to merge, no PDFs added");
        }
        $fname = '';
        try {
            $fpdi = clone $this->fpdi;
            foreach ($this->files as $fileData) {
                list($fname, $pages, $cleanup) = $fileData;
                $pages = $pages->getPages();
                $iPageCount = $fpdi->setSourceFile($fname);
                // If no pages are specified, add all pages
                if (empty($pages)) {
                    $pages = range(1, $iPageCount);
                }
                // Add specified pages
                foreach ($pages as $page) {
                    $template = $fpdi->importPage($page);
                    $size = $fpdi->getTemplateSize($template);
                    $orientation = $size['w'] > $size['h'] ? 'L' : 'P';
                    $fpdi->AddPage($orientation, array($size['w'], $size['h']));
                    $fpdi->useTemplate($template);
                }
            }
            $output = $fpdi->Output('', 'S');
            $fpdi->cleanUp();
            foreach ($this->files as $fileData) {
                list($fname, $pages, $cleanup) = $fileData;
                if ($cleanup) {
                    unlink($fname);
                }
            }
            $this->files = array();
            return $output;
        } catch (\Exception $e) {
            throw new Exception("FPDI: '{$e->getMessage()}' in '{$fname}'", 0, $e);
        }
    }

Usage Example

 /**
  * @Route("/")
  * @Template()
  */
 public function indexAction(Request $request)
 {
     $this->rebuildAction(false);
     /** @var $snappy Snappy */
     $snappy = $this->get('knp_snappy.pdf');
     $cover = $snappy->getOutput($this->getPath('cover.html'));
     $snappy->setOption('margin-top', '20');
     $snappy->setOption('margin-right', '0');
     $snappy->setOption('margin-bottom', '16');
     $snappy->setOption('margin-left', '0');
     $snappy->setOption('header-html', $this->getPath('header.html'));
     $snappy->setOption('header-spacing', '5');
     $snappy->setOption('footer-html', $this->getPath('footer.html'));
     $snappy->setOption('footer-spacing', '5');
     $snappy->setOption('xsl-style-sheet', $this->getPath('toc.xml'));
     $html = file_get_contents($this->getPath('content.html'));
     if ($request->query->get('ids')) {
         $html = $this->addFilters($html, explode(',', $request->query->get('ids')));
     }
     $contents = $snappy->getOutputFromHtml($html, array('toc' => true));
     $pdfMerger = new Merger();
     $pdfMerger->addRaw($cover, new Pages('1'));
     $pdfMerger->addRaw($contents);
     $contents = $pdfMerger->merge();
     return new Response($contents, 200, array('Content-Type' => 'application/pdf'));
 }
All Usage Examples Of iio\libmergepdf\Merger::merge