You are here

public function PdfTemplate::addPage in Views PDF 6

Same name and namespace in other branches
  1. 7.3 views_pdf_template.php \PdfTemplate::addPage()
  2. 7 views_pdf_template.php \PdfTemplate::addPage()
  3. 7.2 views_pdf_template.php \PdfTemplate::addPage()

This method adds a new page to the PDF.

2 calls to PdfTemplate::addPage()
PdfTemplate::drawContent in ./views_pdf_template.php
This method draws a field on the PDF.
PdfTemplate::drawTable in ./views_pdf_template.php
This method draws a table on the PDF.

File

./views_pdf_template.php, line 694
PDF Class to generate PDFs with native PHP. This class based on FPDF and FPDI.

Class

PdfTemplate
The main class to generate the PDF.

Code

public function addPage($path = NULL, $reset = false, $numbering = 'main') {
  $this->mainContentPageNumber++;
  $this->rowContentPageNumber++;

  // Reset without any template
  if ((empty($path) || !file_exists($path)) && $reset == true) {
    parent::addPage();
    $this
      ->setPageFormat($this->defaultFormat, $this->defaultOrientation);
    return;
  }
  $files = $this->defaultPageTemplateFiles;

  // Reset with new template
  if ($reset) {
    $files = array();
  }
  if ($path != NULL) {
    $files[] = array(
      'path' => $path,
      'numbering' => $numbering,
    );
  }
  $format = false;
  foreach ($files as $file) {
    if (!empty($file['path']) && file_exists($file['path'])) {
      $path = realpath($file['path']);
      $numberOfPages = $this
        ->setSourceFile($path);
      if ($file['numbering'] == 'row') {
        $index = min($this->rowContentPageNumber, $numberOfPages);
      }
      else {
        $index = min($this->mainContentPageNumber, $numberOfPages);
      }
      $page = $this
        ->importPage($index);

      // ajust the page format (only for the first template)
      if ($format == false) {
        $dim = $this
          ->getTemplateSize($index);
        $format[0] = $dim['w'];
        $format[1] = $dim['h'];

        //$this->setPageFormat($format);
        if ($dim['w'] > $dim['h']) {
          $orientation = 'L';
        }
        else {
          $orientation = 'P';
        }
        $this
          ->setPageFormat($format, $orientation);
        parent::addPage();
      }

      // Apply the template
      $this
        ->useTemplate($page, 0, 0);
    }
  }

  // if all paths were empty, ensure that at least the page is added
  if ($format == false) {
    parent::addPage();
    $this
      ->setPageFormat($this->defaultFormat, $this->defaultOrientation);
  }

  // When a new page is added, we copy the header / footer data from the prvevious page.
  // this ensures that we get always the data, independent on a page break
  // inside a record.
  if (isset($this->headerFooterData[$this
    ->getPage() - 1])) {

    //  $this->headerFooterData[$this->getPage()] = $this->headerFooterData[($this->getPage() - 1)];
  }
}