Zend Framework——PDF

Zend_Pdf

生成新的或加载 PDF 文档

...
// 生成新的 PDF 文档.
$pdf1 = new Zend_Pdf();

// 从文件加载 PDF 文档
$pdf2 = Zend_Pdf::load($fileName);

// 从字符串加载 PDF 文档
$pdf3 = Zend_Pdf::parse($pdfString);
...

请求 PDF 文档的指定版本

PDF 文件格式支持增量式文档更新。这样每次文档更新,就产生新的版本。Zend_Pdf 模块支持指定版本的读取。

版本可以指定为 Zend_Pdf::load() 和 Zend_Pdf::parse()的第二个参数或由 Zend_Pdf::rollback() 来请求,
Zend_Pdf::rollback() 方法必需在任何修改前调用,否则它的行为就没有定义。

...
// Load PDF previouse revision of the document.
$pdf1 = Zend_Pdf::load($fileName, 1);

// Load PDF previouse revision of the document.
$pdf2 = Zend_Pdf::parse($pdfString, 1);

// Load first revision of the document.
$pdf3 = Zend_Pdf::load($fileName);
$revisions = $pdf3->revisions();
$pdf3->rollback($revisions - 1);
...

保存修改到 PDF 文档

有两个方法来保存修改到 PDF 文档:Zend_Pdf::save() 和 Zend_Pdf::render()。

Zend_Pdf::save($filename, $updateOnly = false) 保存 PDF 文档到一个文件。如果 $updateOnly 是 true,那么只有新的 PDF 文件段被追加到文件,否则,重写文件。

Zend_Pdf::render($newSegmentOnly = false) 把 PDF 文档当作字符串返回。如果 $newSegmentOnly 是 true,那么只有新的 PDF 文件段返回。

...
// Load PDF document.
$pdf = Zend_Pdf::load($fileName);
...
// Update document
$pdf->save($fileName, true);
// Save document as a new file
$pdf->save($newFileName);

// Return PDF document as a string.
$pdfString = $pdf->render();

...

Zend_Pdf_Page

新页面可以通过创建 Zend_Pdf_Page 对象或调用 Zend_Pdf::newPage() 方法来获得,它返回 Zend_Pdf_Page 对象。Zend_Pdf::newPage() 方法生成已经附加到文档的页面,和未附加的页面不同的是它不能和若干个 PDF 文档一起用,但是性能会稍好一些。有一个参数可选:Zend_Pdf_Page::SIZE_A4Zend_Pdf_Page::SIZE_A4_LANDSCAPEZend_Pdf_Page::SIZE_LETTERZend_Pdf_Page::SIZE_LETTER_LANDSCAPE
文档存储在 Zend_Pdf 类的 public 成员 $pages 里,它是 Zend_Pdf_Page 对象的一个数组。它完整地定义了设置和文档页面的顺序并可以当做普通的数组来处理:

    ...
    // Reverse page order
    $pdf->pages = array_reverse($pdf->pages);
    //array_reverse 以相反顺序返回元素
    ...
    // Add new page  Object
    $pdf->pages[] = new Zend_Pdf_Page(Zend_Pdf_Page::SIZE_A4);
    // Add new page  method
    $pdf->pages[] = $pdf->newPage(Zend_Pdf_Page::SIZE_A4);

    // Remove specified page.
    unset($pdf->pages[$id]);

    ...

页面克隆

    ...
    // 将模板页面存储在单独的变量中
    $template = $pdf->pages[$templatePageIndex];
    ...
    // Add new page
    $page1 = new Zend_Pdf_Page($template);
    $pdf->pages[] = $page1;
    ...

    // Add another page
    $page2 = new Zend_Pdf_Page($template);
    $pdf->pages[] = $page2;
    ...

    // Remove source template page from the documents.
    unset($pdf->pages[$templatePageIndex]);

    ...

绘制图形

1.坐标体系
它从页面的左下角开始,默认情况下以磅(1/72英寸)为单位进行测量。
从一个pages对象中获取Width和Hight

$width  = $pdfPage->getWidth();
$height = $pdfPage->getHeight();

2.颜色
Zend_Pdf模块支持Gray Scale,RGB和CMYK颜色空间,还提供HTML样式颜色:

// $grayLevel (float number). 0.0 (black) - 1.0 (white)
$color1 = new Zend_Pdf_Color_GrayScale($grayLevel);

// $r, $g, $b (float numbers). 0.0 (min intensity) - 1.0 (max intensity)
$color2 = new Zend_Pdf_Color_Rgb($r, $g, $b);

// $c, $m, $y, $k (float numbers). 0.0 (min intensity) - 1.0 (max intensity)
$color3 = new Zend_Pdf_Color_Cmyk($c, $m, $y, $k);

//html
$color1 = new Zend_Pdf_Color_Html('#3366FF');
$color2 = new Zend_Pdf_Color_Html('silver');
$color3 = new Zend_Pdf_Color_Html('forestgreen');

