tcpdf使用及字体设置

tcpdf对css支持不是很好,例如div设置为Inline属性就不好用,样式比较复杂的建议使用dompdf

require_once(ORG_PATH . "tcpdf/tcpdf.php");
$pdf = new TCPDF('P', 'mm', 'A4', true, 'UTF-8', true);
//设置PDF页面边距 LEFT, TOP, RIGHT
//$pdf->SetMargins(20, 20, 20);

//设置文件信息
$pdf->SetCreator(TITLE_NAME);
$pdf->SetAuthor(TITLE_NAME);
$pdf->SetTitle("这是标题");

//删除预定义的打印 页眉/页尾
$pdf->setPrintHeader(false);
/*
$pdf->setHeaderFont(Array('stsongstdlight', '', '10'));
//多行文本会显示不全,通过SetMargins中的top设置,可以显示更多的头部内容
//第一个arrary是设置头文本颜色,第二个是设置横线颜色
$pdf->SetHeaderData('logo.png', 30, '第一行',"第二行\n換行內容", array(30,54,25), array(30,54,25));
$pdf->SetHeaderMargin(5);
*/

$pdf->setPrintFooter(false);
/*
$pdf->setFooterFont(Array('stsongstdlight', '', '8'));
$pdf->SetFooterMargin(10);
*/

//设置默认等宽字体
$pdf->SetDefaultMonospacedFont('courier');

//设置默认字体构造子集模式,设置false将把字体嵌入到pdf中(文件会变大)
$pdf->setFontSubsetting(false);

//设置字体,字体在fonts目录中。
//有时设置字体后会发现html中不能设粗体或斜体,这和字体本身有关
//解决方法再生成粗体和斜体字体,命名为**b、**i(例如fzssjwb、fzssjwi)
//这时在css中使用<b>、<h1>等标签就会自动加粗
$pdf->SetFont('fzssjw');

//设置自动换页
$pdf->SetAutoPageBreak(true, 20);

$html = file_get_contents("1.html");

//添加一个页面
$pdf->AddPage();
// output the HTML content
$pdf->writeHTML($html, true, false, true, false, '');
$pdf->lastPage();

$pdf->Output("test001.pdf", "I");

1.html 

html页中可以不写html,head等。另外发现样式只能设置一次,不知道是不是自己测试有问题(如设置html,body{***},不能再次设置body{***},否则设置都不生效)。

如果在php中没有设置SetFont,在html中可以设置font-family达到一样效果,但font-family只能设置tcpdf中的字体

<!DOCTYPE html>
<style>
	body{
		width: 200mm;
		height: 240mm;
		margin: 15mm auto;
		padding: 0;
		font-size: 10.5pt;
		border: black 1px solid;
		/* font-family: "simsun"; */
	}
	.container {
		height: 180mm;
		width: 155mm;
		margin: 100px auto;
	}
	table{
		letter-spacing: 2px;
		line-height: 30px;
		width: 100%;
		border-collapse: collapse;
		table-layout: fixed;
	}
	.content {
		margin-top: 40px;
	}
	.between {
		margin-left: 30px;
		margin-top: 20px;
	}
	.between input{
		width: 300px;
		margin: 3mm 0;
	}
	.foot {
		margin-top: 100px;
	}
	.foot table {
		letter-spacing: 3.5px;
	}
	.qz {
		margin-top: 30px;
	}
	.qz table {
		line-height: 40px;
	}
	.table {
		float: right;
	}
	.foot span {
		text-decoration: black solid ;
		border-bottom: 1px black solid;
	}
	h1{
		text-align:center;
	}
</style>

<body>
<div class="container">
	<h1>授权委托书</h1>
	<div class="content">
		<table>
			<tr>
				<td>&nbsp;&nbsp;现本人不可撤销的向 ******* 银行</td>
			</tr>
			<tr>
				<td><strong>授权:</strong></td>
			</tr>
			<tr>
				<td style="letter-spacing: 6px">&nbsp;&nbsp;本人因办理 *********** 业务,已经</td>
			</tr>
			<tr>
				<td>向 0000000 公司支付首付款,现委托贵</td>
			</tr>
			<tr>
				<td>行将本人的信用卡(卡号 234234234342)</td>
			</tr>
			<tr>
				<td>进行启用,并将其余款项 **** 元,</td>
			</tr>
			<tr>
				<td>以透支形式扣收后支付给该公司,入账账户信息如下:</td>
			</tr>
		</table>
	</div>

	<div class="between">
		<table>
			<tr>
				<td colspan="1" class="table">账户名称:</td>
				<td colspan="4"><input type="text"  value="{$info.zhmc|default=$gs_name}" id="zhmc"></td>
			</tr>
			<tr>
				<td colspan="1" class="table">账&nbsp;&nbsp;号:</td>
				<td colspan="4"><input type="text" value="{$info.zh|default=$bank_number}" id="zh"></td>
			</tr>
			<tr>
				<td colspan="1" class="table"><span style="letter-spacing: 11px">开户</span>行:</td>
				<td colspan="4"> ********** </td>
			</tr>
		</table>
	</div>

	<div class="foot">
		<table>
			<tr>
				<td>&nbsp;&nbsp;<span>本人在签署此授权委托书之前,已认真仔细阅读上述内容,清</span></td>
			</tr>
			<tr>
				<td><span>楚知晓并自愿承担由此引发的相关法律责任。</span></td>
			</tr>
		</table>
	</div>

	<div class="qz">
		<table>
			<tr>
				<td colspan="5"></td>
				<td colspan="4">授权人:</td>
			</tr>
			<tr>
				<td colspan="6"></td>
				<td colspan="3">2019年04月01日</td>
			</tr>
		</table>
	</div>
</div>
</body>

tcpdf中设置中文字体

默认tcpdf中没带中文字体,可以自己下载ttf字体生成,使用tcpdf中自带的生成文件就行(tools/tcpdf_addfont.php) ,使用方法tools目录中有说明,执行tcpdf_addfont.php后字体文件自动生成到fonts目录中。

在使用过程中发现用 php ./tcpdf_addfont.php  -t CID0CS -i simsun.ttf (方正宋体)生成的字体会变粗,这时在html中可以正常使用h1等标签,但整体字都变粗。使用 php ./tcpdf_addfont.php  -t TrueTypeUnicode -i simsun.ttf 生成的字体很正常,但使用h1等标签时不会变粗体。我解决办法是把CID0CS生成的文件名设为simsunb(粗体),TrueTypeUnicode生成的文件名设为simsun,这样$pdf->SetFont或font-family中设置为simsun,h1等标签就会自动变为粗体。

微软雅黑字体测试使用CID0CS方式生成的字体可以正常设置加粗或斜体,这里看来字体是有很大关系的

 

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值