Skip to content

Commit a07f459

Browse files
authored
Merge pull request #134 from mikehaertl/131-add-to-string-method
Implement toString() method in Pdf
2 parents 0488dc7 + 78a2afd commit a07f459

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

src/Pdf.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,19 @@ public function send($filename=null,$inline=false)
518518
return true;
519519
}
520520

521+
/**
522+
* Get the raw PDF contents (triggers PDF creation).
523+
* @return string|bool the PDF content as a string or `false` if the PDF
524+
* wasn't created successfully.
525+
*/
526+
public function toString()
527+
{
528+
if (!$this->getCommand()->getExecuted() && !$this->execute()) {
529+
return false;
530+
}
531+
return file_get_contents($this->getTmpFile()->getFileName());
532+
}
533+
521534
/**
522535
* @return Command the command instance that executes pdftk
523536
*/

tests/PdfTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,17 @@ public function testCanGetDataFields()
552552
$this->assertEquals($this->formDataFieldsArray, $data->__toArray());
553553
}
554554

555+
public function testCanGetPdfContentAsString()
556+
{
557+
$document = $this->getDocument1();
558+
$file = $this->getOutFile();
559+
560+
$pdf = new Pdf($document);
561+
$this->assertTrue($pdf->saveAs($file));
562+
$this->assertFileExists($file);
563+
$this->assertEquals(file_get_contents($file), $pdf->toString());
564+
}
565+
555566
protected function getDocument1()
556567
{
557568
return __DIR__.'/files/document1.pdf';

0 commit comments

Comments
 (0)