注册 登录
无忧答案网 返回首页

张老师的个人空间 https://www.ap5u.com/?112605 [收藏] [复制] [RSS] 专业远程教辅导

日志

TCPDF 开发文档(中文翻译版)

已有 302 次阅读2022-3-26 19:48

2017年5月3日15:06:15 

这个是英文翻译版,我看过作者的文档其实不太友善或者不方便阅读,不如wiki方便

后面补充一些,结构性文档翻译

这是一部官方网站文档,剩余大部分都是开发的时候和网络总结来的

项目官网:https://tcpdf.org/

github:https://github.com/tecnickcom/TCPDF 

都没比较完整的api文档,所以最后的demo需要总去总结,好吧

(发现所有例子全部翻译消耗时间过于长,只把其中不同的部分分离出来,也方便读者使用)

 

2018年1月24日09:27:20 

发现一个比较大的问题,不算bug,但是是一个比较蛋疼的 问题,当你需要直接将word或者excel的东西,直接获取出来,贴到编辑器,在拿出来,替换内容标签做pdf的时候,

tcpdf需要的是必须闭合的标签,要可以100%转换成xml,不然就报错 Undefined index: startcolumn in tcpdf.php (line 19456)

 

2018年1月29日17:43:18

因为新需要,合同页面变得很复杂,需要支持后端编辑合同模板,又得兼容合同编辑之后,在生成合同

 解决办法,找前端直接使用table布局或则div+css,直接把页面写出来在存起来,不然就会报错,虽然麻烦一点,而且也不是那么灵活,但是如果在不随意改变合同模板样式的架构的情况下,还是可以使用的

笔者现在已经尝试了dompdf 但是目前看来这个的中文支持不好,css也好像支持不好, 

 如果你需要弄复杂页面的合同,请参看 :http://www.cnblogs.com/zx-admin/p/8352003.html

mpdf目前对table布局和div+css布局支持唯一个支持的比较好的插件

 

新增composer 使用tcpdf

composer require tecnickcom/tcpdf
//引入tcpdf
use TCPDF;

我使用laravel是5.4,但是服务上php是5.6但是laravel 这个组件 doctrine/inflector 1.2.x-dev requires php ^7.0

需要的是7,又不好直接服务器上php7,thinkphp最新版的话就直接上7,5.6版本不会出现类似的问题,好吧,我还是觉得tp好使

如果把laravl的计划任务模块,移植到tp上就爽了,有时间在弄这个

 

 

 

Fonts设置字体

注意:以下信息仅对旧的TCPDF库有效。 新的tc-lib-pdf库使用能够即时转换字体的新的tc-lib-pdf-字体库。

TCPDF支持TrueTypeUnicode(UTF-8 Unicode),OpenTypeUnicode,TrueType,OpenType,Type1,CID-0和Core(标准)字体。
有两种使用新字体的方法:将其嵌入PDF(有或没有子集)。 当未嵌入字体时,将在系统中进行搜索。 优点是PDF文件较轻; 另一方面,如果不可用,则使用替换字体。 因此,最好确保在客户端系统上安装所需的字体。 如果该文件要被大量观众查看,建议嵌入。

TCPDF支持字体子集,以减少大型unicode字体文件的文档大小。 如果您将整个字体嵌入到PDF中,则另一端的人即使没有您的字体也可以进行更改。 如果您对字体进行子集,则PDF的文件大小会更小,但是接收PDF的人需要具有相同的字体才能更改PDF。 有关启用/禁用字体子集的选项,请参见SetFont()和AddFont()方法的源代码文档。

可以不嵌入的字体只是标准的核心字体和CID-0字体。

PDF Core(标准)字体是:

  • courier : Courier
  • courierB : Courier Bold
  • courierBI : Courier Bold Italic
  • courierI : Courier Italic
  • helvetica : Helvetica
  • helveticaB : Helvetica Bold
  • helveticaBI : Helvetica Bold Italic
  • helveticaI : Helvetica Italic
  • symbol : Symbol
  • times : Times New Roman
  • timesB : Times New Roman Bold
  • timesBI : Times New Roman Bold Italic
  • timesI : Times New Roman Italic
  • zapfdingbats : Zapf Dingbats

