当前位置:网站首页>PHP generates PDF tcpdf
PHP generates PDF tcpdf
2022-07-03 03:27:00 【qq_ thirty-one million six hundred and ninety-five thousand fiv】
Catalog
Reference material :TCPDF
Control description
- Basic functions do not require external libraries ;
- All standard page formats 、 Custom page format 、 Custom margins and units of measure ;
- UTF-8 Unicode And right to left language ;
- TrueTypeUnicode、OpenTypeUnicode、TrueType、OpenType、Type1 and CID-0 typeface ;
- Font subset ;
- Post some XHTML + CSS Code 、Javascript And forms ;
- Images 、 graphics ( Geometry ) And conversion method ;
- Native support JPEG、PNG and SVG Images ,GD All images supported (GD、GD2、GD2PART、GIF、JPEG、PNG、BMP、XBM、XPM) and ImagMagick All images supported (http:www.imagemagick.org/www/ Format .html)
- One dimensional and two-dimensional bar codes :CODE 39、ANSI MH10.8M-1983、USD-3、3 of 9、CODE 93、USS-93、Standard 2 of 5、Interleaved 2 of 5、CODE 128 A/B/C、2 and 5 Bit based UPC An extension of 、EAN 8、EAN 13、UPC-A、UPC-E、MSI、POSTNET、PLANET、RMS4CC(Royal Mail 4-state Customer Code)、CBC(Customer Bar Code)、KIX(Klant index - Customer index )、 Smart mail bar code 、Onecode、USPS-B-3200、CODABAR、CODE 11、PHARMACODE、PHARMACODE TWO-TRACKS、Datamatrix ECC200、QR-Code、PDF417;
- ICC Color profile 、 Grayscale 、RGB、CMYK、 Spot colors and transparencies ;
- Automatic header and footer management ;
- the height is 256 Bit document encryption and digital signature authentication ;
- UNDO Order business ;
- PDF notes , Include links 、 Text and file attachments ;
- Text rendering mode ( fill 、 Stroke and cut );
- Multi column mode ;
- No write page area ;
- Bookmarks and directories ;
- Text hyphen ;
- Text stretching and spacing ( track / Word spacing adjustment );
- Automatic paging 、 Line feed and text alignment , Including alignment ;
- Automatic page numbers and page groups ;
- Move and delete pages ;
- Page compression ( need php-zlib Expand );
- XOBject Templates ;
- PDF/A-1b (ISO 19005-1:2005) Support .
design sketch

