PHP  
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
search for in the  
<overloadpdf_add_annotation>
view the version of this page
Last updated: Thu, 21 Aug 2003

LXXVIII. PDF functions

Introduction

The PDF functions in PHP can create PDF files using the PDFlib library created by Thomas Merz.

The documentation in this section is only meant to be an overview of the available functions in the PDFlib library and should not be considered an exhaustive reference. Please consult the documentation included in the source distribution of PDFlib for the full and detailed explanation of each function here. It provides a very good overview of what PDFlib is capable of doing and contains the most up-to-date documentation of all functions.

All of the functions in PDFlib and the PHP module have identical function names and parameters. You will need to understand some of the basic concepts of PDF and PostScript to efficiently use this extension. All lengths and coordinates are measured in PostScript points. There are generally 72 PostScript points to an inch, but this depends on the output resolution. Please see the PDFlib documentation included with the source distribution of PDFlib for a more thorough explanation of the coordinate system used.

Please note that most of the PDF functions require a pdfdoc as its first parameter. Please see the examples below for more information.

Note: If you're interested in alternative free PDF generators that do not utilize external PDF libraries, see this related FAQ.

Requirements

PDFlib is available for download at http://www.pdflib.com/products/pdflib/index.html, but requires that you purchase a license for commercial use. The JPEG and TIFF libraries are required to compile this extension.

Issues with older versions of PDFlib

Any version of PHP 4 after March 9, 2000 does not support versions of PDFlib older than 3.0.

PDFlib 3.0 or greater is supported by PHP 3.0.19 and later.

Installation

To get these functions to work, you have to compile PHP with --with-pdflib[=DIR]. DIR is the PDFlib base install directory, defaults to /usr/local. In addition you can specify the jpeg, tiff, and pnglibrary for PDFlib to use, which is optional for PDFlib 4.x. To do so add to your configure line the options --with-jpeg-dir[=DIR] --with-png-dir[=DIR] --with-tiff-dir[=DIR].

When using version 3.x of PDFlib, you should configure PDFlib with the option --enable-shared-pdflib.

Runtime Configuration

This extension has no configuration directives defined in php.ini.

Confusion with old PDFlib versions

Starting with PHP 4.0.5, the PHP extension for PDFlib is officially supported by PDFlib GmbH. This means that all the functions described in the PDFlib manual (V3.00 or greater) are supported by PHP 4 with exactly the same meaning and the same parameters. Only the return values may differ from the PDFlib manual, because the PHP convention of returning FALSE was adopted. For compatibility reasons, this binding for PDFlib still supports the old functions, but they should be replaced by their new versions. PDFlib GmbH will not support any problems arising from the use of these deprecated functions.

Table 1. Deprecated functions and their replacements

Old functionReplacement
pdf_put_image()Not needed anymore.
pdf_execute_image()Not needed anymore.
pdf_get_annotation()pdf_get_bookmark() using the same parameters.
pdf_get_font()pdf_get_value() passing "font" as the second parameter.
pdf_get_fontsize()pdf_get_value() passing "fontsize" as the second parameter.
pdf_get_fontname()pdf_get_parameter() passing "fontname" as the second parameter.
pdf_set_info_creator()pdf_set_info() passing "Creator" as the second parameter.
pdf_set_info_title()pdf_set_info() passing "Title" as the second parameter.
pdf_set_info_subject()pdf_set_info() passing "Subject" as the second parameter.
pdf_set_info_author()pdf_set_info() passing "Author" as the second parameter.
pdf_set_info_keywords()pdf_set_info() passing "Keywords" as the second parameter.
pdf_set_leading()pdf_set_value() passing "leading" as the second parameter.
pdf_set_text_rendering()pdf_set_value() passing "textrendering" as the second parameter.
pdf_set_text_rise()pdf_set_value() passing "textrise" as the second parameter.
pdf_set_horiz_scaling()pdf_set_value() passing "horizscaling" as the second parameter.
pdf_set_text_matrix()Not available anymore
pdf_set_char_spacing()pdf_set_value() passing "charspacing" as the second parameter.
pdf_set_word_spacing()pdf_set_value() passing "wordspacing" as the second parameter.
pdf_set_transition()pdf_set_parameter() passing "transition" as the second parameter.
pdf_open()pdf_new() plus an subsequent call of pdf_open_file()
pdf_set_font()pdf_findfont() plus an subsequent call of pdf_setfont()
pdf_set_duration()pdf_set_value() passing "duration" as the second parameter.
pdf_open_gif()pdf_open_image_file() passing "gif" as the second parameter.
pdf_open_jpeg()pdf_open_image_file() passing "jpeg" as the second parameter.
pdf_open_tiff()pdf_open_image_file() passing "tiff" as the second parameter.
pdf_open_png()pdf_open_image_file() passing "png" as the second parameter.
pdf_get_image_width()pdf_get_value() passing "imagewidth" as the second parameter and the image as the third parameter.
pdf_get_image_height()pdf_get_value() passing "imageheight" as the second parameter and the image as the third parameter.

