How to use PHP for PDF generation and manipulation
PDF (Portable Document Format) is a commonly used electronic document format, usually used for printing and sharing files. In web development, sometimes we need to generate and manipulate PDF files programmatically. PHP is a widely used server-side language with powerful PDF processing capabilities. This article will introduce how to use PHP to generate and manipulate PDF files, and provide some code examples.
1. Generate PDF files
FPDF is a popular PHP library for generating PDF files on the server. It provides methods for creating document structure and content. The following is a code example for using FPDF to generate a simple PDF file:
require('fpdf/fpdf.php'); // 创建PDF对象 $pdf = new FPDF(); // 添加一页 $pdf->AddPage(); // 设置字体和大小 $pdf->SetFont('Arial', 'B', 16); // 写入内容 $pdf->Cell(40, 10, 'Hello World!'); // 保存PDF文件 $pdf->Output('output.pdf', 'F');
TCPDF is another popular PDF generation library in PHP that provides more Lots of features and options. The following is a code example for using TCPDF to generate a simple PDF file:
require('tcpdf/tcpdf.php'); // 创建PDF对象 $pdf = new TCPDF(); // 添加一页 $pdf->AddPage(); // 设置字体和大小 $pdf->SetFont('helvetica', '', 14); // 写入内容 $pdf->Cell(0, 10, 'Hello World!', 0, 1); // 保存PDF文件 $pdf->Output('output.pdf', 'F');
2. Manipulate existing PDF files
Yes Sometimes we need to merge multiple PDF files into one file. The following code example shows how to merge two PDF files using the FPDI library:
require('fpdf/fpdf.php'); require('fpdi/fpdi.php'); // 创建FPDI对象 $pdf = new FPDI(); // 添加一页 $pdf->AddPage(); // 设置字体和大小 $pdf->SetFont('Arial', 'B', 16); // 导入第一个PDF文件 $pdf->setSourceFile('file1.pdf'); $tplIdx = $pdf->importPage(1); $pdf->useTemplate($tplIdx, 10, 10, 100); // 导入第二个PDF文件 $pdf->setSourceFile('file2.pdf'); $tplIdx = $pdf->importPage(1); $pdf->useTemplate($tplIdx, 10, 50, 100); // 保存合并后的PDF文件 $pdf->Output('output.pdf', 'F');
Sometimes we need to insert pictures in PDF files . The following code example shows how to insert images in PDF files using FPDI and FPDF libraries:
require('fpdf/fpdf.php'); require('fpdi/fpdi.php'); // 创建FPDI和FPDF对象 $pdf = new FPDI(); $pageCount = $pdf->setSourceFile('source.pdf'); $TemplateId = $pdf->importPage(1); $pdf->AddPage(); // 插入图片 $pdf->useTemplate($TemplateId, 0, 0, 210, 297); $pdf->Image('image.jpg', 50, 50, 100, 0); $pdf->Output('output.pdf', 'F');
Sometimes we need to extract text from PDF files to extract text for further processing. The following code example shows how to extract text from a PDF file using the FPDF and FPDI libraries:
require('fpdf/fpdf.php'); require('fpdi/fpdi.php'); // 创建FPDI对象 $pdf = new FPDI(); // 获取页面数量 $pageCount = $pdf->setSourceFile('file.pdf'); // 循环提取每个页面的文本 for ($pageNumber = 1; $pageNumber <= $pageCount; $pageNumber++) { // 导入页面 $tplIdx = $pdf->importPage($pageNumber); // 提取文本 $text = $pdf->getPageText($tplIdx); // 输出文本 echo $text; }
Summary:
This article describes how to use PHP to generate and manipulate PDF files. We used the FPDF and TCPDF libraries to generate PDF files, and the FPDI library to merge PDF files, insert images, and extract text. I hope these examples can help you better understand how to perform PDF processing in PHP and achieve more interesting functions.
The above is the detailed content of How to use PHP for PDF generation and manipulation. For more information, please follow other related articles on the PHP Chinese website!