Home php教程 php手册 用PHP生成PDF文件 with FPDF

用PHP生成PDF文件 with FPDF

Jun 21, 2016 am 09:15 AM
fpdf gt pdf

What is FPDF?
FPDF is a PHP class which allows to generate PDF files with pure PHP, that is to say without using the PDFlib library. The advantage is that PDFlib requires a fee for a commercial usage. F from FPDF stands for Free: you may use it for any kind of usage and modify it to suit your needs.

FPDF可以让我们创建pdf文件而不用去调用PDFlib

下面是一个很简单的用FPDF创建一个pdf文件并添加一页的例子


define('FPDF_FONTPATH','fpdf152/font/');
require('fpdf152/fpdf.php');
$pdf = new FPDF;
$pdf->AddPage();
$pdf->Output('a.pdf', 'D');
?>

当然也可以用$pdf->open();来新建一个pdf文件
但是在这里,Addpage()包含了open(),他同时完成了新建一个pdf文件和添加1页两个步骤

上面例子里的define('FPDF_FONTPATH','fpdf152/font/');定义了存放字体文件的目录
具体下载了压缩包后就看到那个font文件夹了,只要指向那个文件夹就行了
上面这段程序还不能显示任何东西
现在来加两句


define('FPDF_FONTPATH','fpdf152/font/');
require('fpdf152/fpdf.php');
$pdf = new FPDF;
$pdf->AddPage();
$pdf->SetFont('arial');
$pdf->Text(5,20,'test pdf');
$pdf->Output('a.pdf', 'D');
?>

SetFont()设定字体,这步在第1次调用FPDF的时候一定要,否则pdf文件打开时将会提示“没有定义字型“而什么都不显示
这里最好定义比较常用的字体,并且中文不支持。
要支持中文或支持比较偏的字体要用到AddFont(),但是我测试暂时没通过,希望哪位通过的可以指点一下 ^^
Text()打印一个字符串,横坐标为5,纵坐标为20,字符串内容为“test pdf”
关于最后的Output()里的参数'D'可以参照手册里,写的很清楚了
这样导出的pdf文件就会显示'test pdf'了。
如果兴趣好,再加上张图片


define('FPDF_FONTPATH','fpdf152/font/');
require('fpdf152/fpdf.php');
$pdf = new FPDF;
$pdf->AddPage();
$pdf->SetFont('arial');
$pdf->Text(5,20,'test pdf');
$pdf->Image('jianxin_mark.jpg', 5, 30, 60, 50);
$pdf->Output('a.pdf', 'D');
?>

Image()可以在pdf中插入图片,前面是文件名,要包括路径,5是横坐标,30是纵坐标,60是图片宽度,50是高度
ok,这样一个又有文字又有图片的pdf文件就建立了 ^^
其实还有很多功能,手册中都有写,但不是很详细,需要各位自己去研究发掘了 ^^



Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are the differences between Huawei GT3 Pro and GT4? What are the differences between Huawei GT3 Pro and GT4? Dec 29, 2023 pm 02:27 PM

Many users will choose the Huawei brand when choosing smart watches. Among them, Huawei GT3pro and GT4 are very popular choices. Many users are curious about the difference between Huawei GT3pro and GT4. Let’s introduce the two to you. . What are the differences between Huawei GT3pro and GT4? 1. Appearance GT4: 46mm and 41mm, the material is glass mirror + stainless steel body + high-resolution fiber back shell. GT3pro: 46.6mm and 42.9mm, the material is sapphire glass + titanium body/ceramic body + ceramic back shell 2. Healthy GT4: Using the latest Huawei Truseen5.5+ algorithm, the results will be more accurate. GT3pro: Added ECG electrocardiogram and blood vessel and safety

How to embed a PDF document in an Excel worksheet How to embed a PDF document in an Excel worksheet May 28, 2023 am 09:17 AM