Examples

Most of the functions are fairly easy to use. The most difficult part is probably creating your first PDF document. The following example should help to get you started. It creates test.pdf with one page. The page contains the text "Times Roman outlined" in an outlined, 30pt font. The text is also underlined.

Example 1. Creating a PDF document with PDFlib

<?php
$pdf = pdf_new();
pdf_open_file($pdf, "test.pdf");
pdf_set_info($pdf, "Author", "Uwe Steinmann");
pdf_set_info($pdf, "Title", "Test for PHP wrapper of PDFlib 2.0");
pdf_set_info($pdf, "Creator", "See Author");
pdf_set_info($pdf, "Subject", "Testing");
pdf_begin_page($pdf, 595, 842);
pdf_add_outline($pdf, "Page 1");
$font = pdf_findfont($pdf, "Times New Roman", "winansi", 1);
pdf_setfont($pdf, $font, 10);
pdf_set_value($pdf, "textrendering", 1);
pdf_show_xy($pdf, "Times Roman outlined", 50, 750);
pdf_moveto($pdf, 50, 740);
pdf_lineto($pdf, 330, 740);
pdf_stroke($pdf);
pdf_end_page($pdf);
pdf_close($pdf);
pdf_delete($pdf);
echo "<A HREF=getpdf.php>finished</A>";
?>
The script getpdf.php just returns the pdf document.

Example 2. Outputing a precalculated PDF

<?php
$len = filesize($filename);
header("Content-type: application/pdf");
header("Content-Length: $len");
header("Content-Disposition: inline; filename=foo.pdf");
readfile($filename);
?>

The PDFlib distribution contains a more complex example which creates a page with an analog clock. Here we use the in-memory creation feature of PDFlib to alleviate the need to use temporary files. The example was converted to PHP from the PDFlib example. (The same example is available in the CLibPDF documentation.)

Example 3. pdfclock example from PDFlib distribution

<?php
$radius = 200;
$margin = 20;
$pagecount = 10;

$pdf = pdf_new();

if (!pdf_open_file($pdf, "")) {
    print error;
    exit;
};

pdf_set_parameter($pdf, "warning", "true");

pdf_set_info($pdf, "Creator", "pdf_clock.php");
pdf_set_info($pdf, "Author", "Uwe Steinmann");
pdf_set_info($pdf, "Title", "Analog Clock");