3.形状绘图

/**
 * Draw a line from x1,y1 to x2,y2.
 *
 * @param float $x1
 * @param float $y1
 * @param float $x2
 * @param float $y2
 * @return Zend_Pdf_Page 
 */
public function drawLine($x1, $y1, $x2, $y2);
/**
 * Draw a rectangle.
 *
 * Fill types:
 * Zend_Pdf_Page::SHAPE_DRAW_FILL_AND_STROKE - fill rectangle
 *                                             and stroke (default)
 * Zend_Pdf_Page::SHAPE_DRAW_STROKE          - stroke rectangle
 * Zend_Pdf_Page::SHAPE_DRAW_FILL            - fill rectangle
 *
 * @param float $x1
 * @param float $y1
 * @param float $x2
 * @param float $y2
 * @param integer $fillType
 * @return Zend_Pdf_Page 
 */
public function drawRectangle($x1, $y1, $x2, $y2,
                    $fillType = Zend_Pdf_Page::SHAPE_DRAW_FILL_AND_STROKE);
/**
 * Draw a polygon.多边形
 *
 * If $fillType is Zend_Pdf_Page::SHAPE_DRAW_FILL_AND_STROKE or
 * Zend_Pdf_Page::SHAPE_DRAW_FILL, then polygon is automatically closed.
 *
 * @param array $x  - array of float (the X co-ordinates of the vertices)
 * @param array $y  - array of float (the Y co-ordinates of the vertices)
 * @param integer $fillType
 * @param integer $fillMethod
 * @return Zend_Pdf_Page 
 */
public function drawPolygon($x, $y,
                            $fillType =
                                Zend_Pdf_Page::SHAPE_DRAW_FILL_AND_STROKE,
                            $fillMethod =
                                Zend_Pdf_Page::FILL_METHOD_NON_ZERO_WINDING);
/**
 * Draw a circle centered on x, y with a radius of radius.
 *
 * Angles are specified in radians
 *
 * Method signatures:
 * drawCircle($x, $y, $radius);
 * drawCircle($x, $y, $radius, $fillType);
 * drawCircle($x, $y, $radius, $startAngle, $endAngle);
 * drawCircle($x, $y, $radius, $startAngle, $endAngle, $fillType);
 *
 *
 * It's not a really circle, because PDF supports only cubic Bezier
 * curves. But very good approximation.
 * It differs from a real circle on a maximum 0.00026 radiuses (at PI/8,
 * 3*PI/8, 5*PI/8, 7*PI/8, 9*PI/8, 11*PI/8, 13*PI/8 and 15*PI/8 angles).
 * At 0, PI/4, PI/2, 3*PI/4, PI, 5*PI/4, 3*PI/2 and 7*PI/4 it's exactly
 * a tangent to a circle.
 *
 * @param float $x
 * @param float $y
 * @param float $radius
 * @param mixed $param4
 * @param mixed $param5
 * @param mixed $param6
 * @return Zend_Pdf_Page 
 */
public function  drawCircle($x,
                            $y,
                            $radius,
                            $param4 = null,
                            $param5 = null,
                            $param6 = null);
/**
 * Draw an ellipse inside the specified rectangle.在指定的矩形内绘制一个椭圆
 *
 * Method signatures:
 * 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);
 *
 * Angles are specified in radians
 *
 * @param float $x1
 * @param float $y1
 * @param float $x2
 * @param float $y2
 * @param mixed $param5
 * @param mixed $param6
 * @param mixed $param7
 * @return Zend_Pdf_Page 
 */
public function drawEllipse($x1,
                            $y1,
                            $x2,
                            $y2,
                            $param5 = null,
                            $param6 = null,
                            $param7 = null);

4.文字绘图
您可以通过提供基线的x和y坐标在页面上的任何位置绘制单行文本。当前字体和当前字体大小用于文本绘制操作。

/**
 * Draw a line of text at the specified position.
 *
 * @param string $text
 * @param float $x
 * @param float $y
 * @param string $charEncoding (optional)可选
 * Character encoding of source text.Defaults to current locale.
 * 源文本的字符编码。默认为当前语言环境,您可以使用PHP的iconv()函数支持的任何编码方法提供源字符串
 * @throws Zend_Pdf_Exception
 * @return Zend_Pdf_Page 
 */
public function drawText($text, $x, $y, $charEncoding = '');

在页面上绘制一个默认字符串

...
$pdfPage->drawText('Hello world!', 72, 720);
...

在页面上绘制一个UTF-8编码的字符串

...
// Read a UTF-8-encoded string from disk
$unicodeString = fread($fp, 1024);

// Draw the string on the page
$pdfPage->drawText($unicodeString, 72, 720, 'UTF-8');
...

