• 首页
  • vue
  • TypeScript
  • JavaScript
  • scss
  • css3
  • html5
  • php
  • MySQL
  • redis
  • jQuery
  • 位置: php 中文手册 -> PECL扩展库

    PDF文件库

    PDFlib Lite是开源的。然而,PDFlib Lite许可证只允许在特定条件下免费使用。PDFlib Lite支持PDFlib功能的一个子集;有关详细信息,请参阅PDFlib网站。完整版本的PDFlib可在http://www.PDFlib.com/products/PDFlib-family/下载,但要求您购买商业使用许可证。

    2000年3月9日以后的任何PHP 4版本都不支持3.0以前的PDFlib版本。PHP 4.3.0和更高版本支持pdflib4.0或更高版本

    此 PECL 扩展未与 PHP 捆绑。 安装此 PECL 扩展相关的信息可在手册中标题为 PECL 扩展的安装章节中找到。更多信息如新的发行版本、下载、源文件、 维护人员信息及变更日志等,都在此处: https://pecl.php.net/package/pdflib.

    要使这些函数在PHP<4.3.9中工作,必须使用--with-pdflib[=DIR]. DIR默认是/usr/local.

    because I confused in step 2 I write this comment:
    for installing following this instruction step by step:
    download PDFlib-Lite-X.X.XpX form first post...
    open terminal and type:
    $ cd download folder...
    $ sudo tar -xzvf PDFlib-Lite-X.X.XpX.tar.gz
    # cd PDFlib-Lite-X.X.XpX
    # ./configure
    # make
    # make install
    next:
    # apt-get install php5-dev    : online installation if not installed
    # apt-get install php-pear    : online installation if not installed
    # pecl install pdflib         : online installation
        The ask be: 'path to pdflib installation? : ' enter '/usr/local'
    # gedit /etc/php5/apache2/php.ini
        add 'extension=pdf.so in' 1 line
    # /etc/init.d/apache2 reload  OR /etc/init.d/apache2 restart
    Should work! for test you can write "$p = new PDF_new();" in a file.
    

    大多数功能都相当容易使用。最困难的部分可能是创建第一个PDF文档。下面的例子应该有助于您开始。它是为PHP 4开发的,用一个页面创建文件hello.pdf。它定义一些文档信息字段内容,加载Helvetica粗体字体并输出文本“Hello world!(说PHP)。

    <?php
    $p = PDF_new();
    /*  open new PDF file; insert a file name to create the PDF on disk */
    if (PDF_begin_document($p, "", "") == 0) {
        die("Error: " . PDF_get_errmsg($p));
    }
    PDF_set_info($p, "Creator", "hello.php");
    PDF_set_info($p, "Author", "Rainer Schaaf");
    PDF_set_info($p, "Title", "Hello world (PHP)!");
    PDF_begin_page_ext($p, 595, 842, "");
    $font = PDF_load_font($p, "Helvetica-Bold", "winansi", "");
    PDF_setfont($p, $font, 24.0);
    PDF_set_text_pos($p, 50, 700);
    PDF_show($p, "Hello world!");
    PDF_continue_text($p, "(says PHP)");
    PDF_end_page_ext($p, "");
    PDF_end_document($p, "");
    $buf = PDF_get_buffer($p);
    $len = strlen($buf);
    header("Content-type: application/pdf");
    header("Content-Length: $len");
    header("Content-Disposition: inline; filename=hello.pdf");
    print $buf;
    PDF_delete($p);
    ?>
    

    The following example comes with the PDFlib distribution for PHP 5. It uses the new exception handling and object encapsulation features available in PHP 5. It creates the filehello.pdfwith one page. It defines some document info field contents, loads the Helvetica-Bold font and outputs the text "Hello world! (says PHP)".

    Example #2 Hello World example from PDFlib distribution for PHP 5

    <?php
    try {
        $p = new PDFlib();
        /*  open new PDF file; insert a file name to create the PDF on disk */
        if ($p->begin_document("", "") == 0) {
            die("Error: " . $p->get_errmsg());
        }
        $p->set_info("Creator", "hello.php");
        $p->set_info("Author", "Rainer Schaaf");
        $p->set_info("Title", "Hello world (PHP)!");
        $p->begin_page_ext(595, 842, "");
        $font = $p->load_font("Helvetica-Bold", "winansi", "");
        $p->setfont($font, 24.0);
        $p->set_text_pos(50, 700);
        $p->show("Hello world!");
        $p->continue_text("(says PHP)");
        $p->end_page_ext("");
        $p->end_document("");
        $buf = $p->get_buffer();
        $len = strlen($buf);
        header("Content-type: application/pdf");
        header("Content-Length: $len");
        header("Content-Disposition: inline; filename=hello.pdf");
        print $buf;
    }
    catch (PDFlibException $e) {
        die("PDFlib exception occurred in hello sample:\n" .
        "[" . $e->get_errnum() . "] " . $e->get_apiname() . ": " .
        $e->get_errmsg() . "\n");
    }
    catch (Exception $e) {
        die($e);
    }
    $p = 0;
    ?>