while($pagecount-- > 0) {
    pdf_begin_page($pdf, 2 * ($radius + $margin), 2 * ($radius + $margin));

    pdf_set_parameter($pdf, "transition", "wipe");
    pdf_set_value($pdf, "duration", 0.5);
  
    pdf_translate($pdf, $radius + $margin, $radius + $margin);
    pdf_save($pdf);
    pdf_setrgbcolor($pdf, 0.0, 0.0, 1.0);

    /* minute strokes */
    pdf_setlinewidth($pdf, 2.0);
    for ($alpha = 0; $alpha < 360; $alpha += 6) {
        pdf_rotate($pdf, 6.0);
        pdf_moveto($pdf, $radius, 0.0);
        pdf_lineto($pdf, $radius-$margin/3, 0.0);
        pdf_stroke($pdf);
    }

    pdf_restore($pdf);
    pdf_save($pdf);

    /* 5 minute strokes */
    pdf_setlinewidth($pdf, 3.0);
    for ($alpha = 0; $alpha < 360; $alpha += 30) { 
        pdf_rotate($pdf, 30.0);
        pdf_moveto($pdf, $radius, 0.0);
        pdf_lineto($pdf, $radius-$margin, 0.0);
        pdf_stroke($pdf);
    }

    $ltime = getdate();

    /* draw hour hand */
    pdf_save($pdf);
    pdf_rotate($pdf,-(($ltime['minutes']/60.0)+$ltime['hours']-3.0)*30.0);
    pdf_moveto($pdf, -$radius/10, -$radius/20);
    pdf_lineto($pdf, $radius/2, 0.0);
    pdf_lineto($pdf, -$radius/10, $radius/20);
    pdf_closepath($pdf);
    pdf_fill($pdf);
    pdf_restore($pdf);

    /* draw minute hand */
    pdf_save($pdf);
    pdf_rotate($pdf,-(($ltime['seconds']/60.0)+$ltime['minutes']-15.0)*6.0);
    pdf_moveto($pdf, -$radius/10, -$radius/20);
    pdf_lineto($pdf, $radius * 0.8, 0.0);
    pdf_lineto($pdf, -$radius/10, $radius/20);
    pdf_closepath($pdf);
    pdf_fill($pdf);
    pdf_restore($pdf);

    /* draw second hand */
    pdf_setrgbcolor($pdf, 1.0, 0.0, 0.0);
    pdf_setlinewidth($pdf, 2);
    pdf_save($pdf);
    pdf_rotate($pdf, -(($ltime['seconds'] - 15.0) * 6.0));
    pdf_moveto($pdf, -$radius/5, 0.0);
    pdf_lineto($pdf, $radius, 0.0);
    pdf_stroke($pdf);
    pdf_restore($pdf);

    /* draw little circle at center */
    pdf_circle($pdf, 0, 0, $radius/30);
    pdf_fill($pdf);

    pdf_restore($pdf);

    pdf_end_page($pdf);

    # to see some difference
    sleep(1);
}

pdf_close($pdf);

$buf = pdf_get_buffer($pdf);
$len = strlen($buf);

header("Content-type: application/pdf");
header("Content-Length: $len");
header("Content-Disposition: inline; filename=foo.pdf");
print $buf;

pdf_delete($pdf);
?>

See Also

Note: An alternative PHP module for PDF document creation based on FastIO's ClibPDF is available. Please see the ClibPDF section for details. Note that ClibPDF has a slightly different API than PDFlib.