5.设置字体
Zend_Pdf_Page :: drawText()默认使用页面的当前字体和字体大小,使用Zend_Pdf_Page :: setFont()方法设置:

/**
 * Set current font.
 *
 * @param Zend_Pdf_Resource_Font $font
 * @param float $fontSize
 * @return Zend_Pdf_Page 
 */
public function setFont(Zend_Pdf_Resource_Font $font, $fontSize);

Zend_Pdf目前支持标准的14种PDF字体以及您自己的自定义TrueType字体。
字体对象通过两种工厂方法之一获得:标准14 PDF字体的Zend_Pdf_Font :: fontWithName($fontName)或自定义字体的Zend_Pdf_Font :: fontWithPath($ filePath)
标准14 PDF字体名称的常量在Zend_Pdf_Font类中定义:
Zend_Pdf_Font :: FONT_COURIER
Zend_Pdf_Font :: FONT_COURIER_BOLD
Zend_Pdf_Font :: FONT_COURIER_ITALIC
Zend_Pdf_Font :: FONT_COURIER_BOLD_ITALIC
Zend_Pdf_Font :: FONT_TIMES
Zend_Pdf_Font :: FONT_TIMES_BOLD
Zend_Pdf_Font :: FONT_TIMES_ITALIC
Zend_Pdf_Font :: FONT_TIMES_BOLD_ITALIC
Zend_Pdf_Font :: FONT_HELVETICA
Zend_Pdf_Font :: FONT_HELVETICA_BOLD
Zend_Pdf_Font :: FONT_HELVETICA_ITALIC
Zend_Pdf_Font :: FONT_HELVETICA_BOLD_ITALIC
Zend_Pdf_Font :: FONT_SYMBOL
Zend_Pdf_Font :: FONT_ZAPFDINGBATS

创建标准字体

...
// Create new font
$font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA);

// Apply font
$pdfPage->setFont($font, 36);
...

您还可以使用任何单独的TrueType字体(通常具有’.ttf’扩展名)或OpenType字体(’.otf’扩展名)。
要使用TrueType字体,必须提供字体程序的完整文件路径。如果由于某种原因无法读取字体,或者它不是TrueType字体,则工厂方法将引发异常:

创建TrueType字体

...
// Create new font
$goodDogCoolFont = Zend_Pdf_Font::fontWithPath('/path/to/GOODDC__.TTF');

// Apply font
$pdfPage->setFont($goodDogCoolFont, 36);
...

默认情况下,自定义字体将嵌入到生成的PDF文档中。这允许收件人按预期查看页面,即使他们的系统上没有安装正确的字体。如果您担心文件大小,可以通过将 do not embed 选项传递给factory方法来请求不嵌入字体程序:

如果未嵌入字体程序但PDF文件的收件人在其系统上安装了字体,则他们将按预期看到该文档。如果他们没有安装正确的字体,PDF查看器应用程序替换默认字体。

创建TrueType字体,但不将其嵌入PDF文档中。

...
// Create new font
$goodDogCoolFont = Zend_Pdf_Font::fontWithPath('/path/to/GOODDC__.TTF',
                                               Zend_Pdf_Font::EMBED_DONT_EMBED);

// Apply font
$pdfPage->setFont($goodDogCoolFont, 36);
...

某些字体具有非常特定的许可规则,这些规则阻止它们嵌入PDF文档中。因此,如果您尝试使用无法嵌入的字体,那么工厂方法将引发异常。如果允许最终用户选择自己的字体,则首选此抑制技术。

您仍然可以使用这些字体,但您必须如上所述传递 do not embed 标志,或者您可以简单地抑制异常:

抑制无法嵌入的字体抛出的异常。

...
$font = Zend_Pdf_Font::fontWithPath(
           '/path/to/unEmbeddableFont.ttf',
           Zend_Pdf_Font::EMBED_SUPPRESS_EMBED_EXCEPTION
        );
...

字体程序可能相当大,有些可达数十兆字节。默认情况下,使用Flate压缩方案压缩所有嵌入字体,平均节省50%的空间。如果由于某种原因,您不想压缩字体程序,可以使用以下选项禁用它:

不要压缩嵌入的字体

...
$font = Zend_Pdf_Font::fontWithPath('/path/to/someReallyBigFont.ttf',
                                    Zend_Pdf_Font::EMBED_DONT_COMPRESS);
...

最后,必要时,可以使用按位OR运算符组合嵌入选项:

结合字体嵌入选项

...
$font = Zend_Pdf_Font::fontWithPath(
            $someUserSelectedFontPath,
            (Zend_Pdf_Font::EMBED_SUPPRESS_EMBED_EXCEPTION |
            Zend_Pdf_Font::EMBED_DONT_COMPRESS));
...

Zend_Pdf使用CP1252(WinLatin1)绘制带有标准字体的文本,Zend_Pdf文本仍然可以以任何其他编码提供,但只会绘制WinLatin1字符。

