当前位置:网站首页>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;
}
}
边栏推荐
- C programming learning notes [edited by Mr. Tan Haoqiang] (Chapter III sequence programming) 03 operators and expressions
- com.fasterxml.jackson.databind.exc.InvalidFormatException问题
- PHP constructor with parameters - PHP constructor with a parameter
- Small guide for rapid formation of manipulator (VIII): kinematic modeling (standard DH method)
- Spark on yarn resource optimization ideas notes
- Learning notes of C programming [compiled by Mr. Tan Haoqiang] (Chapter III sequence programming) 04 C sentence
- Pytoch lightweight visualization tool wandb (local)
- The XML file generated by labelimg is converted to VOC format
- 二进制流转换成字节数组
- Stepping on pits and solutions when using inputfilter to limit EditText
猜你喜欢
MySql实战45讲【SQL查询和更新执行流程】
On the adjacency matrix and adjacency table of graph storage
VS 2019 配置tensorRT生成engine
Summary of matrix knowledge points in Chapter 2 of Linear Algebra (Jeff's self perception)
MongoDB安装 & 部署
Limit of one question per day
Elsevier latex submitted the article pdftex def Error: File `thumbnails/cas-email. jpeg‘ not found: using draf
The series of hyperbolic function in daily problem
The idea cannot be loaded, and the market solution can be applied (pro test)
UMI route interception (simple and rough)
随机推荐
[algebraic structure] group (definition of group | basic properties of group | proof method of group | commutative group)
Summary of matrix knowledge points in Chapter 2 of Linear Algebra (Jeff's self perception)
Use three JS make a simple 3D scene
Pytoch configuration
Positioning (relative positioning, absolute positioning, fixed positioning, Z-index) 2022-2-11
Using jasmine to monitor constructors - spying on a constructor using Jasmine
L'index des paramètres d'erreur est sorti de la plage pour les requêtes floues (1 > Nombre de paramètres, qui est 0)
The idea cannot be loaded, and the market solution can be applied (pro test)
从输入URL到页面展示这中间发生了什么?
Vs Code configure virtual environment
解决高并发下System.currentTimeMillis卡顿
403 error displayed when vs cloning
MySql实战45讲【事务隔离】
C programming learning notes [edited by Mr. Tan Haoqiang] (Chapter III sequence programming) 05 data input and output
[combinatorics] basic counting principle (addition principle | multiplication principle)
navicat 导出数据库的表结构
Mongodb installation & Deployment
Mongodb replication set [master-slave replication]
二进制流转换成字节数组
PHP constructor with parameters - PHP constructor with a parameter