You are here

public function PdfTemplate::addPage in Views PDF 7.2

Same name and namespace in other branches
  1. 6 views_pdf_template.php \PdfTemplate::addPage()
  2. 7.3 views_pdf_template.php \PdfTemplate::addPage()
  3. 7 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 819
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') {

  // 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 = 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);
  }
}