字符限制

...
$font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_COURIER);
$pdfPage->setFont($font, 36)
        ->drawText('Euro sign - €', 72, 720, 'UTF-8')
        ->drawText('Text with umlauts - à è ì', 72, 650, 'UTF-8');
...

提取字体

Zend_Pdf模块提供从加载的文档中提取字体的可能性。

它可能对增量文档更新很有用。如果没有此功能,则每次要更新时都必须附加并可能将字体嵌入到文档中。

Zend_Pdf和Zend_Pdf_Page对象提供了提取文档或页面中提到的所有字体的特殊方法:

例32.15。从加载的文档中提取字体。

...
$pdf = Zend_Pdf::load($documentPath);
...
// Get all document fonts
$fontList = $pdf->extractFonts();
$pdf->pages[] = ($page = $pdf->newPage(Zend_Pdf_Page::SIZE_A4));
$yPosition = 700;
foreach ($fontList as $font) {
    $page->setFont($font, 15);
    $fontName = $font->getFontName(Zend_Pdf_Font::NAME_POSTSCRIPT,
                                   'en',
                                   'UTF-8');
    $page->drawText($fontName . ':  The quick brown fox jumps over the lazy dog',
                    100,
                    $yPosition,
                    'UTF-8');
    $yPosition -= 30;
}
...
// Get fonts referenced within the first document page
$firstPage = reset($pdf->pages);//reset()把数组指针指向第一项
$firstPageFonts = $firstPage->extractFonts();
...

通过指定字体名称从加载的文档中提取字体。

...
$pdf = new Zend_Pdf();
...
$pdf->pages[] = ($page = $pdf->newPage(Zend_Pdf_Page::SIZE_A4));

$font = Zend_Pdf_Font::fontWithPath($fontPath);
$page->setFont($font, $fontSize);
$page->drawText($text, $x, $y);
...
// This font name should be stored somewhere...
$fontName = $font->getFontName(Zend_Pdf_Font::NAME_POSTSCRIPT,
                               'en',
                               'UTF-8');
...
$pdf->save($docPath);
...

...
$pdf = Zend_Pdf::load($docPath);
...
$pdf->pages[] = ($page = $pdf->newPage(Zend_Pdf_Page::SIZE_A4));

/* $srcPage->extractFont($fontName) can also be used here */
$font = $pdf->extractFont($fontName);

$page->setFont($font, $fontSize);
$page->drawText($text, $x, $y);
...
$pdf->save($docPath, true /* incremental update mode */);
...

提取的字体可用于替代任何其他字体,但具有以下限制:

提取的字体只能在提取它的文档的上下文中使用。

实际上可能没有提取嵌入字体程序。因此,提取的字体无法提供正确的字体指标,原始字体必须用于文本宽度计算:


...
$font = $pdf->extractFont($fontName);
$originalFont = Zend_Pdf_Font::fontWithPath($fontPath);

$page->setFont($font /* use extracted font for drawing */, $fontSize);
$xPosition = $x;
for ($charIndex = 0; $charIndex < strlen($text); $charIndex++) {
    $page->drawText($text[$charIndex], xPosition, $y);

    // Use original font for text width calculation
    $width = $originalFont->widthForGlyph(
                 $originalFont->glyphNumberForCharacter($text[$charIndex])
             );
    $xPosition += $width/$originalFont->getUnitsPerEm()*$fontSize;
}
...

图像绘制
Zend_Pdf_Page类提供 drawImage() 方法来绘制图像:

/**
 * Draw an image at the specified position on the page.
 *
 * @param Zend_Pdf_Resource_Image $image
 * @param float $x1
 * @param float $y1
 * @param float $x2
 * @param float $y2
 * @return Zend_Pdf_Page 
 */
public function drawImage(Zend_Pdf_Resource_Image $image, $x1, $y1, $x2, $y2);

应使用 Zend_Pdf_Image :: imageWithPath($filePath) 方法创建图像对象(现在支持JPG,PNG和TIFF图像),JPEG支持需要配置PHP GD扩展,PNG支持要求将ZLIB扩展配置为使用Alpha通道图像:

图像绘制

...
// load image
$image = Zend_Pdf_Image::imageWithPath('my_image.jpg');

$pdfPage->drawImage($image, 100, 100, 400, 300);
...

线描样式
线描样式由线宽,线条颜色和线条划线图案定义。所有这些参数都可以通过Zend_Pdf_Page类方法分配:

/** Set line color. */
public function setLineColor(Zend_Pdf_Color $color);

/** Set line width. */
public function setLineWidth(float $width);

/**
 * Set line dashing pattern.
 *
 * Pattern is an array of floats:
 *     array(on_length, off_length, on_length, off_length, ...)
 * Phase is shift from the beginning of line.
 *
 * @param array $pattern
 * @param array $phase
 * @return Zend_Pdf_Page 
 */
