Step 1 - Install the PDFlib extension
PDFlib
is not included with the default PHP 5.3 distribution, so you will have
to install it. Download the PHP version of the PHPlib library from here
.
The Windows build of PHP 5 comes in many different varieties. I'll assume that you are using the VC9 non-thread safe build, and that you have installed PHP to the default location.
Extract the libpdf_php.dll
file from the PDFlib-7.0.4p6-MSWin32-php\bind\php5\php-530-nozts_VS9\
directory in the PHPlib zip file to C:\Program Files\PHP\ext
.
Then add the following two lines to the PHP.ini
file located in C:\Program Files\PHP
:
[PHP_PDF]
extension=libpdf_php.dll
<!-- AD_PLACEHOLDER_4-->
Step 2 - Create the PDF document
Open up a new try block, create a new PDFlib instance, and then start a new PDF document by calling begin_document
.
try {
$pdf=new PDFlib();
if(!$pdf->begin_document("",""))
{
throw new PDFlibException("Error creating PDF document. ".$pdf->get_errmsg());
}
<!-- AD_PLACEHOLDER_5-->
Step 3 - Set the PDF properties
The set_info
function allows you set some optional document properties. Here we define the author and title of the PDF document.
$pdf->set_info("Author","Matthew Casperson");
$pdf->set_info("Title","Create a PDF document with PHP");
<!-- AD_PLACEHOLDER_6--><!-- AD_PLACEHOLDER_7-->Step 4 - Create a new page
The begin_page_ext
function
is called to start a new page in the PDF document. Here we have
specified the page to have dimensions of 595 x 842 points, making it an
A4 page.
$pdf->begin_page_ext(595,842,"");
Other common page sizes (in points) are:
a4
595 x 842
a5
421 x 595
a6
297 x 421
letter
612 x 792
legal
612 x 1008
ledger
1224 x 792
11x17
792 x 1224
<!-- AD_PLACEHOLDER_8-->
Step 5 - Prepare a font
To print any text to the PDF document we first need to prepare the font that will be used. The load_font
function takes a locally installed font and prepares it for use with PDFlib. The setfont
function specifies that we will be using this font when writing text.
$font=$pdf->load_font("Helvetica-Bold","winansi","");
$pdf->setfont($font,24.0);
<!-- AD_PLACEHOLDER_9-->
Step 6 - Write some text
Using the set_text_pos
and show
functions we can write some text to the PDF document at the specified location.
$pdf->set_text_pos(50,800);
$pdf->show("Hello World");
<!-- AD_PLACEHOLDER_10-->
Step 7 - End the page
The end_page_ext
function ends the page that we started in step 4.
$pdf->end_page_ext("");
<!-- AD_PLACEHOLDER_11-->
Step 8 - End the document
The end_document function ends the PDF document we started in step 2.
$pdf->end_document("");
<!-- AD_PLACEHOLDER_12-->
Step 9 - Send the PDF document
Typically
a PHP script is used to send a HTML document. In this case we need to
override this default behaviour to tell the recieving browser that it
is about to recieve a PDF document. The following code creates the HTTP
headers required to identify the data being sent as a PDF document,
which will cause the browser to open up Adobe Reader (or the default
PDF application) when the data is transmitted.
$buffer=$pdf->get_buffer();
$len=strlen($buffer);
header("Content-type: application/pdf");
header("Content-Length: $len");
header("Content-Disposition: inline; filename=example.pdf");
echo $buffer;
<!-- AD_PLACEHOLDER_13-->
Step 10 - Deal with any errors
The try block is closed, and a catch block is created. If there were any errors creating the PDF document, the user is notified.
}
catch (PDFlibException $e)
{
echo 'Error Number:'.$e->get_errnum()."n";
echo 'Error Message:'.$e->get_errmsg();
}
<!-- AD_PLACEHOLDER_14-->
Conclusion
PDFlib
makes creating PDF documents a breeze. You could extend this example to
allow users to download pre-filled PDF documents, or to create PDF
versions of a web page for easy printing.
分享到:
相关推荐
way it does is stored in the PDF file. So, no matter what fonts a balloter (or reader) has on their computer, they'll be able to see the file as you intended it to be seen. If you don't embed fonts, ...
标题 "Creating XML Documents with PL/SQL" 涉及到的是如何使用PL/SQL语言来创建、处理和解析XML文档。在Oracle数据库环境中,PL/SQL是常用的编程语言,它扩展了SQL的功能,使得开发者可以编写存储过程、函数、...
Complete example programs with colorized code illustrate each important aspect of Java programming - all in easy steps. This book assumes no previous knowledge of any programming language so it's ...
Jim Wilson作为本书的作者,是JWHedgehog, Inc.的总裁,该公司专注于为Android、iOS和Microsoft平台提供解决方案。作者拥有超过30年的软件工程经验,特别是在移动设备和基于位置的解决方案领域,已经有超过15年的...
- **标题**:“Creating Apps in Kivy.pdf” - **描述**:“Creating Apps in Kivy” 这两个部分简明扼要地指出了本书的主要内容:通过使用Kivy框架来开发应用程序。Kivy是一个用于构建高性能触摸应用的开源Python...
使用Python + Kivy 开发应用程序。 Kivy 是一个跨平台的GUI 支持Window、Linux、Mac、Android 虽然包含了代码,还是贴出代码下载地址: https://github.com/oreillymedia/creating_apps_in_kivy
[Microsoft Press] Creating Research and Scientific Documents Using Microsoft Word (英文版) [Microsoft Press] Creating Research and Scientific Documents Using Microsoft Word (E-Book) ☆ 图书概要:☆ ...
《学习C++通过创建Unreal Engine 4游戏》是一本专为希望掌握C++编程语言并利用Unreal Engine 4开发游戏的初学者和进阶者设计的书籍。Unreal Engine 4是一款强大的游戏开发引擎,由 Epic Games 开发,支持多平台游戏...
Long gone are the days of mobile apps with a static UI ...In this book, you'll learn how to use fragments to meet the challenges of creating dynamic UIs in the modern world of mobile app development.
iOS 10 SDK Development: Creating iPhone and iPad Apps with Swift by Chris Adamson English | 24 Mar. 2017 | ASIN: B071RRCK9R | 264 Pages | AZW3 | 5.24 MB All in on Swift! iOS 10 and Xcode 8 make it ...
Chapter 10: Creating Audio for Games Chapter 11: Creating 3D Graphics with WebGL Part IV: Local Storage and Multiplayer Games Chapter 12: Local Storage and Caching Chapter 13: Going Online with ...
HTML5.Games.Creating.Fun.with.HTML5.CSS3.and.WebGL.pdf HTML5.Games.Creating.Fun.with.HTML5.CSS3.and.WebGL.pdf HTML5.Games.Creating.Fun.with.HTML5.CSS3.and.WebGL.pdf
在“Learning.C++.by.Creating.Games.with.UE4.Code.zip”这个压缩包中,我们可以推测它包含了一系列关于使用C++在虚幻4中开发游戏的教程代码。通过这些代码,学习者可以逐步理解如何利用C++在虚幻4环境下创建游戏。...