Table of Contents
pdf_add_annotation -- Deprecated: Adds annotation
pdf_add_bookmark -- Adds bookmark for current page
pdf_add_launchlink -- Add a launch annotation for current page
pdf_add_locallink -- Add a link annotation for current page
pdf_add_note -- Sets annotation for current page
pdf_add_outline -- Deprecated: Adds bookmark for current page
pdf_add_pdflink -- Adds file link annotation for current page
pdf_add_thumbnail -- Adds thumbnail for current page
pdf_add_weblink -- Adds weblink for current page
pdf_arc -- Draws an arc (counterclockwise)
pdf_arcn -- Draws an arc (clockwise)
pdf_attach_file -- Adds a file attachment for current page
pdf_begin_page -- Starts new page
pdf_begin_pattern -- Starts new pattern
pdf_begin_template -- Starts new template
pdf_circle -- Draws a circle
pdf_clip -- Clips to current path
pdf_close_image -- Closes an image
pdf_close_pdi_page --  Close the page handle
pdf_close_pdi --  Close the input PDF document
pdf_close -- Closes a pdf resource
pdf_closepath_fill_stroke -- Closes, fills and strokes current path
pdf_closepath_stroke -- Closes path and draws line along path
pdf_closepath -- Closes path
pdf_concat -- Concatenate a matrix to the CTM
pdf_continue_text -- Outputs text in next line
pdf_curveto -- Draws a curve
pdf_delete -- Deletes a PDF object
pdf_end_page -- Ends a page
pdf_end_pattern -- Finish pattern
pdf_end_template -- Finish template
pdf_endpath -- Deprecated: Ends current path
pdf_fill_stroke -- Fills and strokes current path
pdf_fill -- Fills current path
pdf_findfont -- Prepare font for later use with pdf_setfont().
pdf_get_buffer -- Fetch the buffer containig the generated PDF data.
pdf_get_font -- Deprecated: font handling
pdf_get_fontname -- Deprecated: font handling
pdf_get_fontsize -- Deprecated: font handling
pdf_get_image_height -- Deprecated: returns height of an image
pdf_get_image_width -- Deprecated: Returns width of an image
pdf_get_majorversion --  Returns the major version number of the PDFlib
pdf_get_minorversion --  Returns the minor version number of the PDFlib
pdf_get_parameter -- Gets certain parameters
pdf_get_pdi_parameter -- Get some PDI string parameters
pdf_get_pdi_value -- Gets some PDI numerical parameters
pdf_get_value -- Gets certain numerical value
pdf_initgraphics -- Resets graphic state
pdf_lineto -- Draws a line
pdf_makespotcolor -- Makes a spotcolor
pdf_moveto -- Sets current point
pdf_new -- Creates a new pdf resource
pdf_open_CCITT -- Opens a new image file with raw CCITT data
pdf_open_file -- Opens a new pdf object
pdf_open_gif -- Deprecated: Opens a GIF image
pdf_open_image_file -- Reads an image from a file
pdf_open_image -- Versatile function for images
pdf_open_jpeg -- Deprecated: Opens a JPEG image
pdf_open_memory_image -- Opens an image created with PHP's image functions
pdf_open_pdi_page --  Prepare a page
pdf_open_pdi --  Opens a PDF file
pdf_open_png --  Deprecated: Opens a PNG image
pdf_open_tiff -- Deprecated: Opens a TIFF image
pdf_open -- Deprecated: Open a new pdf object
pdf_place_image -- Places an image on the page
pdf_place_pdi_page -- Places an image on the page
pdf_rect -- Draws a rectangle
pdf_restore -- Restores formerly saved environment
pdf_rotate -- Sets rotation
pdf_save -- Saves the current environment
pdf_scale -- Sets scaling
pdf_set_border_color -- Sets color of border around links and annotations
pdf_set_border_dash -- Sets dash style of border around links and annotations
pdf_set_border_style -- Sets style of border around links and annotations
pdf_set_char_spacing -- Deprecated: Sets character spacing
pdf_set_duration -- Deprecated: Sets duration between pages
pdf_set_font -- Deprecated: Selects a font face and size
pdf_set_horiz_scaling -- Sets horizontal scaling of text
pdf_set_info_author --  Deprecated: Fills the author field of the document
pdf_set_info_creator --  Deprecated: Fills the creator field of the document
pdf_set_info_keywords --  Deprecated: Fills the keywords field of the document
pdf_set_info_subject --  Deprecated: Fills the subject field of the document
pdf_set_info_title --  Deprecated: Fills the title field of the document
pdf_set_info -- Fills a field of the document information
pdf_set_leading -- Deprecated: Sets distance between text lines
pdf_set_parameter -- Sets certain parameters
pdf_set_text_matrix -- Deprecated: Sets the text matrix
pdf_set_text_pos -- Sets text position
pdf_set_text_rendering -- Deprecated: Determines how text is rendered
pdf_set_text_rise -- Deprecated: Sets the text rise
pdf_set_value -- Sets certain numerical value
pdf_set_word_spacing -- Deprecated: Sets spacing between words
pdf_setcolor -- Sets fill and stroke color
pdf_setdash -- Sets dash pattern
pdf_setflat -- Sets flatness
pdf_setfont -- Set the current font
pdf_setgray_fill -- Sets filling color to gray value
pdf_setgray_stroke -- Sets drawing color to gray value
pdf_setgray -- Sets drawing and filling color to gray value
pdf_setlinecap -- Sets linecap parameter
pdf_setlinejoin -- Sets linejoin parameter
pdf_setlinewidth -- Sets line width
pdf_setmatrix -- Sets current transformation matrix
pdf_setmiterlimit -- Sets miter limit
pdf_setpolydash -- Deprecated: Sets complicated dash pattern
pdf_setrgbcolor_fill -- Sets filling color to rgb color value
pdf_setrgbcolor_stroke -- Sets drawing color to rgb color value
pdf_setrgbcolor -- Sets drawing and filling color to rgb color value
pdf_show_boxed -- Output text in a box
pdf_show_xy -- Output text at given position
pdf_show -- Output text at current position
pdf_skew -- Skews the coordinate system
pdf_stringwidth -- Returns width of text using current font
pdf_stroke -- Draws line along path
pdf_translate -- Sets origin of coordinate system