public function setLineDashingPattern($pattern, $phase = 0);

填充样式
Zend_Pdf_Page :: drawRectangle()Zend_Pdf_Page :: drawPolygon()Zend_Pdf_Page :: drawCircle()Zend_Pdf_Page :: drawEllipse()方法将$ fillType参数作为可选参数:
Zend_Pdf_Page :: SHAPE_DRAW_STROKE - 描边形状
Zend_Pdf_Page :: SHAPE_DRAW_FILL - 仅填充形状
Zend_Pdf_Page :: SHAPE_DRAW_FILL_AND_STROKE - 填充和描边(默认行为)
Zend_Pdf_Page :: drawPolygon()方法还需要一个额外的参数$fillMethod
Zend_Pdf_Page :: FILL_METHOD_NON_ZERO_WINDING(默认)
对于由五角星组成的路径,由五个相互交叉的相互连接的直线段绘制,规则认为内部是由恒星包围的整个区域,包括中心的五边形。对于由两个同心圆组成的路径,两个圆圈所包围的区域被认为是在内部,只要两者都在相同的方向上绘制。如果圆圈是以相反的方向绘制的,根据规则,它们之间只有“圆环”形状在里面; “甜甜圈洞”在外面。
Zend_Pdf_Page :: FILL_METHOD_EVEN_ODD
对于五角星,规则认为三角形点位于路径内,而不是中心的五边形。对于两个同心圆,在内部仅考虑两个圆之间的“圆环”形状,而不管绘制圆的方向如何。

线性变换
1.旋转。
在应用任何绘制操作之前,可以旋转PDF页面。它可以通过Zend_Pdf_Page :: rotate()方法完成:

/**
 * Rotate the page.
 *
 * @param float $x  - the X co-ordinate of rotation point
 * @param float $y  - the Y co-ordinate of rotation point
 * @param float $angle - rotation angle
 * @return Zend_Pdf_Page
 */
public function rotate($x, $y, $angle);

2.缩放。
Zend_Pdf_Page :: scale()

/**
 * Scale coordination system.
 *
 * @param float $xScale - X dimention scale factor
 * @param float $yScale - Y dimention scale factor
 * @return Zend_Pdf_Page
 */
public function scale($xScale, $yScale);

3.移动。
Zend_Pdf_Page :: translate()

/**
 * Translate coordination system.
 *
 * @param float $xShift - X coordinate shift
 * @param float $yShift - Y coordinate shift
 * @return Zend_Pdf_Page
 */
public function translate($xShift, $yShift);

4.倾斜。
Zend_Pdf_Page :: skew()

/**
 * Translate coordination system.
 *
 * @param float $x  - the X co-ordinate of axis skew point
 * @param float $y  - the Y co-ordinate of axis skew point
 * @param float $xAngle - X axis skew angle
 * @param float $yAngle - Y axis skew angle
 * @return Zend_Pdf_Page
 */
public function skew($x, $y, $xAngle, $yAngle);

保存/恢复图形状态
在任何时候页面图形状态(当前字体,字体大小,线条颜色,填充颜色,线条样式,页面旋转,剪辑区域)都可以保存然后恢复。保存操作将数据放入图形状态堆栈,还原操作从那里检索它。
Zend_Pdf_Page类中有两种方法可用于这些操作:

/**
 * Save the graphics state of this page.
 * This takes a snapshot of the currently applied style, position,
 * clipping area and any rotation/translation/scaling that has been
 * applied.
 *
 * @return Zend_Pdf_Page 
 */
public function saveGS();

/**
 * Restore the graphics state that was saved with the last call to
 * saveGS().
 *
 * @return Zend_Pdf_Page 
 */
public function restoreGS();

剪裁绘制区域
PDF和Zend_Pdf模块支持绘制区域的剪切。Zend_Pdf_Page类为剪切操作提供了一组方法。

/**
 * Intersect current clipping area with a rectangle.
 *
 * @param float $x1
 * @param float $y1
 * @param float $x2
 * @param float $y2
 * @return Zend_Pdf_Page 
 */
public function clipRectangle($x1, $y1, $x2, $y2);


/**
 * Intersect current clipping area with a polygon.
 *
 * @param array $x  - array of float (the X co-ordinates of the vertices)
 * @param array $y  - array of float (the Y co-ordinates of the vertices)
 * @param integer $fillMethod
 * @return Zend_Pdf_Page 
 */
public function clipPolygon($x,
                            $y,
                            $fillMethod =
                                Zend_Pdf_Page::FILL_METHOD_NON_ZERO_WINDING);


/**
 * Intersect current clipping area with a circle.
 *
 * @param float $x
 * @param float $y
 * @param float $radius
 * @param float $startAngle
 * @param float $endAngle
 * @return Zend_Pdf_Page 
 */
public function clipCircle($x,
                           $y,
                           $radius,
                           $startAngle = null,
                           $endAngle = null);


