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

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
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

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!