转换TCPDF的字体

使用addTTFfont()方法,您可以直接从TrueType,OpenType或Type1字体创建一个TCPDF字体。
注意:'fonts'文件夹必须由webserver可写。



实例:

$fontname = $pdf->addTTFfont(‘/path-to-font/DejaVuSans.ttf’, ‘TrueTypeUnicode’, “, 32);

检查addTTFfont()的源代码文档以获取更多信息。

设置字体

在配置文件(config / tcpdf_config.php)上将K_PATH_FONTS常量设置为TCPDF字体路径。
在TCPDF类构造函数的第四个参数上,如果使用Unicode字体(true)或旧字体(false),则必须指定。

要在脚本中设置字体,只需调用SetFont()方法即可。 在打印文本或生成的文档无效之前,必须至少调用此方法一次。 该方法可以在创建第一个页面之前调用,字体从页面到页面保留:

SetFont(string family[,string style[,string size]])
  • family : 字体属性 它可以是字体名称或标准系列之一(不区分大小写):
    • Courier (fixed-width)
    • Helvetica or Arial (synonymous; sans serif)
    • Times (serif)
    • Symbol (symbolic)
    • ZapfDingbats (symbolic)
    也可以传递一个空字符串。 在这种情况下,现有的家庭被保留。
  • style : 字体样式。 可能的值是(不区分大小写):
    • empty string: regular
    • B: bold
    • I: italic
    • U: underline
    或任何组合。 默认值为常规值。
  • size:字体大小分。 默认值为当前大小。 如果文档开头没有指定大小,则取值为12。
  • fontfile : 字体定义文件。 默认情况下,名称是从家庭和样式构建的,小写没有空格。
  • subset :如果true只嵌入字体的一个子集(仅存储与所使用字符相关的信息); 如果假嵌入完整字体; 如果'default'使用setFontSubsetting()设置的默认值。 此选项仅对TrueTypeUnicode字体有效。 如果要启用用户更改文档,请将此参数设置为false。 如果您对字体进行分类,那么接收PDF的人将需要使用相同的字体才能更改PDF。 PDF的文件大小也会较小,因为您仅嵌入字体的一部分。

Example:

$pdf->SetFont(‘times’, ‘BI’, 20, “, ‘false’);

Performances(性能)

  • 安装和配置PHP操作码cacher,如XCache;
  • 编辑php.ini文件并增加脚本可能消耗的最大内存量(memory_limit);
  • 编辑php.ini文件并增加每个脚本的最大执行时间(max_execution_time);
  • 编辑config / tcpdf_config.php文件:手动设置$ _SERVER ['DOCUMENT_ROOT'],K_PATH_MAIN和K_PATH_URL常量,并删除自动计算部分;
  • 如果您不使用泰语,请编辑config / tcpdf_config.php文件并将K_THAI_TOPCHARS常量设置为false;
  • 如果您不需要扩展字符,请编辑config / tcpdf_config.php文件,并将默认字体设置为核心字体;
  • 如果不需要UTF-8 Unicode,则将TCPID构造函数上的unicodefalse encoding参数设置为“ISO-8859-1”或其他字符映射。
  • 默认情况下,TCPDF允许字体子集减少嵌入式Unicode TTF字体的大小,这个过程非常慢,需要大量内存,可以使用setFontSubsetting(false)方法关闭;
  • 尽可能使用核心字体而不是嵌入字体;
  • 如果不严格要求,避免使用HTML语法(writeHTML和writeHTMLCell方法)
  • 拆分较小的大块HTML块;
  • 如果不严格要求,避免使用翻译;
  • 更改后重新启动网络服务器。

 

api使用分析实例

 

 

//去掉默认的页头页脚。比如那个横线
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);

 

//设置一个单元格,可以按比例缩放单元格大小
$pdf->Cell(0, 0, 'TEST CELL STRETCH: no stretch', 1, 1, 'C', 0, '', 0);


// 设置背景填充色
$pdf->SetFillColor(220, 255, 220);

/*
*此方法允许以换行符打印文本。
*它们可以是自动的(一旦文本到达单元格的右边界)或显式(通过\ n字符)。 输出所需的多个单元格,一个低于另一个。<br />
*文本可以对齐,居中或对齐。 单元格块可以框架并绘制背景
 */