Implementation code
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2019/8/30
* Time: 14:10
*/
namespace app\nim\controller;
class Pdf
{
public function demo(){
$title=' title ';// title
$fileName='demo';// File name
$html='<div>
<table style="width: 750px;overflow:hidden;" align="center" border="1" cellspacing="0" cellpadding="0"
style="background: #fff;">
<tr style="width:750px">
<td colspan="4">
<table frame="void" cellspacing="0" cellpadding="0">
<tr style="border: none;height: 30px;line-height:30px;margin: 0;padding: 0;" frame="void">
<td colspan="4" style="text-align: center;border: none;font-size: 20px;height: 45px;line-height:55px;font-weight: bold;">
form demo
</td>
</tr>
<tr style="font-size: 18px;height: 30px;" frame="void">
<td style="border: none;width: 71%;"> Tabulation number :6564646</td>
</tr>
<tr style="height: 10px;" >
<td style="width: 29%;"></td>
<td style="border: none;width: 71%;"></td>
</tr>
<tr style="font-size: 16px;text-align:left;border: none;height: 30px;margin:0">
<td style="border: none;font-size:16px;height: 30px;text-indent:15px;width:80%;" >
Tabulated date :<span class="year">2022 </span> year
<span class="month">02 </span> month
<span class="date">15 </span> Japan
</td>
</tr>
<tr style="padding: 0;width: 100%;">
<td colspan="4" style="border: none;width: 100%;">
<table style="width: 95%;font-size: 16px;margin: 0;padding: 0;" border="1" cellspacing="0" cellpadding="0">
<tr style="height: 33px;line-height: 33px;text-align: left;">
<td class="widthL" style="width:24%;height: 33px;text-indent: 8px;"> Name of preparer </td>
<td style="text-align: left;text-indent: 10px;"> Zhang Datou </td>
<td class="widthL" style="width:22%;height: 33px;text-indent: 8px;"> ID number </td>
<td style="width:29%;text-align: left;text-indent: 10px;">54657987978</td>
</tr>
<tr style="height: 33px;line-height: 33px;text-align: left;">
<td class="widthL" style="width:24%;height: 33px;text-indent: 8px;"> Tabulation matters </td>
<td colspan="3" style="text-align: left;text-indent: 10px;"> I'll just play </td>
</tr>
<tr style="height: 33px;line-height: 33px;text-align: left;">
<td rowspan="4" style="height: 33px;text-indent: 8px;">
<br/><span> ginseng </span><br/>
<span> And </span><br/>
<span> people </span><br/>
<span> member </span>
</td>
<td style="text-align: left;text-indent: 10px;"> The administration department </td>
<td colspan="2" class="widthL" style="height: 33px;text-indent: 8px;"> Xin Xiaomeng </td>
</tr>
<tr style="height: 33px;line-height: 33px;text-align: left;">
<td style="text-align: left;text-indent: 10px;"> Human Resource Center </td>
<td colspan="2" class="widthL" style="height: 33px;text-indent: 8px;"> Xiaoyuhang </td>
</tr>
<tr style="height: 33px;line-height: 33px;text-align: left;">
<td style="text-align: left;text-indent: 10px;"> Purchasing Center </td>
<td colspan="2" class="widthL" style="height: 33px;text-indent: 8px;"> Chen Xiaohong </td>
</tr>
<tr style="height: 33px;line-height: 33px;text-align: left;">
<td style="text-align: left;text-indent: 10px;"> The sales department </td>
<td colspan="2" class="widthL" style="height: 33px;text-indent: 8px;"> Hooligans </td>
</tr>
<tr style="height: 33px;line-height: 33px;text-align: left;">
<td class="widthL" style="height: 33px;text-indent: 8px;"> Project cost </td>
<td colspan="2" style="text-align: left;text-indent: 10px;">1000000 </td>
<td style="height: 33px;text-indent: 8px;">1000000</td>
</tr>
<tr style="height: 33px;line-height: 33px;text-align: left;">
<td style="text-align: left;text-indent: 10px;"> remarks </td>
<td colspan="3" class="widthL" style="height: 33px;text-indent: 8px;">PHP Generate PDFdemo Make </td>
</tr>
</table>
</td>
</tr>
<tr>
<td style="height:15px;"></td>
</tr>
</table>
</td>
</tr>
</table>
<p style="height: 20px;"></p>
<p style="width:750px;padding-bottom:30px;text-indent:0;font-size: 16px;">
<span> Auditor : Cry </span>
<span> agent : Tao Yuanming </span>
</p>
<p style="height: 30px;"></p>
</div>
';// The contents of the document : Support HTML+css
$synthesis=[];// Image watermarking
$synthesis[0]['org']=ROOT_PATH.'public/static/111.png';
$synthesis[0]['x']=155;
$synthesis[0]['y']=12;//156
$synthesis[0]['w']=40;
$synthesis[0]['h']=40;
$info=[];// QR code related parameters
$re=$this->pdfRender_D($html, $title, $fileName, $synthesis, $info);
}
/*
* Generate pdf
* @param string $html Content
* @param string $title title
* @param string $fileName The name of the generated file
* @param string $synthesis The path of the image to be synthesized
* @param number $x x Axis
* @param number $y Y Axis
* @param number $m Image width
* @param number $h Picture height
* @param number $setPage What page is the picture placed on
* @param string $append_str Append string AddPage Add page $append_str['AddPage']='1';
* @param array $info $info['uniq_key'] Unique identification $info['step'] promise :cn, agreement :xy, The resolution :jy, Equity :qy, to grant authorization :sq;
* @param number $pdf_type new PDF Storage path ID 2019/10/23
* @return Synthesis returns correctly PDF route Synthesis failure is returned as false
*/
public function pdfRender_D($html, $title = '', $fileName, $synthesis = [], $info)
{
// New path ID
$path = '/static/uploads/preview/' . date('Ym') . '/';// Project storage path
$directory='D:/phpstudy_pro/WWW/hw.com/crm/public';// root directory
$is_path = $directory. $path;// The full path
// Detect and create folders
if ($this->exists_dir($is_path) === false) {
return false;
}
// GetX obtain x Axis coordinates GetY obtain y Axis coordinates
$pdf = new \TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);//A4 PDF_PAGE_FORMAT
// Set piece information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor(str_replace("http://", "", $_SERVER['HTTP_HOST']));
$pdf->SetTitle($title);
$pdf->SetSubject($title);
$pdf->SetKeywords('TCPDF, PDF, example, test, guide,PHP');
// Set the spacing
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
// Set the default constant width font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
// Set the spacing left 、 Upper side 、 Spacing on the right
$pdf->SetMargins(PDF_MARGIN_LEFT, 11, PDF_MARGIN_RIGHT);//PDF_MARGIN_TOP
//$pdf->SetFooterMargin(0);
$pdf->setCellPaddings(0, 0, 0, 0);
$pdf->SetLineStyle(array('width' => 0.5, 'cap' => 'butt', 'join' => 'miter', 'dash' => '0', 'color' => array(0, 0, 0)));
// Set automatic page break
//$pdf->MultiCell(0, 0, 'DISK CACHING TEST: check the parameters of the class constructor.', 1, 'L', 0, 0, '', '', true);
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
// Set the image scale factor
$pdf->setCellHeightRatio(1.2);// Set cell row height 1.3 1.2
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
$pdf->SetFont('stsongstdlight', '', 13); // Set Chinese display
$pdf->AddPage();
$pdf->Cell(0, 1.2, '', 0, 1, 'C');//1.5
$pdf->writeHTML($html);// It takes more time
//PDF Add images
$num_num = $pdf->getNumPages();// Get the current page number
if ($synthesis) {
$count = count($synthesis);
foreach ($synthesis as $k => $v) {
if ($count > 10 && isset($v['qm'])) {// Signature mark
$v['setPage'] = $num_num - 1;
}
if (!isset($v['h'])) {
$v['h'] = '';
}
if (isset($v['setPage'])) {
$pdf->setPage($v['setPage']);//setPage Insert the picture like that The default is the last page $k>=6 || There are too many pictures to add new pages
}
$pdf->Image($v['org'], $v['x'], $v['y'], $v['w'], $v['h'], '', '', '', false, 300);// It takes more time
if (isset($v['html']) && $v['html']) {
$pdf->writeHTMLCell('', '', $v['x'] - 5, $v['y'] + 8, $v['html'], '', 0, 0, true, 'L', true);
}
}
}
// Add QR code on_off=1 Turn on set_page What page is the QR code placed on
$this->qr_code_pdf(
[
'pdf' => $pdf, 'uniq_key' => $info['uniq_key'] ?? '',
'on_off' => $info['on_off'] ?? 0,
'set_page' => $info['set_page'] ?? $num_num,
'num_num' => $num_num,
'is_wine' => $info['wine_footer'] ?? 0,//0 equity Others are the wine industry
]
);
// Pagination
$this->footer($pdf);// Custom paging
$pdf->Output($is_path . $fileName . ".pdf", "F");
$url = $path . $fileName . ".pdf";
if (file_exists($is_path . $fileName . ".pdf") !== false) {
return $url;
}
return false;
}
/*Page footer Custom paging Wine industry page (wine_footer) Equity page (step)
*param object PDF Plug in object
*param array $info['uniq_key'] Unique identification
* */
public function footer($pdf): void
{
$page=1;// Page number
//$pdf->setPage(1);// Setup page
// Use this function to set an optional border , Background color and HTML Text string to output cells ( Rectangular area ) W: Set cell width .0, The distance to the right H: Set the minimum height of the cell X: Abscissa with the upper left corner as the origin Y: The ordinate with the upper left corner as the origin Html:html Text Border: Frame Ln:0, Insert the contents after the cell to the right or left of the table ,1, The next row of the cell ,2, Below the cell Fill: fill Reseth:true, Reset the height of the last line Align: The location of the text Autopadding:true, Automatically adjust the distance from the text to the border .
$pdf->writeHTMLCell('', '', 14, 2, '<span style="color:#000;">' . $page . ' page</span>', '2', 0, 0, true, 'L', true);
$pdf->writeHTMLCell('', '', 80, 2, '<span style="color:#000;"> Header content </span>', '2', 0, 0, true, 'L', true);
// Set single row cells :W: Set the width of the cell H: Sets the height of the cell Text: Cell text Border: Set cell borders .0, no border ,1, A box ,L, The left margin ,R, Right margin ,B, Bottom border ,T, Top border ,LTRB Means that all four sides show Ln:0, Insert the contents after the cell to the right or left of the table ,1, The next row of the cell ,2, Below the cell Align: Text location .L, Align left ,R, Right alignment ,C, In the middle ,J, Automatic alignment Fill: fill .false, The background of the cell is transparent ,true, Cells must be filled Link: Set the link of cell text .
$pdf->SetY(8);
$pdf->Cell(180, 0, '', 1, 0, 'C', 0, '', 0, true);//180
}
/**
* Notes:pdf Add QR code http:/hw.com/sign/pdf/demo
* User: ZHOU WEI YUN
* Date: 2021/9/23 0023
* Time: Afternoon 5:37
* @param array $arr on_off=1 Start adding QR code uniq_key: Unique identification pdf The control object set_page Place page
* @return void
*/
public function qr_code_pdf(array $arr): void
{
// Generate qr code file https://tcpdf.org/examples/example_027/
if (!empty($arr['on_off']) && $arr['on_off'] === 1) {
if ($arr['num_num'] < $arr['set_page']) {
$arr['set_page'] = $arr['num_num'];
}
// QR code link
$url = $this->site() . '/h5_contract_url/' . $arr['uniq_key'];
$arr['pdf']->setPage($arr['set_page']);// The QR code is placed on the specified page
$style = array(
'border' => false,
'padding' => 0,
'fgcolor' => array(0, 0, 0),
'bgcolor' => false, //array(255,255,255)
//'module_width' => 1, // width of a single module in points
//'module_height' => 1 // height of a single module in points
);
// Generate qr code
$arr['pdf']->write2DBarcode($url, 'QRCODE,L', 16, 12, 17, 17, $style, 'N');
}
}
/**
* Notes: Get the current domain name
* User: ZHOU WEI YUN
* Date: 2022/2/12 0012
* Time: In the morning 11:38
* @param array
* @return string
*/
public function site(): string
{
return $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['SERVER_NAME'];
}
/**
* Notes: Check if the folder exists There is no automatic folder creation
* User: ZHOU WEI YUN
* Date: 2021/3/23 0023
* Time: In the morning 10:51
* @param string $path The absolute path to the file
* @return bool false File creation failed Others return to true
*/
public function exists_dir(string $path): bool
{
if (file_exists($path) === false) {
$path = str_replace("\\", '/', $path);
$path_arr = explode('/', $path);
$ping = '';
// Cycle level by level detection Create if it does not exist
foreach ($path_arr as $k => $v) {
$ping .= $v . '/';
if (file_exists($ping) === false && !mkdir($ping, 755) && is_dir($ping)) {
return false;
}
}
}
return true;
}
}边栏推荐
- Nasvit: neural architecture search of efficient visual converter with gradient conflict perception hypernetwork training
- Limit of one question per day
- The calculation of stripe, kernel and padding in CNN
- Change and access of median value of listening object
- BigVision代码
- 别再用 System.currentTimeMillis() 统计耗时了,太 Low,StopWatch 好用到爆!
- Latest version of NPM: the "NPM" item cannot be recognized as the name of a cmdlet, function, script file, or runnable program. Please check
- MySQL Real combat 45 [SQL query and Update Execution Process]
- C# WebRequest POST模式 ,基于“Basic Auth”口令认证模式,使用multipart/form-data方式上传文件及提交其他数据
- Converts a timestamp to a time in the specified format
猜你喜欢

