GithubHelp home page GithubHelp logo

anklimsk / cakephp-tcpdf Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 5.2 MB

Generate PDF files with the CakePHP

Home Page: https://anklimsk.github.io/cakephp-tcpdf

License: MIT License

PHP 100.00%
cakephp2 cakephp-plugin tcpdf

cakephp-tcpdf's Introduction

CakePHP 2.x Generate PDF files plugin

Build Status Coverage Status Latest Stable Version License

Generate PDF files with the CakePHP

This plugin provides next features:

  • Generate PDF files

Installation

  1. Install the Plugin using composer: composer require anklimsk/cakephp2-tcpdf

  2. Add the next line to the end of the file app/Config/bootstrap.php:

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

Using this plugin

  1. In your Model:

    • Create the following methods:

      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. In your Controller:

    • Add the RequestHandler component to AppController, and map pdf to the CakeTCPDF plugin, e.g.:

      public $components = [
          ...,
          'RequestHandler' => [
              'viewClassMap' => [
                  'pdf' => 'CakeTCPDF.Pdf'
              ]
          ]
      );
    • Add to your controller action:

      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. In your View:

    • Create a link to the a action with the extension .pdf, e.g.:

      $this->Html->link('PDF file', ['ext' => 'pdf']);
    • Place the View templates in the subdirectory Pdf, e.g.: app/View/Invoices/Pdf/index.ctp

    • Use the CakeTCPDF.exportPdfTable element in your View file, e.g.:

      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'));
    • Use the CakeTCPDF.exportPdfTableContent element in your View file, e.g.:

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

cakephp-tcpdf's People

Contributors

anklimsk avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.