You are here

public function ViewsPdfBase::addPage in Views PDF 8

This method adds a new page to the PDF.

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

File

src/ViewsPdfBase.php, line 923
Contains \Drupal\views_pdf\ViewsPdfTemplate.

Class

ViewsPdfBase
The main class to generate the PDF.

Namespace

Drupal\views_pdf

Code

public function addPage($path = NULL, $reset = FALSE, $numbering = 'main', $tocpage = FALSE) {

  // Do not add any new page, if we are writing
  // in the footer or header.
  if ($this->InFooter) {
    return;
  }
  $this->mainContentPageNumber++;
  $this->rowContentPageNumber++;

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

  // Reset with new template
  if ($reset) {
    $files = [];
  }
  if ($path != NULL) {
    $files[] = [
      '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);
  }
}