add a note add a note User Contributed Notes
PDF functions
SenorTZ senortz at nospam dot yahoo dot com
28-Jul-2003 08:23
About creating a PDF document based on the content of another document(let's say a text file):

I have tried to send to the PDF-creator page from a link from the sender page the file name of the file I want to read the content from and generate the PDF document containing this content. The idea is is that when I tried to reffer the pdf-creator page via the link your_root/create_pdf.php?filename=$your_file_name, the pdf-creator page does not behave well when before creating the pdf document I have a line like $filename = $_GET["filename"].
I solved this using on the sender page instead of the link a form with a button, so the form has as action "create_pdf.php", as method "post" and a hidden field containing the "filename" value. And it works like this if, on the pdf-creator page I have a line like $filename = $_POST["filename"].

I would like to understand why this way it works and the other way does not.

I hope this helps. Here are the pieces of code I used.

Sender page:
print("<form name='to_pdf' action='see_pdf_file.php' method='post'>");
print("<br/><input type='submit' value='PDF'><input type='hidden' name='filename' value='$filename'></form>");

PDF-creator page:
<?
$filename 
$_POST["filename"];
$file_handle fopen($filename"r");
$file_content file_get_contents($filename);
fclose($file_handle);
//
$file_content wordwrap($file_content,72,"|");
$a_row explode("|",$file_content);
$i 0;
//
$pdf pdf_new();
pdf_open_file($pdf"");
pdf_begin_page($pdf595842);
pdf_set_font($pdf"Times-Roman"16"host");
pdf_add_outline($pdf"Page 1");
pdf_set_value($pdf"textrendering"1);
pdf_show_xy($pdf'The content of the file:',50,700);
while (
$a_row[$i] != "")
{
       
pdf_continue_text($pdf,$a_row[$i]);
       
$i++;
}
pdf_end_page($pdf);
pdf_close($pdf);
//
$data pdf_get_buffer($pdf);
//
header("Content-type: application/pdf");
header("Content-disposition: inline; filename=test.pdf");
header("Content-length: " strlen($data));
//
echo $data;
?>

PDFLib and PHP 431 used.

Thanks.
bmironov at jonview dot com
25-Jun-2003 05:46
RedHat 9 + Apache 2.0 + PHP 4.3.2 + Oracle 9i + PDFlib 5.0.1 (binary distribution)

It seems to be a working bundle if you do some magic with ./configure:

RedHat 9:
kernel-2.4.20-18.9

Apache 2.0.46:
./configure --enable-so --enable-rewrite=shared --enable-status --enable-mpm=prefork

PHP 4.3.2:
./configure \
--program-prefix= \
--prefix=/usr \
--exec-prefix=/usr \
--bindir=/usr/bin \
--sbindir=/usr/sbin \
--sysconfdir=/etc \
--datadir=/usr/share \
--includedir=/usr/include \
--libdir=/usr/lib \
--libexecdir=/usr/libexec \
--localstatedir=/var \
--sharedstatedir=/usr/com \
--mandir=/usr/share/man \
--infodir=/usr/share/info \
--with-config-file-path=/etc \
--with-config-file-scan-dir=/etc/php.d \
--without-tsrm-pthreads \    # !!!!!!!!!!!!!!!!!!!!
--with-zlib \
--with-gd \
--enable-gd-native-ttf \
--with-ttf \
--without-mysql \
--with-apxs2filter=/usr/local/apache2/bin/apxs \
--with-oci8 \
--enable-sigchild \
--enable-inline-optimization

Oracle9i:
ln -s $ORACLE_HOME/rdbms/public/nzerror.h $ORACLE_HOME/rdbms/demo/nzerror.h

ln -s $ORACLE_HOME/rdbms/public/nzt.h $ORACLE_HOME/rdbms/demo/nzt.h

ln -s $ORACLE_HOME/rdbms/public/ociextp.h $ORACLE_HOME/rdbms/demo/ociextp.h

If you want to use bundled GD-library then:
1) install following packages: libjpeg, libjpeg-devel, libpng, libpng-devel, freetype, freetype-devel, libtiff, libtiff-devel, zlib, zlib-devel

