LXXIV. PDF funkce

Introduction

Pokud mßte PDF knihovnu od Thomase Merze (dostupnß z http://www.pdflib.com/pdflib/index.html), m∙╛ete pou╛φvat PDF funkce na tvorbu PDF soubor∙; ke kompilaci budete pot°ebovat takΘ JPEG knihovnu a TIFF knihovnu. Tyto dv∞ knihovny pom∞rn∞ Φasto d∞lajφ potφ╛e p°i konfiguraci PHP. P°i °e╣enφ p°φpadn²ch problΘm∙ se °i∩te chybov²mi zprßvami configure skriptu.

V∞nujte prosφm pozornost v²bornΘ dokumentaci pdflib, kterß je souΦßstφ distribuce zdrojovΘho k≤du. Poskytuje velmi dobr² p°ehled schopnostφ pdflib. V∞t╣ina funkcφ pdflib a p°φslu╣nΘho PHP modulu mß stejnΘ jmΘno. Argumenty jsou takΘ identickΘ. Pokud chcete tento modul vyu╛φvat opravdu efektivn∞, m∞li byste chßpat takΘ n∞kterΘ z koncept∙ PDF nebo Postscriptu. V╣echny rozm∞ry a koordinßty se udßvajφ v Postscriptov²ch bodech. Obecn∞ je 72 PostScriptov²ch bod∙ na palec, ale zßvisφ to na v²stupnφm rozli╣enφ.

Existuje dal╣φ PHP modul na tvorbu PDF dokument∙, zalo╛en² na ClibPDF od firmy FastIO. Mß mφrn∞ jinou API. Detaily viz ClibPDF funkce.

Tento PDF modul zavßdφ nov² typ prom∞nnΘ. Naz²vß se pdfdoc. pdfdoc je pointer na PDF dokument a tΘm∞° v╣echny funkce ho vy╛adujφ jako sv∙j prvnφ argument.

Zmatek se star²mi verzemi pdflib

Od ·plnΘho zaΦßtky podpory PDF v PHP — od pdflib 0.6 — do╣lo k mnoha zm∞nßm zvlß╣t∞ v API pdflib. V∞t╣inu t∞chto zm∞n PHP n∞jak zakrylo, n∞kterΘ vy╛adovaly zm∞nu PHP API. Od pdflib 3.x se API snad stabilizovala, a PHP 4 p°ijala tuto verzi jako mimimßlnφ pro podporu PDF. Nßsledkem toho mnoho funkcφ d°φve Φi pozd∞ji zmizφ nebo bude nahra╛eno alternativami. Podpora pdflib 0.6 u╛ byla naprosto ukonΦena. Nßsledujφcφ tabulka vyjmenovßvß v╣echny funkce, kterΘ jsou od PHP 4.0.2 zastaralΘ a m∞ly by b²t nahra╛eny jejich nov∞j╣φmi verzemi.

Tabulka 1. ZastaralΘ funkce a jejich nßhrady

Starß funkceNßhrada
pdf_put_image()Nenφ pot°eba.
pdf_get_font()pdf_get_value() s "font" jako druh² argument.
pdf_get_fontsize()pdf_get_value() s "fontsize" jako druh² argument.
pdf_get_fontname()pdf_get_parameter() s "fontname" jako druh² argument.
pdf_set_info_creator()pdf_set_info() s "Creator" jako druh² argument.
pdf_set_info_title()pdf_set_info() s "Title" jako druh² argument.
pdf_set_info_subject()pdf_set_info() s "Subject" jako druh² argument.
pdf_set_info_author()pdf_set_info() s "Author" jako druh² argument.
pdf_set_info_keywords()pdf_set_info() s "Keywords" jako druh² argument.
pdf_set_leading()pdf_set_value() s "leading" jako druh² argument.
pdf_set_text_rendering()pdf_set_value() s "textrendering" jako druh² argument.
pdf_set_text_rise()pdf_set_value() s "textrise" jako druh² argument.
pdf_set_horiz_scaling()pdf_set_value() s "horizscaling" jako druh² argument.
pdf_set_text_matrix()neexistuje
pdf_set_char_spacing()pdf_set_value() s "charspacing" jako druh² argument.
pdf_set_word_spacing()pdf_set_value() s "wordspacing" jako druh² argument.
pdf_set_transition()pdf_set_parameter() s "transition" jako druh² argument.
pdf_set_duration()pdf_set_value() s "duration" jako druh² argument.
pdf_open_gif()pdf_open_image_file() s "gif" jako druh² argument.
pdf_open_jpeg()pdf_open_image_file() s "jpeg" jako druh² argument.
pdf_open_tiff()pdf_open_image_file() s "tiff" jako druh² argument.
pdf_open_png()pdf_open_image_file() s "png" jako druh² argument.
pdf_get_imagewidth()pdf_get_value() s "imagewidth" jako druh² argument a obrßzkem jako t°etφ argument.
pdf_get_imageheight()pdf_get_value() s "imageheight" jako druh² argument a obrßzkem jako t°etφ argument.
()()

Rady pro instalaci pdflib 3.x

Od pdflib 3.0 by se pdflib m∞la konfigurovat s volbou --enable-shared-pdflib.

ProblΘmy se star╣φmi verzemi pdflib

Pokud pou╛φvßte pdflib 2.01, zkontrolujte, jak je tato knihovna nainstalovßna. M∞li byste mφt soubor libpdf.so, nebo link na n∞j. Verze 2.01 vytvß°φ soubor libpdf2.01.so, kter² se nedß najφt p°i linkovßnφ testovacφho souboru v configure. Budete muset vytvo°it symbolick² link z libpdf.so na libpdf2.01.so.

Ve verzi 2.20 p°ibyly dal╣φ zm∞ny v API pdflib a podpora Φφnsk²ch a japonsk²ch font∙. Pokud pou╛φvßte pdflib 2.20 bu∩te opatrnφ p°i generovßnφ PDF dokument∙ v pam∞ti. Do verze pdflib 3.0 by mohlo b²t nestabilnφ. Argument k≤dovßnφ v pdf_set_font() se zm∞nil na °et∞zec. To znamenß, ╛e mφsto nap°. 4 musφte pou╛φt 'winansi'.

Pokud pou╛φvßte pdflib 2.30, nemßte k dispozici pdf_set_text_matrix(). P°estala b²t podporovßna. Obecnou radou je zjistit si p°φpadnΘ zm∞ny v release notes pou╛φvanΘ verze pdflib.

«ßdnß verze PHP 4 od data 9. b°ezna 2000 nepodporuje podflib star╣φ ne╛ 3.0. Na druhou stranu, PHP 3 by se nem∞lo pou╛φvat s nov∞j╣φ verzφ pdflib ne╛ 2.01.

Ukßzky

V∞t╣ina funkcφ se pou╛φvß docela snadno. Nejt∞╛╣φ je z°ejm∞ v∙bec n∞jak² jednoduch² PDF dokument v∙bec vytvo°it. Nßsledujφcφ ukßzka by m∞la pomoci zaΦφt. Vytvo°φ soubor test.pdf s jednou strßnkou. Tato strßnka obsahuje text "Times Roman outlined" napsan² 30ti bodov²m obrysem. Text je takΘ podtr╛en².

P°φklad 1. Tvorba PDF dokumentu s pdflib

<?php
$fp = fopen("test.pdf", "w");
$pdf = pdf_open($fp);
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);
fclose($fp);
echo "<A HREF=getpdf.php>finished</A>";
?>

Skript getpdf.php pouze vrßtφ vytvo°en² pdf dokument.

<?php
$fp = fopen("test.pdf", "r");
header("Content-type: application/pdf");
fpassthru($fp);
fclose($fp);
?>

Distribuce pdflib obsahuje rozsßhlej╣φ ukßzku, kterß obsahuje sΘrii strßnek s analogov²mi hodinami. Tato ukßzka p°evedenß do PHP vypadß takto (stejnou ukßzku najdete v dokumentaci k clibpdf modulu):

P°φklad 2. pdfclock ukßzka z pdflib distribuce

<?php
$pdffilename = "clock.pdf";
$radius = 200;
$margin = 20;
$pagecount = 40;

$fp = fopen($pdffilename, "w");
$pdf = pdf_open($fp);
pdf_set_info($pdf, "Creator", "pdf_clock.php3");
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);
}

$pdf = pdf_close($pdf);
fclose($fp);
echo "<A HREF=getpdf.php?filename=".$pdffilename.">finished</A>";
?>

PHP skript getpdf.php pouze vrßtφ v²tvo°en² PDF dokument.

<?php
$fp = fopen($filename, "r");
header("Content-type: application/pdf");
fpassthru($fp);
fclose($fp);
?>

Obsah
pdf_add_annotation — Adds annotation
pdf_add_outline — Adds bookmark for current page
pdf_arc — Draws an arc
pdf_begin_page — ZaΦφt novou stranu
pdf_circle — Draws a circle
pdf_clip — Clips to current path
pdf_close — Zav°φt PDF dokument
pdf_close_image — Closes an image
pdf_closepath — Closes path
pdf_closepath_fill_stroke — Closes, fills and strokes current path
pdf_closepath_stroke — Closes path and draws line along path
pdf_continue_text — Outputs text in next line
pdf_curveto — Draws a curve
pdf_end_page — UkonΦit stranu
pdf_endpath — Ends current path
pdf_execute_image — Places a stored image on the page
pdf_fill — Fills current path
pdf_fill_stroke — Fills and strokes current path
pdf_get_image_height — Returns height of an image
pdf_get_image_width — Returns width of an image
pdf_get_parameter — Zjistit hodnotu parametru
pdf_get_value — Gets certain numerical value
pdf_lineto — Draws a line
pdf_moveto — Sets current point
pdf_open — Otev°φt nov² PDF dokument
pdf_open_gif — Opens a GIF image
pdf_open_image_file — Reads an image from a file
pdf_open_jpeg — Opens a JPEG image
pdf_open_memory_image — Opens an image created with PHP's image functions
pdf_open_png — Opens a PNG image
pdf_open_tiff — Opens a TIFF image
pdf_place_image — Places an image on the page
pdf_put_image — Stores an image in the PDF for later use
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 — Sets character spacing
pdf_set_duration — Sets duration between pages
pdf_set_font — UrΦit font a velikost
pdf_set_horiz_scaling — Sets horizontal scaling of text
pdf_set_info — Vyplnit polo╛ku informacφ o dokumentu
pdf_set_leading — Nastavit vzdßlenost mezi °ßdky
pdf_set_parameter — Nastavit urΦitΘ parametry
pdf_set_text_matrix — Sets the text matrix
pdf_set_text_pos — Sets text position
pdf_set_text_rendering — Determines how text is rendered
pdf_set_text_rise — Sets the text rise
pdf_set_transition — Sets transition between pages
pdf_set_value — Sets certain numerical value
pdf_set_word_spacing — Sets spacing between words
pdf_setdash — Sets dash pattern
pdf_setflat — Sets flatness
pdf_setgray — Sets drawing and filling color to gray value
pdf_setgray_fill — Sets filling color to gray value
pdf_setgray_stroke — Sets drawing color to gray value
pdf_setlinecap — Sets linecap parameter
pdf_setlinejoin — Sets linejoin parameter
pdf_setlinewidth — Sets line width
pdf_setmiterlimit — Sets miter limit
pdf_setrgbcolor — Sets drawing and filling color to rgb color value
pdf_setrgbcolor_fill — Sets filling color to rgb color value
pdf_setrgbcolor_stroke — Sets drawing color to rgb color value
pdf_show — Umφstit text na aktußlnφ pozici
pdf_show_boxed — Vytisknout text v rßmeΦku
pdf_show_xy — Vytisknout text na urΦenΘ pozici
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