1<?php 2/** 3 * Horde_Pdf test suite 4 * 5 * @license http://www.horde.org/licenses/lgpl21 6 * @category Horde 7 * @package Pdf 8 * @subpackage UnitTests 9 */ 10 11/** 12 * Horde_Pdf_test suite 13 * 14 * @category Horde 15 * @package Pdf 16 * @subpackage UnitTests 17 */ 18class Horde_Pdf_WriterTest extends PHPUnit_Framework_TestCase 19{ 20 public function testFactoryWithOptions() 21 { 22 $options = array('orientation' => 'L', 'unit' => 'pt', 'format' => 'A3'); 23 $pdf = new Horde_Pdf_Writer($options); 24 25 $this->assertEquals('L', $pdf->getDefaultOrientation()); 26 $this->assertEquals(841.89, $pdf->getFormatHeight()); 27 $this->assertEquals(1190.55, $pdf->getFormatWidth()); 28 } 29 30 public function testFactoryWithDefaults() 31 { 32 $pdf = new Horde_Pdf_Writer(); 33 34 $this->assertEquals('P', $pdf->getDefaultOrientation()); 35 $this->assertTrue(abs($pdf->getScale() - 2.8346456692913) < 0.000001); 36 $this->assertEquals(841.89, $pdf->getFormatHeight()); 37 $this->assertEquals(595.28, $pdf->getFormatWidth()); 38 } 39 40 public function testHelloWorldUncompressed() 41 { 42 $pdf = new Horde_Pdf_Writer(array('orientation' => 'P', 'format' => 'A4')); 43 $pdf->setInfo('CreationDate', $this->fixtureCreationDate()); 44 $pdf->open(); 45 $pdf->setCompression(false); 46 $pdf->addPage(); 47 $pdf->setFont('Courier', '', 8); 48 $pdf->text(100, 100, 'First page'); 49 $pdf->setFontSize(20); 50 $pdf->text(100, 200, 'HELLO WORLD!'); 51 $pdf->addPage(); 52 $pdf->setFont('Arial', 'BI', 12); 53 $pdf->text(100, 100, 'Second page'); 54 $actual = $pdf->getOutput(); 55 56 $expected = $this->fixture('hello_world_uncompressed'); 57 $this->assertEquals($expected, $actual); 58 } 59 60 public function testHelloWorldCompressed() 61 { 62 $pdf = new Horde_Pdf_Writer(array('orientation' => 'P', 'format' => 'A4')); 63 $pdf->setInfo('CreationDate', $this->fixtureCreationDate()); 64 $pdf->open(); 65 $pdf->setCompression(false); 66 $pdf->addPage(); 67 $pdf->setFont('Courier', '', 8); 68 $pdf->text(100, 100, 'First page'); 69 $pdf->setFontSize(20); 70 $pdf->text(100, 200, 'HELLO WORLD!'); 71 $pdf->addPage(); 72 $pdf->setFont('Arial', 'BI', 12); 73 $pdf->text(100, 100, 'Second page'); 74 $actual = $pdf->getOutput(); 75 76 $expected = $this->fixture('hello_world_compressed'); 77 $this->assertEquals($expected, $actual); 78 } 79 80 public function testAutoBreak() 81 { 82 $pdf = new Horde_Pdf_Writer(array('format' => array(50, 50), 'unit' => 'pt')); 83 $pdf->setInfo('CreationDate', $this->fixtureCreationDate()); 84 $pdf->setCompression(false); 85 $pdf->setMargins(0, 0); 86 87 $pdf->setAutoPageBreak(true); 88 $pdf->open(); 89 $pdf->addPage(); 90 $pdf->setFont('Courier', '', 10); 91 $pdf->write(10, "Hello\nHello\nHello\nHello\nHello\nHello\nHello\n"); 92 $actual = $pdf->getOutput(); 93 94 $expected = $this->fixture('auto_break'); 95 $this->assertEquals($expected, $actual); 96 } 97 98 99 public function testChangePage() 100 { 101 $pdf = new Horde_Pdf_Writer(array('format' => array(80, 80), 'unit' => 'pt')); 102 $pdf->setInfo('CreationDate', $this->fixtureCreationDate()); 103 $pdf->setCompression(false); 104 $pdf->setMargins(0, 0); 105 $pdf->open(); 106 107 // first page 108 $pdf->addPage(); 109 110 $pdf->setFont('Courier', '', 10); 111 $pdf->write(10, "Hello"); 112 113 // second page 114 $pdf->addPage(); 115 116 // back to first page again 117 $pdf->setPage(1); 118 $pdf->write(10, "Goodbye"); 119 120 // back to second page 121 $pdf->setPage(2); 122 123 $expected = $this->fixture('change_page'); 124 $this->assertEquals($expected, $pdf->getOutput()); 125 } 126 127 public function testTextColor() 128 { 129 $pdf = new Horde_Pdf_Writer(); 130 $pdf->setInfo('CreationDate', $this->fixtureCreationDate()); 131 $pdf->setCompression(false); 132 $pdf->open(); 133 $pdf->addPage(); 134 $pdf->setFont('Helvetica', 'B', 48); 135 $pdf->setDrawColor('rgb', 50, 0, 0); 136 $pdf->setTextColor('rgb', 0, 50, 0); 137 $pdf->setFillColor('rgb', 0, 0, 50); 138 $pdf->cell(0, 50, 'Hello Colors', 1, 0, 'C', 1); 139 $actual = $pdf->getOutput(); 140 141 $expected = $this->fixture('text_color'); 142 $this->assertEquals($expected, $actual); 143 } 144 145 public function testTextColorUsingHex() 146 { 147 $pdf = new Horde_Pdf_Writer(); 148 $pdf->setInfo('timestamp', $this->fixtureCreationDate()); 149 $pdf->setCompression(false); 150 $pdf->open(); 151 $pdf->addPage(); 152 $pdf->setFont('Helvetica', 'B', 48); 153 154 $pdf->setDrawColor('hex', '#F00'); 155 $pdf->setTextColor('hex', '#0F0'); 156 $pdf->setFillColor('hex', '#00F'); 157 158 $this->assertEquals('1.000 0.000 0.000 RG', $pdf->getDrawColor()); 159 $this->assertEquals('0.000 1.000 0.000 rg', $pdf->getTextColor()); 160 $this->assertEquals('0.000 0.000 1.000 rg', $pdf->getFillColor()); 161 } 162 163 public function testUnderline() 164 { 165 $pdf = new Horde_Pdf_Writer(array('orientation' => 'P', 'format' => 'A4')); 166 $pdf->setInfo('CreationDate', $this->fixtureCreationDate()); 167 $pdf->open(); 168 $pdf->setCompression(false); 169 $pdf->addPage(); 170 $pdf->setFont('Helvetica', 'U', 12); 171 $pdf->write(15, "Underlined\n"); 172 $pdf->write(15, 'Horde', 'http://www.horde.org'); 173 $actual = $pdf->getOutput(); 174 175 $expected = $this->fixture('underline'); 176 $this->assertEquals($expected, $actual); 177 } 178 179 /** 180 * PEAR Bug #12310 181 */ 182 public function testHeaderFooterStyles() 183 { 184 $pdf = new HeaderFooterStylesPdf(array( 185 'orientation' => 'P', 186 'unit' => 'mm', 187 'format' => 'A4', 188 )); 189 $pdf->setCompression(false); 190 $pdf->setInfo('title', '20000 Leagues Under the Seas'); 191 $pdf->setInfo('author', 'Jules Verne'); 192 $pdf->setInfo('CreationDate', $this->fixtureCreationDate()); 193 $pdf->printChapter(1, 'A RUNAWAY REEF', '20k_c1.txt'); 194 $pdf->printChapter(2, 'THE PROS AND CONS', '20k_c2.txt'); 195 $actual = $pdf->getOutput(); 196 197 $expected = $this->fixture('header_footer_styles'); 198 $this->assertEquals($expected, $actual); 199 } 200 201 /** 202 * Horde Bug #5964 203 */ 204 public function testLinks() 205 { 206 $pdf = new Horde_Pdf_Writer(array('orientation' => 'P', 'format' => 'A4')); 207 $pdf->setInfo('CreationDate', $this->fixtureCreationDate()); 208 $pdf->open(); 209 $pdf->setCompression(false); 210 $pdf->addPage(); 211 $pdf->setFont('Helvetica', 'U', 12); 212 $pdf->write(15, 'Horde', 'http://www.horde.org'); 213 $pdf->write(15, "\n"); 214 $link = $pdf->addLink(); 215 $pdf->write(15, 'here', $link); 216 $pdf->addPage(); 217 $pdf->setLink($link); 218 $pdf->image(__DIR__ . '/fixtures/horde-power1.png', 15, 15, 0, 0, '', 'http://pear.horde.org/'); 219 $actual = $pdf->getOutput(); 220 221 $expected = $this->fixture('links'); 222 $this->assertEquals($expected, $actual); 223 } 224 225 /** 226 * PEAR Bug #12310 227 */ 228 public function testCourierStyle() 229 { 230 $pdf = new Horde_Pdf_Writer(); 231 $pdf->setFont('courier', '', 10); 232 } 233 234 // Test Helpers 235 236 protected function fixture($name) 237 { 238 $filename = __DIR__ . "/fixtures/{$name}.pdf"; 239 $fixture = file_get_contents($filename); 240 241 $this->assertInternalType('string', $fixture); 242 return $fixture; 243 } 244 245 protected function fixtureCreationDate() 246 { 247 return 'D:20071105152947'; 248 } 249 250} 251 252class HeaderFooterStylesPdf extends Horde_Pdf_Writer 253{ 254 public function header() 255 { 256 $this->setFont('Arial', '', 15); 257 $w = $this->getStringWidth($this->_info['title']) + 6; 258 $this->setX((210 - $w) / 2); 259 $this->setDrawColor('rgb', 0/255, 80/255, 180/255); 260 $this->setFillColor('rgb', 230/255, 230/255, 0/255); 261 $this->setTextColor('rgb', 220/255, 50/255, 50/255); 262 $this->setLineWidth(1); 263 $this->cell($w, 9, $this->_info['title'], 1, 1, 'C', 1); 264 $this->newLine(10); 265 } 266 267 public function footer() 268 { 269 $this->setY(-15); 270 $this->setFont('Arial', 'I', 8); 271 $this->setTextColor('gray', 128/255); 272 $this->cell(0, 10, 'Page ' . $this->getPageNo(), 0, 0, 'C'); 273 } 274 275 public function chapterTitle($num, $label) 276 { 277 $this->setFont('Arial', '', 12); 278 $this->setFillColor('rgb', 200/255, 220/255, 255/255); 279 $this->cell(0, 6, "Chapter $num : $label", 0, 1, 'L', 1); 280 $this->newLine(4); 281 } 282 283 public function chapterBody($file) 284 { 285 $filename = __DIR__ . "/fixtures/$file"; 286 $text = file_get_contents($filename); 287 $this->setFont('Times', '', 12); 288 $this->multiCell(0, 5, $text); 289 $this->newLine(); 290 $this->setFont('', 'I'); 291 $this->cell(0, 5, '(end of extract)'); 292 } 293 294 public function printChapter($num, $title, $file) 295 { 296 $this->addPage(); 297 $this->chapterTitle($num, $title); 298 $this->chapterBody($file); 299 } 300 301} 302