search
Homephp教程php手册用PHP生成PDF文件 with FPDF_php基础

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


这里可以下到最新的windows下的版本:
http://www.fpdf.org/en/dl.php?v=152&f=zip


这里可以下到中文手册:
http://www.fpdf.org/en/dl.php?id=72

若要其他版本可以到这里下到:
http://www.fpdf.org/
这也是FPDF的官方网站,如果有问题可以去上面问,回复的挺快的,我上午问了1个问题他们下午就回了。不过那上面的时间都是美国时间。

下面是一个很简单的用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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

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.