//设置一个text文本块
$pdf->MultiCell(55, 5, '[LEFT] '.$txt, 1, 'L', 1, 0, '', '', true);

 

个人demo实例

注意pdf不是完全支持html标签,所以如果你的写入pdf的是html内容请使用txt或者qq对话框,过滤掉多数的html,

不然写入pdf就会异常,但是是openXML就不会有这个问题,写入word就没任何问题

$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
            $pdf->setPrintHeader(false);    //页面头部横线取消
            $pdf->setPrintFooter(false); //页面底部更显取消
            $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);//自动分页
            $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);//设置页面margin
            $pdf->SetFont('stsongstdlight', '', 12);//设置字体,注意在循环里面一定要把new都一起放在循环里面,不然会报错,没有设置字体,因为这个需要上下文来读取配置
            $pdf->AddPage();//增加一个页面
            $pdf->setCellPaddings(0, 0, 0, 0);//设置一个单元格的padding
            //追加用户亲笔签名到文件底部
            $identify = $v['identify'];
            $contract = M("contract")->where(array("identify" => "$identify"))->find();
            $file = M("file")->where(array("file_id" => $contract['contract_sign_pic_file_id']))->find();
            $sign_pic_url = app_standard_path_new($file['file_path']);

            //tcpdf支持远程图片,所以不用麻烦

            $pdf->writeHTML($contract_content, $ln = true, $fill = false, $reseth = false, $cell = false, $align = '1');//这个如果里面有远程图片,不能直接获取,需要在代码本地才行,把

            $pdf->writeHTML('<b>用户签名</b><br>', $ln = true, $fill = false, $reseth = false, $cell = false, $align = '1');

            $pdf->Image($sign_pic_url, '', '', '40%', '40%', '', '', 'T', false, 300, '', false, false, 1, false, false, false);
//这个可以获得远程图片地址,但是注意它一定是可以在公网可以访问或者授权的
//如果怕图片太大也是可以安比例缩小,放大的

// i 输出到浏览器,D下来php://output S保存
contractdata=pdf->Output($file_name, 'S');

file_put_contents(tmpname,contract_data);//所以写入你想写入的地方的文件

 新增实例,吧图片章浮动到文字上面demo 

2018年1月17日11:34:27

 

例子的官方地址  https://tcpdf.org/examples/ 


<?php

// Include the main TCPDF library (search for installation path).
require_once('./tcpdf/tcpdf.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 001'); //设置文件的title
$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 . ' 001', PDF_HEADER_STRING, array(0, 64, 255), array(0, 64, 128)); //设置头部,比如header_logo,header_title,header_string及其属性
$pdf->setFooterData(array(0, 64, 0), array(0, 64, 128));

// 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); //设置margins 参考css的margins
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER); //设置页头margins
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER); //设置页脚margins
// 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 default font subsetting mode
$pdf->setFontSubsetting(true); //设置默认字体子集模式
// Set font
// dejavusans is a UTF-8 Unicode font, if you only need to
// print standard ASCII chars, you can use core fonts like
// helvetica or times to reduce file size.
$pdf->SetFont('dejavusans', '', 14, '', true); //设置字体
// Add a page
// This method has several options, check the source code documentation for more information.
$pdf->AddPage(); //增加一个页面
// set text shadow effect  设置文字阴影效果
$pdf->setTextShadow(array('enabled' => true, 'depth_w' => 0.2, 'depth_h' => 0.2, 'color' => array(196, 196, 196), 'opacity' => 1, 'blend_mode' => 'Normal'));

// Set some content to print
$html = <<<EOD
<h1>Welcome to <a href="http://www.tcpdf.org" style="text-decoration:none;background-color:#CC0000;color:black;">&nbsp;<span style="color:black;">TC</span><span style="color:white;">PDF</span>&nbsp;</a>!</h1>
<i>This is the first example of TCPDF library.</i>
<p>This text is printed using the <i>writeHTMLCell()</i> method but you can also use: <i>Multicell(), writeHTML(), Write(), Cell() and Text()</i>.</p>
<p>Please check the source code documentation and other examples for further information.</p>
<p style="color:#CC0000;">TO IMPROVE AND EXPAND TCPDF I NEED YOUR SUPPORT, PLEASE <a href="http://sourceforge.net/donate/index.php?group_id=128076">MAKE A DONATION!</a></p>
EOD;

