PDF verwendet die selbe Geometrie wie PostScript. Sie beginnt an der linken unteren Ecke der Seite und wird in Punkten (1/72 Zoll) gemessen.
Die Seitengröße kann vom Seitenobjekt erhalten werden:
$width = $pdfPage->getWidth(); $height = $pdfPage->getHeight();
PDF bietet leistungsfähige Möglichkeiten für die Farbdarstellung. Die Zend_Pdf
Komponente unterstützt die Grauskala sowie RGB und CYMK Farbräume. Jede kann überall
verwendet werden, wo ein Zend_Pdf_Color
Objekt benötigt wird. Die
Zend_Pdf_Color_GrayScale
, Zend_Pdf_Color_RGB
und
Zend_Pdf_Color_CMYK
Klassen stellen folgende Funktionalitäten bereit:
// $grayLevel (Fließkommazahl). 0.0 (schwarz) - 1.0 (weiß) $color1 = new Zend_Pdf_Color_GrayScale($grayLevel); // $r, $g, $b (Fließkommazahlen). 0.0 (minimale Helligkeit) - 1.0 (maximale Helligkeit) $color2 = new Zend_Pdf_Color_RGB($r, $g, $b); // $c, $m, $y, $k (Fließkommazahlen). 0.0 (minimale Helligkeit) - 1.0 (maximale Helligkeit) $color3 = new Zend_Pdf_Color_CMYK($c, $m, $y, $k);
Alle Zeichenoperationen können im Kontext einer PDF Seite durchgeführt werden.
Die Zend_Pdf_Page
Klass stellt einen Satz von einfachen Formen bereit:
/** * Zeichne eine Linie von x1,y1 nach x2,y2. * * @param float $x1 * @param float $y1 * @param float $x2 * @param float $y2 */ public function drawLine($x1, $y1, $x2, $y2);
/** * Zeichne ein Rechteck. * * Füllarten: * Zend_Pdf_Const::SHAPEDRAW_FILLNSTROKE - fülle und strichele das Rechteck (Standard) * Zend_Pdf_Const::SHAPEDRAW_STROKE - strichele das Rechteck * Zend_Pdf_Const::SHAPEDRAW_FILL - fülle das Rechteck * * @param float $x1 * @param float $y1 * @param float $x2 * @param float $y2 * @param integer $fillType */ public function drawRectangle($x1, $y1, $x2, $y2, $fillType = Zend_Pdf_Const::SHAPEDRAW_FILLNSTROKE);
/** * Zeichne ein Polygon * * Wenn $fillType Zend_Pdf_Const::SHAPEDRAW_FILLNSTROKE oder Zend_Pdf_Const::SHAPEDRAW_FILL ist, * wird das Polygon automatisch geschlossen. * Für eine detaillierte Beschreibung dieser Methode schaue in eine PDF Dokumentation * (Kapitel 4.4.2 Path painting Operators, Filling) * * @param array $x - Array mit Floats (die X Koordinaten der Eckpunkte) * @param array $y - Array mit Floats (the Y Koordinaten der Eckpunkte) * @param integer $fillType * @param integer $fillMethod */ public function drawPolygon($x, $y, $fillType = Zend_Pdf_Const::SHAPEDRAW_FILLNSTROKE, $fillMethod = Zend_Pdf_Const::FILLMETHOD_NONZEROWINDING);
/** * Zeichne einen Kreis mit dem Mittelpunkt x, y dem Radius radius. * * Winkel werden im Bogenmaß angegeben * * Methoden Signaturen: * drawCircle($x, $y, $radius); * drawCircle($x, $y, $radius, $fillType); * drawCircle($x, $y, $radius, $startAngle, $endAngle); * drawCircle($x, $y, $radius, $startAngle, $endAngle, $fillType); * * * Es ist kein echter Kreis, weil PDF nur kubische Bezierkurven unterstützt. * Aber es ist eine sehr Annäherung. * Es unterscheidet sich von echten Kreisen maximal um 0.00026 Radien * (Bei PI/8, 3*PI/8, 5*PI/8, 7*PI/8, 9*PI/8, 11*PI/8, 13*PI/8 und 15*PI/8 Winkeln). * Bei 0, PI/4, PI/2, 3*PI/4, PI, 5*PI/4, 3*PI/2 und 7*PI/4 ist es exakt eine Tangente zu einem Kreis. * * @param float $x * @param float $y * @param float $radius * @param mixed $param4 * @param mixed $param5 * @param mixed $param6 */ public function drawCircle($x, $y, $radius, $param4 = null, $param5 = null, $param6 = null);
/** * Zeichne eine Ellipse innerhalb des angegebenen Rechtecks. * * Methoden Signaturen: * drawEllipse($x1, $y1, $x2, $y2); * drawEllipse($x1, $y1, $x2, $y2, $fillType); * drawEllipse($x1, $y1, $x2, $y2, $startAngle, $endAngle); * drawEllipse($x1, $y1, $x2, $y2, $startAngle, $endAngle, $fillType); * * Winkel werden im Bogenmaß angegeben * * @param float $x1 * @param float $y1 * @param float $x2 * @param float $y2 * @param mixed $param5 * @param mixed $param6 * @param mixed $param7 */ public function drawEllipse($x1, $y1, $x2, $y2, $param5 = null, $param6 = null, $param7 = null);
Auch alle Textoperationen können im Kontext einer PDF Seite durchgeführt werden.
/** * Zeichne eine Textzeile an einer bestimmten Position. * * @param string $text * @param float $x * @param float $y * @throws Zend_Pdf_Exception */ public function drawText($text, $x, $y );
Der aktuelle Zeichensatz und die aktuelle Schriftgröße werden für Textoperationen verwendet. Bitte beachte die ausführliche Beschreibung unten.
Die Zend_Pdf_Page::drawText()
Methode verwendet den aktuellen Zeichensatz,
der mit der Methode Zend_Pdf_Page::setFont()
festgelegt werden kann:
/** * Lege den aktuellen Zeichensatz fest. * * @param Zend_Pdf_Font $font * @param float $fontSize */ public function setFont(Zend_Pdf_Font $font, $fontSize);
PDF unterstützt Type1, TrueType, Type3 und zusammengesetzte Zeichensätze. Es gibt
14 Type1 Standardzeichensätze, die von PDF bereit gestellt werden. Die
Zend_Pdf Komponente unterstützt derzeit nur diese Standardzeichensätze. Auf diese kann
über die Klasse Zend_Pdf_Font_Standard
zurückgegriffen werden. Der konkrete
Zeichensatz wird in einem Parameter des Konstruktur festgelegt:
Beispiel 11.5. Erstelle einen Standardzeichensatz
<?php ... // Erstelle einen neuen Zeichensatz $font = new Zend_Pdf_Font_Standard(Zend_Pdf_Const::FONT_HELVETICA); // Wende Zeichensatz an $pdfPage->setFont($font, 36); ... ?>
Die Zeichensatzkonstanten für die 14 Standardzeichensätze sind innerhalb der
Zend_Pdf_Const
Klasse definiert:
Zend_Pdf_Const::FONT_TIMES_ROMAN
Zend_Pdf_Const::FONT_TIMES_BOLD
Zend_Pdf_Const::FONT_TIMES_ITALIC
Zend_Pdf_Const::FONT_TIMES_BOLDITALIC
Zend_Pdf_Const::FONT_HELVETICA
Zend_Pdf_Const::FONT_HELVETICA_BOLD
Zend_Pdf_Const::FONT_HELVETICA_ITALIC
Zend_Pdf_Const::FONT_HELVETICA_BOLDITALIC
Zend_Pdf_Const::FONT_COURIER
Zend_Pdf_Const::FONT_COURIER_BOLD
Zend_Pdf_Const::FONT_COURIER_ITALIC
Zend_Pdf_Const::FONT_COURIER_BOLDITALIC
Zend_Pdf_Const::FONT_SYMBOL
Zend_Pdf_Const::FONT_ZAPFDINGBATS
Die Zend_Pdf_Page
Klasse stellt die drawImage() Methode für das Zeichnen
von Grafiken bereit:
/** * Zeichne eine Grafik an der angegebenen Position der Seite. * * @param Zend_Pdf_Image $image * @param float $x1 * @param float $y1 * @param float $x2 * @param float $y2 */ public function drawImage(Zend_Pdf_Image $image, $x1, $y1, $x2, $y2);
Grafikobjekte werden durch Ableitungen der Zend_Pdf_Image
Klasse
abgebildet.
Es werden zur Zeit JPG, PNG und TIFF Grafiken unterstützt:
Beispiel 11.6. Zeichnen von Grafiken
<?php ... // Erstelle ein Grafikobjekt direkt (wähle eines aus) $image = new Zend_Pdf_Image_JPEG('my_image.jpg'); $image = new Zend_Pdf_Image_TIFF('my_image.tiff'); $image = new Zend_Pdf_Image_PNG('my_image.png'); // oder verwende Zend_Pdf_ImageFactory um den entsprechenden Typ zu laden $image = Zend_Pdf_ImageFactory::factory('my_image.jpg'); $pdfPage->drawImage($image, 100, 100, 400, 300); ... ?>
Wichtig! Die Zend_Pdf_Image_JPEG Klasse setzt voraus, dass die GD Erweiterung für PHP konfiguriert wurde. Wichtig! Die Zend_Pdf_Image_PNG Klasse setzt voraus, dass die ZLIB Erweiterung konfiguriert wurde, um mit Grafiken mit Alphakanal zu arbeiten.
Wende dich an die PHP Dokumentation für weitere Informationen (http://www.php.net/manual/en/ref.image.php). (http://www.php.net/manual/en/ref.zlib.php).
Der Stil der Strichzeichnungen wurd durch die Linienbreite, die Linienfarbe und das
Strichmuster definiert. Alle diese Parameter können an die Klassenmethoden von
Zend_Pdf_Page
übergeben werden:
/** Setze die Linienfarbe. */ public function setLineColor(Zend_Pdf_Color $color); /** Setze die Linienbreite. */ public function setLineWidth(float $width); /** * Setze das Strichmuster. * * Pattern ist ein Array mit Fließkommazahlen: array(on_length, off_length, on_length, off_length, ...) * Phase is shift from the beginning of line. * * @param array $pattern * @param array $phase */ public function setLineDashingPattern($pattern, $phase = 0);
Die Methoden Zend_Pdf_Page::drawRectangle()
,
Zend_Pdf_Page::drawPolygon()
, Zend_Pdf_Page::drawCircle()
und
Zend_Pdf_Page::drawEllipse()
akzeptieren das $fillType
Argument als optionalen Parameter. Es kann lauten:
Zend_Pdf_Const::SHAPEDRAW_STROKE - strichele die Form
Zend_Pdf_Const::SHAPEDRAW_FILL - fülle die Form
Zend_Pdf_Const::SHAPEDRAW_FILLNSTROKE - fülle und strichele die Form (Standardverhalten)
Die Zend_Pdf_Page::drawPolygon()
Methode akzeptiert
$fillMethod
als zusätzlichen Parameter:
Zend_Pdf_Const::FILLMETHOD_NONZEROWINDING (Standardverhalten)
Die PDF Referenz beschreibt diese Regel wie folgt:
The nonzero winding number rule determines whether a given point is inside a path by conceptually drawing a ray from that point to infinity in any direction and then examining the places where a segment of the path crosses the ray. Starting with a count of 0, the rule adds 1 each time a path segment crosses the ray from left to right and subtracts 1 each time a segment crosses from right to left. After counting all the crossings, if the result is 0 then the point is outside the path; otherwise it is inside. Note: The method just described does not specify what to do if a path segment coincides with or is tangent to the chosen ray. Since the direction of the ray is arbitrary, the rule simply chooses a ray that does not encounter such problem intersections. For simple convex paths, the nonzero winding number rule defines the inside and outside as one would intuitively expect. The more interesting cases are those involving complex or self-intersecting paths like the ones shown in Figure 4.10 (in a PDF Reference). For a path consisting of a five-pointed star, drawn with five connected straight line segments intersecting each other, the rule considers the inside to be the entire area enclosed by the star, including the pentagon in the center. For a path composed of two concentric circles, the areas enclosed by both circles are considered to be inside, provided that both are drawn in the same direction. If the circles are drawn in opposite directions, only the "doughnut" shape between them is inside, according to the rule; the "doughnut hole" is outside.
Zend_Pdf_Const::FILLMETHOD_EVENODD
Die PDF Referenz beschreibt diese Regel wie folgt:
An alternative to the nonzero winding number rule is the even-odd rule. This rule determines the "insideness" of a point by drawing a ray from that point in any direction and simply counting the number of path segments that cross the ray, regardless of direction. If this number is odd, the point is inside; if even, the point is outside. This yields the same results as the nonzero winding number rule for paths with simple shapes, but produces different results for more complex shapes. Figure 4.11 (in a PDF Reference) shows the effects of applying the even-odd rule to complex paths. For the five-pointed star, the rule considers the triangular points to be inside the path, but not the pentagon in the center. For the two concentric circles, only the "doughnut" shape between the two circles is considered inside, regardless of the directions in which the circles are drawn.
Bevor eine Zeichenoperation angewendet wird, können PDF Seiten gedreht werden. Dies
kann mit Hilfe der Zend_Pdf_Page::rotate()
Methode durchgeführt werden:
/** * Drehe die Seite um den Punkt ($x, $y) mit dem angegebenen Winkel (im Bogenmaß). * * @param float $angle */ public function rotate($x, $y, $angle);
Jederzeit kann der Grafikzustand der Seite (aktueller Zeichensatz, Schriftgröße, Linienfarbe, Füllfarbe, Linienstil, Seitendrehung, Zeichenbereich) gespeichert und wiederhergestellt werden. Speicheroperationen legen die Daten auf einen Grafikzustand Stapel, Wiederherstelloperationen holen Sie daher zurück.
In der Zend_Pdf_Page
Klasse gibt es für diese Operationen zwei Methoden:
/** * Speichere den Grafikzustand dieser Seite. * Es wir ein Schnappschuss vom aktuell festgelegten Stil, Position, Zeichenbereich und * jeder festgelegten Drehung/Umrechnung/Skalierung erstellt. */ public function saveGS(); /** * Stelle den Grafikzustand wieder her, der mit dem letzten Aufruf von saveGS() gespeichert wurde. */ public function restoreGS();
PDF und die Zend_Pdf Komponente unterstützen die Begrenzung des Zeichenbereichs. Der aktuelle Zeichenbereich begrenzt den Seitenbereich, der von Zeichenoperationen beeinflusst werden kann. Zu Beginn ist dies die gesamte Seite.
Die Zend_Pdf_Page
Klasse stellt einen Satz von Methoden für die Begrenzung
bereit.
/** * Durchschneide den aktuellen Zeichenbereich mit einem Rechteck. * * @param float $x1 * @param float $y1 * @param float $x2 * @param float $y2 */ public function clipRectangle($x1, $y1, $x2, $y2);
/** * Durchschneide den aktuellen Zeichenbereich mit einem Polygon. * * @param array $x - Array mit Floats (die X Koordinaten der Eckpunkte) * @param array $y - Array mit Floats (die Y Koordinaten der Eckpunkte) * @param integer $fillMethod */ public function clipPolygon($x, $y, $fillMethod = Zend_Pdf_Const::FILLMETHOD_NONZEROWINDING);
/** * Durchschneide den aktuellen Zeichenbereich mit einem Kreis. * * @param float $x * @param float $y * @param float $radius * @param float $startAngle * @param float $endAngle */ public function clipCircle($x, $y, $radius, $startAngle = null, $endAngle = null);
/** * Durchschneide den aktuellen Zeichenbereich mit einer Ellipse. * * Methoden Signaturen: * drawEllipse($x1, $y1, $x2, $y2); * drawEllipse($x1, $y1, $x2, $y2, $startAngle, $endAngle); * * @todo verarbeite die Sonderfälle mit $x2-$x1 == 0 oder $y2-$y1 == 0 * * @param float $x1 * @param float $y1 * @param float $x2 * @param float $y2 * @param float $startAngle * @param float $endAngle */ public function clipEllipse($x1, $y1, $x2, $y2, $startAngle = null, $endAngle = null);
Die Zend_Pdf_Style
Klasse stellt Stilfunktionalitäten bereit.
Stile können verwendet werden, um mit einer Operation die Parameter für den Grafikzustand zu speichern und auf eine PDF Seite anzuwenden:
/** * Lege den Stil für zukünftige Zeichenoperationen auf dieser Seite fest * * @param Zend_Pdf_Style $style */ public function setStyle(Zend_Pdf_Style $style); /** * Gebe den Stil der Seite zurück. * * @return Zend_Pdf_Style|null */ public function getStyle();
Die Zend_Pdf_Style
Klasse stellt einen Satz von Methoden bereit, um
verschiedene Parameter des Grafikstadiums zu setzen und zu holen:
/** * Setze die Linienfarbe. * * @param Zend_Pdf_Color $color */ public function setLineColor(Zend_Pdf_Color $color);
/** * Hole die Linienfarbe. * * @return Zend_Pdf_Color|null */ public function getLineColor();
/** * Setze die Linienbreite. * * @param float $width */ public function setLineWidth($width);
/** * Hole die Linienbreite. * * @return float */ public function getLineWidth();
/** * Setze das Strichmuster * * @param array $pattern * @param float $phase */ public function setLineDashingPattern($pattern, $phase = 0);
/** * Hole das Strichmuster * * @return array */ public function getLineDashingPattern();
/** * Get line dashing phase * * @return float */ public function getLineDashingPhase();
/** * Setze die Füllfarbe * * @param Zend_Pdf_Color $color */ public function setFillColor(Zend_Pdf_Color $color);
/** * Hole die Füllfarbe. * * @return Zend_Pdf_Color|null */ public function getFillColor();
/** * Ändere den Zeichensatz. * * @param Zend_Pdf_Font $font * @param float $fontSize */ public function setFont(Zend_Pdf_Font $font, $fontSize);
/** * Ändere die Schriftgröße * * @param float $fontSize */ public function setFontSize($fontSize);
/** * Hole den Zeichensatz. * * @return Zend_Pdf_Font $font */ public function getFont();
/** * Hole die Schriftgröße * * @return float $fontSize */ public function getFontSize();