温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

thinkPHP框架整合tcpdf插件

发布时间:2021-06-03 18:02:31 来源:亿速云 阅读:381 作者:Leah 栏目:开发技术

thinkPHP框架整合tcpdf插件?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

步骤:

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');路径要为据对路径。

看完上述内容,你们掌握thinkPHP框架整合tcpdf插件的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI