PDF functions Podręcznik PHPPoprzedniNastępnyLXIV. PDF functions Introduction
The PDF functions in PHP can create PDF files using the PDFlib
library created by Thomas
Merz. PDFlib is available for download at http://www.pdflib.com/pdflib/index.html, but requires that you purchase
a license for commercial use. The JPEG and TIFF libraries are required to compile
this extension. Please see the PDFlib installation
section for more information about compiling PDF support into PHP.
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
pdf object as it's first parameter. Please
see the examples below for more information.
Notatka:
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
compared to PDFlib.
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.
Tabela 1. Deprecated functions and its replacementsOld functionReplacementpdf_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 anymorepdf_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.
PDFlib 3.x Installation Hints
When using version 3.x of PDFlib, you should configure PDFlib
with the option --enable-shared-pdflib.
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.
Examples
Most of the functions are fairly easy to use. The most difficult part
is probably creating a very simple PDF document at all. The following
example should help to get 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.
Przykład 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");
pdf_set_font($pdf, "Times-Roman", 30, "host");
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.
<?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, converted to PHP from the PDFlib example, is
as follows: (The same example is available in the CLibPDF documentation.)
Przykład 2. 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);
?>
Spis treścipdf_add_annotation -- Deprecated: Adds annotationpdf_add_bookmark -- Adds bookmark for current pagepdf_add_launchlink -- Add a launch annotation for current pagepdf_add_locallink -- Add a link annotation for current pagepdf_add_note -- Add a note annotation for current pagepdf_add_outline -- Deprecated: Adds bookmark for current pagepdf_add_pdflink -- Adds file link annotation for current pagepdf_add_thumbnail -- Adds thumbnail for current pagepdf_add_weblink -- Adds weblink for current pagepdf_arc -- Draws an arc (counterclockwise)pdf_arcn -- Draws an arc (clockwise)pdf_attach_file -- Adds a file attachement for current pagepdf_begin_page -- Starts new pagepdf_begin_pattern -- Starts new patternpdf_begin_template -- Starts new templatepdf_circle -- Draws a circlepdf_clip -- Clips to current pathpdf_close -- Closes a pdf objectpdf_closepath -- Closes pathpdf_closepath_fill_stroke -- Closes, fills and strokes current pathpdf_closepath_stroke -- Closes path and draws line along pathpdf_close_image -- Closes an imagepdf_close_pdi --
Close the input PDF document
pdf_close_pdi_page --
Close the page handle
pdf_concat -- Concatenate a matrix to the CTMpdf_continue_text -- Outputs text in next linepdf_curveto -- Draws a curvepdf_delete -- Deletes a PDF objectpdf_end_page -- Ends a pagepdf_endpath -- Deprecated: Ends current pathpdf_end_pattern -- Finish patternpdf_end_template -- Finish templatepdf_fill -- Fills current pathpdf_fill_stroke -- Fills and strokes current pathpdf_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 handlingpdf_get_fontname -- Deprecated: font handlingpdf_get_fontsize -- Deprecated: font handlingpdf_get_image_height -- Returns height of an imagepdf_get_image_width -- Returns width of an imagepdf_get_parameter -- Gets certain parameterspdf_get_pdi_parameter -- Get some PDI string parameterspdf_get_pdi_value -- Gets some PDI numerical parameterspdf_get_majorversion --
Returns the major version number of the PDFlib
pdf_get_minorversion --
Returns the minor version number of the PDFlib
pdf_get_value -- Gets certain numerical valuepdf_initgraphics -- Resets graphic statepdf_lineto -- Draws a linepdf_makespotcolor -- Makes a spotcolorpdf_moveto -- Sets current pointpdf_new -- Creates a new pdf objectpdf_open -- Deprecated: Open a new pdf objectpdf_open_CCITT -- Opens a new image file with raw CCITT datapdf_open_file -- Opens a new pdf objectpdf_open_gif -- Deprecated: Opens a GIF imagepdf_open_image -- Versatile function for imagespdf_open_image_file -- Reads an image from a filepdf_open_jpeg -- Deprecated: Opens a JPEG imagepdf_open_memory_image -- Opens an image created with PHP's image functionspdf_open_pdi --
Opens a PDF file
pdf_open_pdi_page --
Prepare a page
pdf_open_png --
Deprecated: Opens a PNG image
pdf_open_tiff -- Deprecated: Opens a TIFF imagepdf_place_image -- Places an image on the pagepdf_place_pdi_page -- Places an image on the pagepdf_rect -- Draws a rectanglepdf_restore -- Restores formerly saved environmentpdf_rotate -- Sets rotationpdf_save -- Saves the current environmentpdf_scale -- Sets scalingpdf_setcolor -- Sets fill and stroke colorpdf_setdash -- Sets dash patternpdf_setflat -- Sets flatnesspdf_setfont -- Set the current fontpdf_setgray -- Sets drawing and filling color to gray valuepdf_setgray_fill -- Sets filling color to gray valuepdf_setgray_stroke -- Sets drawing color to gray valuepdf_setlinecap -- Sets linecap parameterpdf_setlinejoin -- Sets linejoin parameterpdf_setlinewidth -- Sets line widthpdf_setmatrix -- Sets current transformation matrixpdf_setmiterlimit -- Sets miter limitpdf_setpolydash -- Sets complicated dash patternpdf_setrgbcolor -- Sets drawing and filling color to rgb color valuepdf_setrgbcolor_fill -- Sets filling color to rgb color valuepdf_setrgbcolor_stroke -- Sets drawing color to rgb color valuepdf_set_border_color -- Sets color of border around links and annotationspdf_set_border_dash -- Sets dash style of border around links and annotationspdf_set_border_style -- Sets style of border around links and annotationspdf_set_char_spacing -- Deprecated: Sets character spacingpdf_set_duration -- Deprecated: Sets duration between pagespdf_set_font -- Deprecated: Selects a font face and sizepdf_set_horiz_scaling -- Sets horizontal scaling of textpdf_set_info -- Fills a field of the document informationpdf_set_info_author --
Fills the author field of the document
pdf_set_info_creator --
Fills the creator field of the document
pdf_set_info_keywords --
Fills the keywords field of the document
pdf_set_info_subject --
Fills the subject field of the document
pdf_set_info_title --
Fills the title field of the document
pdf_set_leading -- Deprecated: Sets distance between text linespdf_set_parameter -- Sets certain parameterspdf_set_text_pos -- Sets text positionpdf_set_text_rendering -- Deprecated: Determines how text is renderedpdf_set_text_rise -- Deprecated: Sets the text risepdf_set_text_matrix -- Deprecated: Sets the text matrixpdf_set_value -- Sets certain numerical valuepdf_set_word_spacing -- Depriciated: Sets spacing between wordspdf_show -- Output text at current positionpdf_show_boxed -- Output text in a boxpdf_show_xy -- Output text at given positionpdf_skew -- Skews the coordinate systempdf_stringwidth -- Returns width of text using current fontpdf_stroke -- Draws line along pathpdf_translate -- Sets origin of coordinate systemPoprzedniSpis treściNastępnyoverloadPoczątek rozdziałupdf_add_annotation