It is usually necessary to insert PDF documents into Excel worksheets. Just like a company's project list, we can instantly append text and character data to Excel cells. But what if you want to attach the solution design for a specific project to its corresponding data row? Well, people often stop and think. Sometimes thinking doesn't work either because the solution isn't simple. Dig deeper into this article to learn how to easily insert multiple PDF documents into an Excel worksheet, along with very specific rows of data. Example Scenario In the example shown in this article, we have a column called ProductCategory that lists a project name in each cell. Another column ProductSpeci

How to merge PDFs on iPhone How to merge PDFs on iPhone Feb 02, 2024 pm 04:05 PM

When working with multiple documents or multiple pages of the same document, you may want to combine them into a single file to share with others. For easy sharing, Apple allows you to merge multiple PDF files into one file to avoid sending multiple files. In this post, we will help you know all the ways to merge two or more PDFs into one PDF file on iPhone. How to Merge PDFs on iPhone On iOS, you can merge PDF files into one in two ways – using the Files app and the Shortcuts app. Method 1: Using Files App The easiest way to merge two or more PDFs into one file is to use the Files app. Open on iPhone

3 Ways to Get Text from PDF on iPhone 3 Ways to Get Text from PDF on iPhone Mar 16, 2024 pm 09:20 PM

Apple's Live Text feature recognizes text, handwritten notes and numbers in photos or through the Camera app and allows you to paste that information onto any other app. But what to do when you're working with a PDF and want to extract text from it? In this post, we will explain all the ways to extract text from PDF files on iPhone. How to Get Text from PDF File on iPhone [3 Methods] Method 1: Drag Text on PDF The easiest way to extract text from PDF is to copy it, just like on any other app with text . 1. Open the PDF file you want to extract text from, then long press anywhere on the PDF and start dragging the part of the text you want to copy. 2

How to verify signature in PDF How to verify signature in PDF Feb 18, 2024 pm 05:33 PM

We usually receive PDF files from the government or other agencies, some with digital signatures. After verifying the signature, we see the SignatureValid message and a green check mark. If the signature is not verified, the validity is unknown. Verifying signatures is important, let’s see how to do it in PDF. How to Verify Signatures in PDF Verifying signatures in PDF format makes it more trustworthy and the document more likely to be accepted. You can verify signatures in PDF documents in the following ways. Open the PDF in Adobe Reader Right-click the signature and select Show Signature Properties Click the Show Signer Certificate button Add the signature to the Trusted Certificates list from the Trust tab Click Verify Signature to complete the verification Let

How to process PDF files using PHP How to process PDF files using PHP Jun 19, 2023 pm 02:41 PM

As a universal file format, PDF files are widely used in various application scenarios, such as e-books, reports, contracts, etc. During the development process, we often need to generate, edit, read and other operations on PDF files. As a scripting language, PHP can also easily complete these tasks. This article will introduce how to use PHP to process PDF files. 1. Generate PDF files There are many ways to generate PDF files, the most common of which is to use the PDF library. PDF library is a tool that generates PDF documents for

How to convert pdg files to pdf How to convert pdg files to pdf Nov 14, 2023 am 10:41 AM

Methods include: 1. Use professional document conversion tools; 2. Use online conversion tools; 3. Use virtual printers.

How to import and annotate PDFs in Apple Notes How to import and annotate PDFs in Apple Notes Oct 13, 2023 am 08:05 AM

In iOS 17 and MacOS Sonoma, Apple added the ability to open and annotate PDFs directly in the Notes app. Read on to find out how it's done. In the latest versions of iOS and macOS, Apple has updated the Notes app to support inline PDFs, which means you can insert PDFs into Notes and then read, annotate, and collaborate on the document. This feature also works with scanned documents and is available on both iPhone and iPad. Annotate a PDF in Notes on iPhone and iPad If you're using an iPhone and want to annotate a PDF in Notes, the first thing to do is select the PDF file

See all articles