设为首页 收藏本站
查看: 685|回复: 0

[经验分享] Creating PDF documents with PHP in 10 steps

[复制链接]

尚未签到

发表于 2017-4-7 09:31:45 | 显示全部楼层 |阅读模式
  Step 1 - Install the PDFlib extension
  PDFlibis not included with the default PHP 5.3 distribution, so you will haveto 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.dllfile 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.inifile 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_infofunction 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--> DSC0000.jpg <!-- AD_PLACEHOLDER_7-->Step 4 - Create a new page
  The begin_page_ext functionis called to start a new page in the PDF document. Here we havespecified the page to have dimensions of 595 x 842 points, making it anA4 page.
  $pdf->begin_page_ext(595,842,"");
  Other common page sizes (in points) are:
  a4595 x 842
  a5421 x 595
  a6297 x 421
  letter612 x 792
  legal612 x 1008
  ledger1224 x 792
  11x17792 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_fontfunction takes a locally installed font and prepares it for use with PDFlib. The setfontfunction 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_posand showfunctions 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_extfunction 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
  Typicallya PHP script is used to send a HTML document. In this case we need tooverride this default behaviour to tell the recieving browser that itis about to recieve a PDF document. The following code creates the HTTPheaders required to identify the data being sent as a PDF document,which will cause the browser to open up Adobe Reader (or the defaultPDF 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
  PDFlibmakes creating PDF documents a breeze. You could extend this example toallow users to download pre-filled PDF documents, or to create PDFversions of a web page for easy printing.

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-361325-1-1.html 上篇帖子: PHP获取当前URL URI 等server相关信息 下篇帖子: 用 Smarty 分离 PHP 应用程序中的形式与功能
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表