anklimsk/cakephp2-tcpdf

使用 CakePHP 2.x 生成 PDF 文件

安装: 138

依赖者: 0

建议者: 0

安全: 0

星标: 0

关注者: 1

分支: 0

开放问题: 0

类型:cakephp-plugin

v1.0.3 2020-04-02 17:44 UTC

This package is auto-updated.

Last update: 2024-09-29 04:26:03 UTC


README

Build Status Coverage Status Latest Stable Version License

使用 CakePHP 生成 PDF 文件

此插件提供以下功能

  • 生成 PDF 文件

安装

  1. 使用 composer 安装插件: composer require anklimsk/cakephp2-tcpdf

  2. 将以下行添加到文件末尾 app/Config/bootstrap.php

    CakePlugin::load('CakeTCPDF', ['bootstrap' => true, 'routes' => true]);

使用此插件

  1. 在您的 Model

    • 创建以下方法

      public function getExportConfig() {
          $header = [__('Field label 1'), __('Field label 2'), __('Field label 3'), __('Field label 4')];
          $width = [35, 20, 10, 15];
          $align = ['L', 'L', 'C', 'R'];
          $fileName = __('Export file');
      
          return compact('header', 'width', 'align', 'fileName');
      }
      
      public function getExportData($conditions = []) {
          ...
          $result = [
              'Group header (List name)' => [
                  'Sub header' => [
                      [
                          'Field value 1',
                          'Field value 2',
                          'Field value 3',
                          'Field value 4',
                      ]
                  ]
              ]
          ];
      
          return $result;
      }
  2. 在您的 Controller

    • RequestHandler 组件添加到 AppController,并将 pdf 映射到 CakeTCPDF 插件,例如:

      public $components = [
          ...,
          'RequestHandler' => [
              'viewClassMap' => [
                  'pdf' => 'CakeTCPDF.Pdf'
              ]
          ]
      );
    • 添加到您的控制器操作

      public export($id = null) {
          if (!$this->RequestHandler->prefers('pdf')) {
              throw new BadRequestException(__('Invalid export type');
          }
      
          $conditions = [];
          if (!empty($id)) {
              $conditions['Model.id'] = $id;
          }
          $exportConfig = $this->Model->getExportConfig();
          $exportData = $this->Model->getExportData();
      
          $this->set(compact('exportConfig', 'exportData'));
      }
  3. 在您的 View

    • 创建指向具有扩展名 .pdf 的操作的链接,例如:

      $this->Html->link('PDF file', ['ext' => 'pdf']);
    • 将视图模板放置在子目录 Pdf 中,例如: app/View/Invoices/Pdf/index.ctp

    • 在您的视图文件中使用 CakeTCPDF.exportPdfTable 元素,例如:

      if (!empty($exportConfig)) {
          extract($exportConfig);
      }
      
      if (!isset($orientation)) {
          $orientation = PDF_PAGE_ORIENTATION;
      }
      
      if (isset($fileName)) {
          $this->setFileName($fileName);
      }
      
      $this->tcpdf->setPageOrientation($orientation, TRUE, PDF_MARGIN_BOTTOM);
      $this->tcpdf->options['footerText'] = $this->tcpdf->getAliasNumPage();
      
      // set font
      $this->tcpdf->SetFont(PDF_FONT_NAME_DATA, 'B', PDF_FONT_SIZE_DATA);
      // set default font subsetting mode
      $this->tcpdf->setFontSubsetting(true);
      
      //set margins
      $this->tcpdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
      $this->tcpdf->SetHeaderMargin(PDF_MARGIN_HEADER);
      $this->tcpdf->SetFooterMargin(PDF_MARGIN_FOOTER);
      
      $this->tcpdf->AddPage();
      $this->tcpdf->setPrintFooter(true);
      echo $this->element('CakeTCPDF.exportPdfTable', compact('exportConfig', 'exportData'));
    • 在您的视图文件中使用 CakeTCPDF.exportPdfTableContent 元素,例如:

      echo $this->element('CakeTCPDF.exportPdfTableContent', compact('exportConfig'));