/**
 * Intersect current clipping area with an ellipse.
 *
 * Method signatures:
 * drawEllipse($x1, $y1, $x2, $y2);
 * drawEllipse($x1, $y1, $x2, $y2, $startAngle, $endAngle);
 *
 * @todo process special cases with $x2-$x1 == 0 or $y2-$y1 == 0
 *
 * @param float $x1
 * @param float $y1
 * @param float $x2
 * @param float $y2
 * @param float $startAngle
 * @param float $endAngle
 * @return Zend_Pdf_Page 
 */
public function clipEllipse($x1,
                            $y1,
                            $x2,
                            $y2,
                            $startAngle = null,
                            $endAngle = null);

样式
Zend_Pdf_Style类提供样式功能。
样式可用于存储一组图形状态参数,并通过一个操作将其应用于PDF页面:

/**
 * Set the style to use for future drawing operations on this page
 *
 * @param Zend_Pdf_Style $style
 * @return Zend_Pdf_Page 
 */
public function setStyle(Zend_Pdf_Style $style);

/**
 * Return the style, applied to the page.
 *
 * @return Zend_Pdf_Style|null
 */
public function getStyle();

Zend_Pdf_Style类提供了一组设置或获取不同图形状态参数的方法:

/**
 * Set line color.
 *
 * @param Zend_Pdf_Color $color
 * @return Zend_Pdf_Page 
 */
public function setLineColor(Zend_Pdf_Color $color);


/**
 * Get line color.
 *
 * @return Zend_Pdf_Color|null
 */
public function getLineColor();


/**
 * Set line width.
 *
 * @param float $width
 * @return Zend_Pdf_Page 
 */
public function setLineWidth($width);


/**
 * Get line width.
 *
 * @return float
 */
public function getLineWidth();


/**
 * Set line dashing pattern
 *
 * @param array $pattern
 * @param float $phase
 * @return Zend_Pdf_Page 
 */
public function setLineDashingPattern($pattern, $phase = 0);


/**
 * Get line dashing pattern
 *
 * @return array
 */
public function getLineDashingPattern();


/**
 * Get line dashing phase
 *
 * @return float
 */
public function getLineDashingPhase();


/**
 * Set fill color.
 *
 * @param Zend_Pdf_Color $color
 * @return Zend_Pdf_Page 
 */
public function setFillColor(Zend_Pdf_Color $color);


/**
 * Get fill color.
 *
 * @return Zend_Pdf_Color|null
 */
public function getFillColor();


/**
 * Set current font.
 *
 * @param Zend_Pdf_Resource_Font $font
 * @param float $fontSize
 * @return Zend_Pdf_Page 
 */
public function setFont(Zend_Pdf_Resource_Font $font, $fontSize);


/**
 * Modify current font size
 *
 * @param float $fontSize
 * @return Zend_Pdf_Page 
 */
public function setFontSize($fontSize);


/**
 * Get current font.
 *
 * @return Zend_Pdf_Resource_Font $font
 */
public function getFont();


/**
 * Get current font size
 *
 * @return float $fontSize
 */
public function getFontSize();

32.5.15。 透明度
Zend_Pdf模块支持透明度处理。
可以使用 Zend_Pdf_Page :: setAlpha() 方法设置透明度:

/**
 * Set the transparency
 *
 * $alpha == 0  - transparent
 * $alpha == 1  - opaque
 *
 * Transparency modes, supported by PDF:
 * Normal (default), Multiply, Screen, Overlay, Darken, Lighten,
 * ColorDodge, ColorBurn, HardLight, SoftLight, Difference, Exclusion
 *
 * @param float $alpha
 * @param string $mode
 * @throws Zend_Pdf_Exception
 * @return Zend_Pdf_Page 
 */
public function setAlpha($alpha, $mode = 'Normal');

Zend_Pdf模块用法示例
此示例可以在demos / Zend / Pdf / demo.php文件中找到。还有test.pdf文件,可以与此演示一起用于测试目的。

/**
 * @package Zend_Pdf
 * @subpackage demo
 */

if (!isset($argv[1])) {
    echo "USAGE: php demo.php <pdf_file> [<output_pdf_file>]\n";
    exit;
}

try {
    $pdf = Zend_Pdf::load($argv[1]);
} catch (Zend_Pdf_Exception $e) {
    if ($e->getMessage() == 'Can not open \'' . $argv[1] .
                            '\' file for reading.') {
        // Create new PDF if file doesn't exist
        $pdf = new Zend_Pdf();

        if (!isset($argv[2])) {
            // force complete file rewriting (instead of updating)
            $argv[2] = $argv[1];
        }
    } else {
        // Throw an exception if it's not the "Can't open file
        // exception
        throw $e;
    }
}

//------------------------------------------------------------------------
// Reverse page order
$pdf->pages = array_reverse($pdf->pages);

