예제 #1
0
 public function testFontExtracting()
 {
     if (PHP_OS == 'AIX') {
         $this->markTestSkipped('Not supported on AIX');
     }
     $pdf = new Pdf\PdfDocument();
     $fontsList = array(Pdf\Font::FONT_COURIER, Pdf\Font::FONT_HELVETICA_BOLD, Pdf\Font::FONT_TIMES_BOLD_ITALIC);
     foreach ($fontsList as $fontName) {
         // Add new page generated by Zend_PDF object (page is attached to the specified the document)
         $pdf->pages[] = $page = $pdf->newPage(Pdf\Page::SIZE_A4_LANDSCAPE);
         $font = Pdf\Font::fontWithName($fontName);
         $page->setFont($font, 10)->drawText($font->getFontName(Pdf\Font::NAME_POSTSCRIPT, 'en') . ':', 100, 400);
         $page->setFont($font, 20)->drawText("'The quick brown fox jumps over the lazy dog'", 100, 360);
         $type = $font->getFontType();
     }
     $TTFFontsList = array('VeraBd.ttf', 'VeraBI.ttf', 'VeraIt.ttf', 'VeraMoBd.ttf', 'VeraMoBI.ttf', 'VeraMoIt.ttf', 'VeraMono.ttf', 'VeraSeBd.ttf', 'VeraSe.ttf', 'Vera.ttf');
     foreach ($TTFFontsList as $fontName) {
         // Add new page generated by Zend_PDF object (page is attached to the specified the document)
         $pdf->pages[] = $page = $pdf->newPage(Pdf\Page::SIZE_A4_LANDSCAPE);
         $font = Pdf\Font::fontWithPath(__DIR__ . '/_fonts/' . $fontName);
         $page->setFont($font, 10)->drawText($font->getFontName(Pdf\Font::NAME_POSTSCRIPT, 'en', 'CP1252') . ':', 100, 400);
         $page->setFont($font, 20)->drawText("'The quick brown fox jumps over the lazy dog'", 100, 360);
         $type = $font->getFontType();
     }
     $pdf->save(__DIR__ . '/_files/output.pdf');
     unset($pdf);
     $pdf1 = Pdf\PdfDocument::load(__DIR__ . '/_files/output.pdf');
     $newPages = array();
     $fontList = array();
     $fontNames = array();
     foreach ($pdf1->pages as $page) {
         $pageFonts = $page->extractFonts();
         foreach ($pageFonts as $font) {
             $fontList[] = $font;
             $fontNames[] = $font->getFontName(Pdf\Font::NAME_POSTSCRIPT, 'en', 'UTF-8');
         }
     }
     $this->assertEquals(array(Pdf\Font::FONT_COURIER, Pdf\Font::FONT_HELVETICA_BOLD, Pdf\Font::FONT_TIMES_BOLD_ITALIC, 'BitstreamVeraSans-Bold', 'BitstreamVeraSans-BoldOblique', 'BitstreamVeraSans-Oblique', 'BitstreamVeraSansMono-Bold', 'BitstreamVeraSansMono-BoldOb', 'BitstreamVeraSansMono-Oblique', 'BitstreamVeraSansMono-Roman', 'BitstreamVeraSerif-Bold', 'BitstreamVeraSerif-Roman', 'BitstreamVeraSans-Roman'), $fontNames);
     $pdf1->pages[] = $page = $pdf1->newPage(Pdf\Page::SIZE_A4);
     $yPosition = 700;
     foreach ($fontList as $font) {
         $page->setFont($font, 15)->drawText("The quick brown fox jumps over the lazy dog", 100, $yPosition);
         $yPosition -= 30;
     }
     $fontNames1 = array();
     foreach ($pdf1->extractFonts() as $font) {
         $fontNames1[] = $font->getFontName(Pdf\Font::NAME_POSTSCRIPT, 'en', 'UTF-8');
     }
     $this->assertEquals(array(Pdf\Font::FONT_COURIER, Pdf\Font::FONT_HELVETICA_BOLD, Pdf\Font::FONT_TIMES_BOLD_ITALIC, 'BitstreamVeraSans-Bold', 'BitstreamVeraSans-BoldOblique', 'BitstreamVeraSans-Oblique', 'BitstreamVeraSansMono-Bold', 'BitstreamVeraSansMono-BoldOb', 'BitstreamVeraSansMono-Oblique', 'BitstreamVeraSansMono-Roman', 'BitstreamVeraSerif-Bold', 'BitstreamVeraSerif-Roman', 'BitstreamVeraSans-Roman'), $fontNames1);
     $page = reset($pdf1->pages);
     $font = $page->extractFont(Pdf\Font::FONT_COURIER);
     $this->assertTrue($font instanceof Font\Extracted);
     $font = $page->extractFont(Pdf\Font::FONT_TIMES_BOLD_ITALIC);
     $this->assertNull($font);
     $font = $pdf1->extractFont(Pdf\Font::FONT_TIMES_BOLD_ITALIC);
     $this->assertTrue($font instanceof Font\Extracted);
     $font = $pdf1->extractFont(Pdf\Font::FONT_TIMES_ROMAN);
     $this->assertNull($font);
     $pdf1->save(__DIR__ . '/_files/output1.pdf');
     unset($pdf1);
     $pdf2 = Pdf\PdfDocument::load(__DIR__ . '/_files/output1.pdf');
     $this->assertTrue($pdf2 instanceof Pdf\PdfDocument);
     unset($pdf2);
     unlink(__DIR__ . '/_files/output.pdf');
     unlink(__DIR__ . '/_files/output1.pdf');
 }
예제 #2
0
 /**
  * Draw a polygon in the rendering resource
  * @param string $text
  * @param float $size
  * @param array $position
  * @param string $font
  * @param integer $color
  * @param string $alignment
  * @param float $orientation
  */
 protected function _drawText($text, $size, $position, $font, $color, $alignment = 'center', $orientation = 0)
 {
     $page = $this->_resource->pages[$this->_page];
     $color = new Color\Rgb((($color & 0xff0000) >> 16) / 255.0, (($color & 0xff00) >> 8) / 255.0, ($color & 0xff) / 255.0);
     $page->setLineColor($color);
     $page->setFillColor($color);
     $page->setFont(Font::fontWithPath($font), $size * $this->_moduleSize * 1.2);
     $width = $this->widthForStringUsingFontSize($text, Font::fontWithPath($font), $size * $this->_moduleSize);
     $angle = pi() * $orientation / 180;
     $left = $position[0] * $this->_moduleSize + $this->_leftOffset;
     $top = $page->getHeight() - $position[1] * $this->_moduleSize - $this->_topOffset;
     switch ($alignment) {
         case 'center':
             $left -= $width / 2 * cos($angle);
             $top -= $width / 2 * sin($angle);
             break;
         case 'right':
             $left -= $width;
             break;
     }
     $page->rotate($left, $top, $angle);
     $page->drawText($text, $left, $top);
     $page->rotate($left, $top, -$angle);
 }