MySql实战45讲【行锁】

Mongodb installation & Deployment

Use of El tree search method

UMI route interception (simple and rough)

The idea cannot be loaded, and the market solution can be applied (pro test)
![C programming learning notes [edited by Mr. Tan Haoqiang] (Chapter III sequence programming) 03 operators and expressions](/img/4a/1df03d9f3315debb4c335260ed39f2.jpg)
C programming learning notes [edited by Mr. Tan Haoqiang] (Chapter III sequence programming) 03 operators and expressions

VS 2019 配置tensorRT生成engine

Limit of one question per day

umi 路由拦截(简单粗暴)

Anhui University | small target tracking: large-scale data sets and baselines
随机推荐
Bigvision code
[combinatorics] number of solutions of indefinite equations (number of combinations of multiple sets R | number of non negative integer solutions of indefinite equations | number of integer solutions
Advanced redis applications [password protection, data persistence, master-slave synchronization, sentinel mode, transactions] [not completed yet (semi-finished products)]
BigVision代码
Model transformation onnx2engine
二维数组中的元素求其存储地址
Hi3536c v100r001c02spc040 cross compiler installation
Avec trois. JS fait une scène 3D simple
What happens between entering the URL and displaying the page?
将时间戳转为指定格式的时间
Vs Code configure virtual environment
MongoDB简介
文件重命名
[set theory] partial order relation (partial order relation definition | partial order set definition | greater than or equal to relation | less than or equal to relation | integer division relation |
Positioning (relative positioning, absolute positioning, fixed positioning, Z-index) 2022-2-11
[AI practice] Application xgboost Xgbregressor builds air quality prediction model (I)
Anhui University | small target tracking: large-scale data sets and baselines
Yolov5 project based on QT
Idea format code idea set shortcut key format code
The XML file generated by labelimg is converted to VOC format