Thinkphp 整合tcpdf

文章链接:http://www.cnblogs.com/liushiyong1/p/4201117.html

网上查了些关于tcpdf 使用教程,整合到TP的话,会有些小问题,由于基础还不是很扎实,花了点时间终于整合OK了。下面介绍步骤:
环境:
  TP版本:TP3.2.2
  tcpdf:tcpdf_6_2_3

  1. 将tcpdf_6_2_3.zip解压在Web root目录下面,把examples文件夹下面的tcpdf_include.php文件拷贝到tcpdf文件夹下面,再把tcpdf/config/tcpdf_config.php内容替换成tcpdf/examples/config/tcpdf_config_alt.php中的内容
      这里要注意的是:tcp_include.php中 $tcpdf_include_dirs数组要多添加一行:“realpath('./').'/tcpdf/tcpdf.php',”
  2. 新建test.php
      
    复制代码

    <?php
    // Include the main TCPDF library (search for installation path).require_once('./tcpdf/tcpdf_include.php');
    // create new PDF document$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
    // set document information
    $pdf->SetCreator(PDF_CREATOR);
    $pdf->SetAuthor('Nicola Asuni');
    $pdf->SetTitle('TCPDF Example');
    $pdf->SetSubject('TCPDF Tutorial');
    $pdf->SetKeywords('TCPDF, PDF, example, test, guide');
    // set default header data$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' wisvalley', PDF_HEADER_STRING);
    // set header and footer fonts
    $pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
    $pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
    // set default monospaced font$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
    // set margins$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
    $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
    // set auto page breaks$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
    // set image scale factor$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
    // set some language-dependent strings (optional)if (@file_exists(dirname(FILE).'/lang/eng.php')) { require_once(dirname(FILE).'/lang/eng.php'); $pdf->setLanguageArray($l);}
    // ---------------------------------------------------------// set font//
    $pdf->SetFont('helvetica', '', 20);
    $pdf->SetFont('stsongstdlight', '', 20);
    // add a page$pdf->AddPage();
    $txt = 'your content';$pdf->Write(0, $txt, '', 0, 'L', true, 0, false, false, 0);
    $pdf->Output('/var/www/example_038.pdf', 'I');
    //浏览器预览//$pdf->Output('example_038.pdf', 'F');
    //存储文件//$pdf->Output('example_038.pdf', 'D');
    //下载文件
    复制代码

这样就可以了。下面说下我碰到的几个问题:
  1.我把这些代码拷贝到TP控制器的某个方法里面报错:Class 'Home\Controller\TCPDF' not found
    解答:$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
        改成$pdf = new \TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
        对于tp3.2引入了命名空间,这个‘\’很重要

2.**TCPDF ERROR: **Unable to create output file: example_038.pdf
    解答:$pdf->Output('/var/www/example_038.pdf', 'I');路径要为据对路径。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,001评论 19 139
  • 现在 你看到了 就很好 遇见 现在 你在 就好 ---无良
    无良雨落_阅读 147评论 0 0
  • 1、 一天,妻子,情妇,红颜、初恋在一起打麻将。 男人端了水过来,被障碍物绊倒,玻璃划破了腿。 情妇赶过来把他扶到...
    16aa5fab5f62阅读 136评论 0 0
  • 今天读李笑来老师《把时间当作朋友》p73-111 感知时间、记录开销、制定预算、计划、列表、流程、预演、验收。 感...
    拱卒人生陈艺博阅读 159评论 0 0