dtdd25012 2013-12-26 09:14
浏览 127
已采纳

在Laravel中扩展TCPDF类

I'm starting with Laravel 4 and I'd like to use TCPDF to create documents with custom header and footer, like in this example. To do it, I need to extend the original class somewhere and then use extended class in service provider. I also use https://github.com/maxxscho/laravel-tcpdf as service provider for TCPDF.

  • 写回答

1条回答 默认 最新

  • dongqi8030 2013-12-27 21:50
    关注

    TCPDF is a dependency of my laravel-tcpdf service provider. This means it is autoloaded from Laravel. You can create your own class which extends TCPDF directly and then override the header() and footer() method. For example create the file app/pdf/Pdf.php:

    <?php namespace Pdf;
    
    class Pdf extends \TCPDF {
    
        //Page header
        public function Header() {
    
            // Set font
            $this->SetFont('helvetica', 'B', 20);
            // Title
            $this->Cell(0, 15, '<< TCPDF Example 003 >>', 0, false, 'C', 0, '', 0, false, 'M', 'M');
        }
    
        // Page footer
        public function Footer() {
            // Position at 15 mm from bottom
            $this->SetY(-15);
            // Set font
            $this->SetFont('helvetica', 'I', 8);
            // Page number
            $this->Cell(0, 10, 'Page '.$this->getAliasNumPage().'/'.$this->getAliasNbPages(), 0, false, 'C', 0, '', 0, false, 'T', 'M');
        }
    }
    

    To autoload you new file add "app/pdf" to you composer.json autoload classmap and then run composer dump-autoload from your console.

    Now create your PDF:

    // create new PDF document
    $pdf = new \Pdf\Pdf;
    
    // set document information
    $pdf->SetCreator(PDF_CREATOR);
    $pdf->SetAuthor('Nicola Asuni');
    $pdf->SetTitle('TCPDF Example 003');
    $pdf->SetSubject('TCPDF Tutorial');
    $pdf->SetKeywords('TCPDF, PDF, example, test, guide');
    
    // ....
    

    I know, it's not the best solution, but it should work. My package was a little quick'n'dirty because I needed TCPDF in a Laravel project to and so I created this little package. I will optimize my package so you can override functions easier.

    For now, I hope, this will help you!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?