// Create new Style
$style = new Zend_Pdf_Style();
$style->setFillColor(new Zend_Pdf_Color_Rgb(0, 0, 0.9));
$style->setLineColor(new Zend_Pdf_Color_GrayScale(0.2));
$style->setLineWidth(3);
$style->setLineDashingPattern(array(3, 2, 3, 4), 1.6);
$fontH = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA_BOLD);
$style->setFont($fontH, 32);

try {
    // Create new image object
    $imageFile = dirname(__FILE__) . '/stamp.jpg';
    $stampImage = Zend_Pdf_Image::imageWithPath($imageFile);
} catch (Zend_Pdf_Exception $e) {
    // Example of operating with image loading exceptions.
    if ($e->getMessage() != 'Image extension is not installed.' &&
        $e->getMessage() != 'JPG support is not configured properly.') {
        throw $e;
    }
    $stampImage = null;
}

// Mark page as modified
foreach ($pdf->pages as $page){
    $page->saveGS()
         ->setAlpha(0.25)
         ->setStyle($style)
         ->rotate(0, 0, M_PI_2/3);

    $page->saveGS();
    $page->clipCircle(550, -10, 50);
    if ($stampImage != null) {
        $page->drawImage($stampImage, 500, -60, 600, 40);
    }
    $page->restoreGS();

    $page->drawText('Modified by Zend Framework!', 150, 0)
         ->restoreGS();
}

// Add new page generated by Zend_Pdf object
// (page is attached to the specified the document)
$pdf->pages[] = ($page1 = $pdf->newPage('A4'));

// Add new page generated by Zend_Pdf_Page object
// (page is not attached to the document)
$page2 = new Zend_Pdf_Page(Zend_Pdf_Page::SIZE_LETTER_LANDSCAPE);
$pdf->pages[] = $page2;

// Create new font
$font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA);

// Apply font and draw text
$page1->setFont($font, 36)
      ->setFillColor(Zend_Pdf_Color_Html::color('#9999cc'))
      ->drawText('Helvetica 36 text string', 60, 500);

// Use font object for another page
$page2->setFont($font, 24)
      ->drawText('Helvetica 24 text string', 60, 500);

// Use another font
$fontT = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_TIMES);
$page2->setFont($fontT, 32)
      ->drawText('Times-Roman 32 text string', 60, 450);

// Draw rectangle
$page2->setFillColor(new Zend_Pdf_Color_GrayScale(0.8))
      ->setLineColor(new Zend_Pdf_Color_GrayScale(0.2))
      ->setLineDashingPattern(array(3, 2, 3, 4), 1.6)
      ->drawRectangle(60, 400, 400, 350);

// Draw circle
$page2->setLineDashingPattern(Zend_Pdf_Page::LINE_DASHING_SOLID)
      ->setFillColor(new Zend_Pdf_Color_Rgb(1, 0, 0))
      ->drawCircle(85, 375, 25);

// Draw sectors
$page2->drawCircle(200, 375, 25, 2*M_PI/3, -M_PI/6)
      ->setFillColor(new Zend_Pdf_Color_Cmyk(1, 0, 0, 0))
      ->drawCircle(200, 375, 25, M_PI/6, 2*M_PI/3)
      ->setFillColor(new Zend_Pdf_Color_Rgb(1, 1, 0))
      ->drawCircle(200, 375, 25, -M_PI/6, M_PI/6);

// Draw ellipse
$page2->setFillColor(new Zend_Pdf_Color_Rgb(1, 0, 0))
      ->drawEllipse(250, 400, 400, 350)
      ->setFillColor(new Zend_Pdf_Color_Cmyk(1, 0, 0, 0))
      ->drawEllipse(250, 400, 400, 350, M_PI/6, 2*M_PI/3)
      ->setFillColor(new Zend_Pdf_Color_Rgb(1, 1, 0))
      ->drawEllipse(250, 400, 400, 350, -M_PI/6, M_PI/6);

// Draw and fill polygon
$page2->setFillColor(new Zend_Pdf_Color_Rgb(1, 0, 1));
$x = array();
$y = array();
for ($count = 0; $count < 8; $count++) {
    $x[] = 140 + 25*cos(3*M_PI_4*$count);
    $y[] = 375 + 25*sin(3*M_PI_4*$count);
}
$page2->drawPolygon($x, $y,
                    Zend_Pdf_Page::SHAPE_DRAW_FILL_AND_STROKE,
                    Zend_Pdf_Page::FILL_METHOD_EVEN_ODD);

// ----------- Draw figures in modified coordination system --------------

// Coordination system movement
$page2->saveGS();
$page2->translate(60, 250); // Shift coordination system

// Draw rectangle
$page2->setFillColor(new Zend_Pdf_Color_GrayScale(0.8))
      ->setLineColor(new Zend_Pdf_Color_GrayScale(0.2))
      ->setLineDashingPattern(array(3, 2, 3, 4), 1.6)
      ->drawRectangle(0, 50, 340, 0);