2) ln -s /usr/lib/libjpeg.so.62 /usr/lib/libjpeg.so
ln -s /usr/lib/libpng.so.62 /usr/lib/libpng.so

It seems to be a working combination, because it is NOT give you:
1) error message in Apache's error_log:
Module compiled with module API=20020429, debug=0, thread-safety=0
PHP compiled with module API=20020429, debug=0, thread-safety=1

2) error message in Apache's error_log:
[notice] child pid 12345 exit signal Segmentation fault (11)

3) MS Internet Explorer can show PDF-output from your PHP-script via Acrobat plug-in and does not crush. No confusing messages about opening "Adobe Acrobat Control for ActiveX".

Hope it will save you some time.

Good luck,
Boris
frederic dot surleau at atosorigin dot com
18-Sep-2002 04:12
If you use session, all headers you can send about "cache" are too late !

See http://bugs.php.net/?id=19450&edit=2

The solution is something like this :

<?php
// Default is "no-cache"
session_cache_limiter('private');

session_start();

$pdf pdf_new();

/* your code here */

pdf_close($pdf);

$buf pdf_get_buffer($pdf);
$len strlen($buf);

header("Content-type: application/pdf");
header("Content-Length: $len");
header("Content-Disposition: inline; filename=foo.pdf");
print 
$buf;
pdf_delete($pdf);
?>
matt at nospam dot org
30-Aug-2002 01:11
Adding to my prior note, IE 6 has a strange feature of using GET when refreshing a pdf document, even though the page was originally POSTed to.  This may be the root cause of all the trouble listed above regarding posting and pdf. 

So, I recommend:
1) using a two page form/action handler when doing pdf rendering instead of the standard $PHP_SELF form/self handler to resolve the problem discussed above
2) Using either GET, or a self posting form that sets cookies and then redirects to the pdf creation page instead of POST, so that the parms get to the page.  HTH
gilbertng at hongkong dot com
11-Jun-2002 05:23
Hope it can help someone:

    $pdf = pdf_new();
    //pdf_open_file($pdf,"");
    if (!pdf_open_file($pdf, "")) {
            print error;
            exit;   
    }
   

             PDF_set_parameter($pdf, "resourcefile", "/usr/local/pdflib/fonts/pdflib.upr");
    PDF_set_parameter($pdf,"prefix","/usr/local/pdflib/fonts");

    pdf_begin_page($pdf, 595, 842);
    pdf_add_outline($pdf, "Page 1");

    //pdf_set_font($pdf, "Times-Roman", 30, "host");
             // set chinese characters,
    $font = pdf_findfont($pdf, "MHei-Medium", "B5pc-H",0);
    if ($font) {
        pdf_setfont($pdf, $font, 30);
    }   

    pdf_set_value($pdf, "textrendering",0);
    pdf_show_xy($pdf, "中文 100 Roman outlined", 50, 750);

    pdf_set_font($pdf, "Times-Roman", 30, "host");
    pdf_show_xy($pdf, "中文 Times Roman outlined", 50, 600);
    pdf_moveto($pdf, 50, 740);
    pdf_lineto($pdf, 330, 740);
    pdf_stroke($pdf);
    pdf_end_page($pdf);
    pdf_close($pdf);

    $buf = pdf_get_buffer($pdf);
    $len = strlen($buf);

    header("Content-type: application/pdf");
    header("Content-Length: $len");
    header("Content-Disposition: inline; filename=foo.pdf");
    print $buf;

    pdf_delete($pdf);
chernyshevsky at hotmail dot com
06-May-2002 05:22
If you're wondering how to highlight words inside a PDF file, take a look at this script I've written (doesn't need PDFLib)

http://zeus.jtlnet.com/~conradis/pdfhi.php.txt

It's a whole lot harder than you think. (Rarely has no much code been written that does so little, that's what I say :-) Worth looking at if you want to do searches inside a PDF.
pbierans at lynet dot de
28-Mar-2002 01:56
Load extension, open a PDF, add a font, modify PDF in memory and send
it to browser:

<?php
 
// no cache headers:
 
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
 
header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
 