/*
 * 
  此方法允许以换行符打印文本。
  它们可以是自动的(一旦文本到达单元格的右边界)或显式(通过\ n字符)。 输出所需的许多单元,一个低于另一个。
    文本可以对齐,居中或对齐。 单元格块可以框架并绘制背景。
 */

// Print text using writeHTMLCell() 
$pdf->writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, '', true); //使用writeHTMLCell打印文本
// ---------------------------------------------------------
// Close and output PDF document
// This method has several options, check the source code documentation for more information.
$pdf->Output('example_001.pdf', 'I'); //I输出在浏览器上

//============================================================+
// END OF FILE
//============================================================+


Example 001 : first example with default Header and Footer  第一个例子设置默认页眉和页脚的


Example 002 : without Header and Footer  取消页眉和页脚 

Example 003 : custom Header and Footer  自定义页眉和页脚

Example 004 : text Stretching with Cell()

Example 005 : Multicell()

Example 006 : WriteHTML()

Example 007 : independent columns with WriteHTMLCell()

Example 008 : external UTF-8 Unicode text file

Example 009 : Image()

Example 010 : text on multiple columns

Example 011 : table with primitive methods

Example 012 : graphic methods

Example 013 : graphic transformations

Example 014 : forms and javascript

Example 015 : index with Bookmarks()

Example 016 : document encryption

Example 017 : independent columns with MultiCell()

Example 018 : Persian and Arabic language on RTL document

Example 019 : alternative config file

Example 020 : complex alignment with Multicell()

Example 021 : writeHTML() text flow

Example 022 : CMYK colors

Example 023 : page groups

Example 024 : object visibility with setVisibility() and layers with startLayer()

Example 025 : object transparency with SetAlpha()

Example 026 : text clipping

Example 027 : 1D barcodes

Example 028 : multiple page formats

Example 029 : Set PDF viewer display preferences with setViewerPreferences()

Example 030 : colour gradients

Example 031 : pie chart graphic

Example 032 : EPS/AI vectorial image with ImageEPS()

Example 033 : mixed font types (TrueType Unicode, core, CID-0)

Example 034 : clipping masks

Example 035 : border styles with SetLineStyle()

Example 036 : PDF text annotations

Example 037 : spot colors

Example 038 : unembedded CID-0 CJK font

Example 039 : HTML text justification

Example 040 : booklet mode (double-sided pages)

Example 041 : file attachment

Example 042 : image with transparency (alpha channel)

Example 043 : disk caching

Example 044 : move, copy and delete pages

Example 045 : table of contents

Example 046 : text hyphenation

Example 047 : transactions and UNDO

Example 048 : HTML tables with header and rowspan

Example 049 : call TCPDF methods in HTML

Example 050 : 2D barcodes (QR-Code, Datamatrix ECC200 and PDF417)

Example 051 : image as a page background

Example 052 : digital signature certification

Example 053 : javascript functions

Example 054 : XHTML form

Example 055 : core fonts dump

Example 056 : crop marks and registration marks

Example 057 : vertical alignment and metrics on Cell()

Example 058 : SVG vectorial image with ImageSVG()

Example 059 : table of contents with HTML templates

Example 060 : advanced page settings

Example 061 : XHTML + CSS

Example 062 : XObject templates

Example 063 : text stretching and spacing (tracking/kerning)

Example 064 : no-write page regions

Example 065 : PDF/A-1b (ISO 19005-1:2005) document

原文链接

https://www.cnblogs.com/zx-admin/p/6801978.html


路过

雷人

握手

鲜花

鸡蛋

评论 (0 个评论)

facelist

您需要登录后才可以评论 登录 | 注册

QQ|手机版|小黑屋|网站地图|无忧答案网 ( 冀ICP备18010495号-1 )

GMT+8, 2024-4-20 23:16

Powered by 无忧答案网 X3.5

Copyright © 2018-2020 Design: Ap5u.Com

返回顶部