Yii2 mPDF my footer only show on the last page

27 Views Asked by At

I want to set a footer on my PDF page using mPDF library but my footer only shows on the last page. Example image as you can see the previous page doesn't show footer but the second (last page) does.

I've tried putting footer before content like this

$this->SetFooter($footer);
$stylesheet = file_get_contents(Yii::getAlias('@frontend') . '/web/css/bootstrap3.7.min.css');
$stylesheet .= $css;
$this->WriteHTML($stylesheet, 1);
$this->WriteHTML($content, 2);

or this

$this->SetFooter($footer);
$this->WriteHTML($stylesheet, 1);
$this->WriteHTML($content, 2);

but none of them works. here is my component.

public function genPdf($content, $css = '', $footer = [], $additionals = [], $watermark = "")
  {

    $stylesheet = file_get_contents(Yii::getAlias('@frontend') . '/web/css/bootstrap3.7.min.css');
    $stylesheet .= $css;
    $this->WriteHTML($stylesheet, 1);
    $this->WriteHTML($content, 2);


    if ($footer) {
      $this->SetFooter($footer);
    }

    if ($watermark) {
      $this->WriteHTML('<watermarktext content="' . $watermark . '" alpha="0.3" />');
    }

    if (count($additionals)) {
      $basePath = Yii::getAlias('@webroot');
      $index = 0;

      foreach ($additionals as $doc) {
        // $this->SetImportUse();
        if ($doc['kind'] === 'pdf') {
          continue;
          $fileName = $basePath . '/tmp/file' . $index . '.pdf';
          file_put_contents($fileName, fopen($doc['uri'], 'r'));
          if (file_exists($fileName)) {
            $pagecount = $this->setSourceFile($fileName);

            if ($pagecount) {
              $tplId = $this->importPage($pagecount);
              $this->AddPage();
              $this->useTemplate($tplId);
              $this->useTemplate($tplId, 50, 50, 100, 100);
              $this->WriteHTML('<watermarktext content="' . $doc['name'] . '" alpha="0.3" />');
            }
          }
        } else if ($doc['kind'] === 'jpg') {
          $fileName = $basePath . '/tmp/file' . $index . '.jpg';
          file_put_contents($fileName, fopen($doc['uri'], 'r'));
          if (file_exists($fileName)) {
            $this->AddPage();
            $this->Image($fileName, 0, 0, 210, 297, 'jpg', '', true, false);
            $this->WriteHTML('<watermarktext content="' . $doc['name'] . '" alpha="0.3" />');
          }
        }

        $index++;
      }
    }

    if (!empty($this->filename)) {
      $this->Output($this->filename, 'I');
    } else {
      $this->Output();
    }
  }

and this is my controller

private function footer()
  {
    $dtime = \DateTime::createFromFormat("Y-m-d", date('Y-m-d'));
    $timestamp = $dtime->getTimestamp();
    $footer = array(
      'odd' => array(
        'L' => array(
          'content' => 'create by: ABC'. ' (' .  Yii::$app->date->date($timestamp) . ')',
          'font-size' => 10,
          'font-family' => 'garuda',
        ),
        'R' => array(
          'content' => "{PAGENO}/{nb}",
          'font-size' => 11,
          'font-family' => 'garuda',
        ),
        'line' => 1,
      ),
      'even' => array(
        'L' => array(
          'content' => 'create by: ABC'. ' (' .  Yii::$app->date->date($timestamp) . ')',
          'font-size' => 10,
          'font-family' => 'garuda',
        ),
        'R' => array(
          'content' => "{PAGENO}/{nb}",
          'font-size' => 11,
          'font-family' => 'garuda',
        ),
        'line' => 1,
      ),
    );

    return $footer;
  }

  private function outputPDF($fileName, $content, $cssFilePath, $overrideConfig = [], $additionals = [], $watermark = "")
  {

    $mpdf = new NgMpdf('utf-8', 'A4', 12, 'thsarabunnew', $left = 18, $right = 13, $top = 8, $bottom = 8, $mgh = 5, $mgf = 2, 'P');

    $mpdf->showWatermarkText = true;
    $mpdf->filename = $fileName . ".pdf";
    $mpdf->title = $fileName;

    $customCssContent = $this->getBasePdfCss();
    if (!empty($cssFilePath)) {
      $customCssContent .= file_get_contents($cssFilePath);
    }


    $mpdf->genPdf($content, $customCssContent, $this->footer(), $additionals, $watermark);
  }
0

There are 0 best solutions below