header("Cache-Control: no-store, no-cache, must-revalidate");
 
header("Cache-Control: post-check=0, pre-check=0"false);
 
header("Pragma: no-cache");

 
$ext_name="libpdf_php.so";
   
// libpdf_php.so is the PDFLIB for SunOS by "PDFlib GmbH"
    // visit http://www.pdflib.com

  // if the extension is not automatically loaded by Apache
  // dl() will try to load it on demand:
 
if (!extension_loaded($ext_name) && !@dl($ext_name))
  {
   
?>
    <table width="100%" border="0"><tr><td align="center">
      <table style="border: solid #f0f0f0 2px;"><tr>
        <td valign="middle" style="padding: 20px; margin: 0px;">
          <p style="font-family: arial; font-size: 12px; ">
          <b>Sorry,</b><br>
          &nbsp;<br>
          A PDF can not be generated right now.<br>
          The administrator has been informed and will fix this as
          soon as possible.<br>
          Please try again later.
        </p>
      </td></tr></table>
    </td></tr></table>
    <?php
    mail
('admin@domain.com','Error: PDFLib not found',
         
'Called by script:\n  '.$SCRIPT_FILENAME.'?'.$QUERY_STRING,
         
"From: warnings@domain.com\n");
    exit;
  } 
// verify that extension is usable

  // unique serial number:
 
srand(microtime()*10000);
 
$usnrgmdate("Ymd-His-").rand(1000,9999).'-';
 
$pdf_file=$usnr.'result.pdf';
 
$src_file='source.pdf';

 
// create pdf object
 
$pdf pdf_new();
 
pdf_open_file($pdf);
 
pdf_set_parameter($pdf'serial',      'if-you-have-one');

 
// fonts to embed, they are in the folder of this file:
 
pdf_set_parameter($pdf'FontAFM',     'TradeGothic=Tg______.afm');
 
pdf_set_parameter($pdf'FontOutline''TradeGothic=Tg______.pfb');
 
pdf_set_parameter($pdf'FontPFM',     'TradeGothic=Tg______.pfm');

 
// load the source file:
 
$src_doc   =pdf_open_pdi($pdf,$src_file,''0);
 
$src_page  =pdf_open_pdi_page($pdf,$src_doc,1,'');
 
$src_width =pdf_get_pdi_value($pdf,'width' ,$src_doc,$src_page,0);
 
$src_height=pdf_get_pdi_value($pdf,'height',$src_doc,$src_page,0);

 
pdf_begin_page($pdf$src_width$src_height);
  {
   
// place the sourcefile to the background of the actual page:
   
pdf_place_pdi_page($pdf,$src_page,0,0,1,1);
   
pdf_close_pdi_page($pdf,$src_page);

   
// modify the page:
   
pdf_set_font($pdf'TradeGothic'8'host');
   
pdf_show_xy($pdf'Now: '.gmdate("Y-m-d H:i:s"),50,50);
  }
 
pdf_end_page($pdf);
 
pdf_close($pdf);

 
// prepare output:
 
$pdfdata pdf_get_buffer($pdf); // to echo the pdf-data
 
$pdfsize strlen($pdfdata);     // IE requires the datasize

  // real datatype headers:
 
header('Content-type: application/pdf');
 
header('Content-disposition: attachment; filename="'.$pdf_file.'"');
 
header('Content-length: '.$pdfsize);
  echo 
$pdfdata;
  exit; 
// keep this one so no #13#10 or #32 will be written
?>
a dot marchand dot nospam at home dot com
02-May-2001 02:42
To continue on the internet explorer (Iexplorer, IE) requirements, instead of content-length, a simple:
header("Accept-Ranges: bytes");

is enough for the getpdf.php file working right. Even Netscape will without error with this modification.

Aurelien
c dot hue at sofrel dot com
15-Sep-2000 05:52
For PdfLib.dll v2.01 look http://www.mm4.de

<overloadpdf_add_annotation>
 Last updated: Thu, 21 Aug 2003
show source | credits | sitemap | mirror sites 
Copyright © 2001-2003 The PHP Group
All rights reserved.
This mirror generously provided by: http://php.mirrors.ilisys.com.au/
Last updated: Sat 01 Nov 2003 04:13:36 EST EST