// Draw circle
$page2->setLineDashingPattern(Zend_Pdf_Page::LINE_DASHING_SOLID)
      ->setFillColor(new Zend_Pdf_Color_Rgb(1, 0, 0))
      ->drawCircle(25, 25, 25);

// Draw sectors
$page2->drawCircle(140, 25, 25, 2*M_PI/3, -M_PI/6)
      ->setFillColor(new Zend_Pdf_Color_Cmyk(1, 0, 0, 0))
      ->drawCircle(140, 25, 25, M_PI/6, 2*M_PI/3)
      ->setFillColor(new Zend_Pdf_Color_Rgb(1, 1, 0))
      ->drawCircle(140, 25, 25, -M_PI/6, M_PI/6);

// Draw ellipse
$page2->setFillColor(new Zend_Pdf_Color_Rgb(1, 0, 0))
      ->drawEllipse(190, 50, 340, 0)
      ->setFillColor(new Zend_Pdf_Color_Cmyk(1, 0, 0, 0))
      ->drawEllipse(190, 50, 340, 0, M_PI/6, 2*M_PI/3)
      ->setFillColor(new Zend_Pdf_Color_Rgb(1, 1, 0))
      ->drawEllipse(190, 50, 340, 0, -M_PI/6, M_PI/6);

// Draw and fill polygon
$page2->setFillColor(new Zend_Pdf_Color_Rgb(1, 0, 1));
$x = array();
$y = array();
for ($count = 0; $count < 8; $count++) {
    $x[] = 80 + 25*cos(3*M_PI_4*$count);
    $y[] = 25 + 25*sin(3*M_PI_4*$count);
}
$page2->drawPolygon($x, $y,
                    Zend_Pdf_Page::SHAPE_DRAW_FILL_AND_STROKE,
                    Zend_Pdf_Page::FILL_METHOD_EVEN_ODD);

// Draw line
$page2->setLineWidth(0.5)
      ->drawLine(0, 25, 340, 25);

$page2->restoreGS();


// Coordination system movement, skewing and scaling
$page2->saveGS();
$page2->translate(60, 150)     // Shift coordination system
      ->skew(0, 0, 0, -M_PI/9) // Skew coordination system
      ->scale(0.9, 0.9);       // Scale coordination system

// Draw rectangle
$page2->setFillColor(new Zend_Pdf_Color_GrayScale(0.8))
      ->setLineColor(new Zend_Pdf_Color_GrayScale(0.2))
      ->setLineDashingPattern(array(3, 2, 3, 4), 1.6)
      ->drawRectangle(0, 50, 340, 0);

// Draw circle
$page2->setLineDashingPattern(Zend_Pdf_Page::LINE_DASHING_SOLID)
      ->setFillColor(new Zend_Pdf_Color_Rgb(1, 0, 0))
      ->drawCircle(25, 25, 25);

// Draw sectors
$page2->drawCircle(140, 25, 25, 2*M_PI/3, -M_PI/6)
      ->setFillColor(new Zend_Pdf_Color_Cmyk(1, 0, 0, 0))
      ->drawCircle(140, 25, 25, M_PI/6, 2*M_PI/3)
      ->setFillColor(new Zend_Pdf_Color_Rgb(1, 1, 0))
      ->drawCircle(140, 25, 25, -M_PI/6, M_PI/6);

// Draw ellipse
$page2->setFillColor(new Zend_Pdf_Color_Rgb(1, 0, 0))
      ->drawEllipse(190, 50, 340, 0)
      ->setFillColor(new Zend_Pdf_Color_Cmyk(1, 0, 0, 0))
      ->drawEllipse(190, 50, 340, 0, M_PI/6, 2*M_PI/3)
      ->setFillColor(new Zend_Pdf_Color_Rgb(1, 1, 0))
      ->drawEllipse(190, 50, 340, 0, -M_PI/6, M_PI/6);

// Draw and fill polygon
$page2->setFillColor(new Zend_Pdf_Color_Rgb(1, 0, 1));
$x = array();
$y = array();
for ($count = 0; $count < 8; $count++) {
    $x[] = 80 + 25*cos(3*M_PI_4*$count);
    $y[] = 25 + 25*sin(3*M_PI_4*$count);
}
$page2->drawPolygon($x, $y,
                    Zend_Pdf_Page::SHAPE_DRAW_FILL_AND_STROKE,
                    Zend_Pdf_Page::FILL_METHOD_EVEN_ODD);

// Draw line
$page2->setLineWidth(0.5)
      ->drawLine(0, 25, 340, 25);

$page2->restoreGS();

//------------------------------------------------------------------------

if (isset($argv[2])) {
    $pdf->save($argv[2]);
} else {
    $pdf->save($argv[1], true /* update */);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值