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 请问一下这个运行结果是怎么来的
  • ¥15 这个复选框什么作用?
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下