forked from warrantgroup/MpdfPortBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Just adding some readability
- Loading branch information
Showing
1 changed file
with
83 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,88 +1,102 @@ | ||
<?php | ||
|
||
namespace TFox\MpdfPortBundle\Service; | ||
|
||
use Symfony\Component\HttpFoundation\Response; | ||
|
||
class MpdfService { | ||
private $addDefaultConstructorArgs = true; | ||
|
||
/** | ||
* Get an instance of mPDF class | ||
* @param array $constructorArgs arguments for mPDF constror | ||
* @return \mPDF | ||
*/ | ||
public function getMpdf($constructorArgs = array()) | ||
{ | ||
$allConstructorArgs = $constructorArgs; | ||
if($this->getAddDefaultConstructorArgs()) { | ||
$allConstructorArgs = array_merge(array('utf-8', 'A4'), $allConstructorArgs); | ||
} | ||
private $addDefaultConstructorArgs = true; | ||
|
||
/** | ||
* Get an instance of mPDF class | ||
* @param array $constructorArgs arguments for mPDF constror | ||
* @return \mPDF | ||
*/ | ||
public function getMpdf($constructorArgs = array()) | ||
{ | ||
$allConstructorArgs = $constructorArgs; | ||
if($this->getAddDefaultConstructorArgs()) { | ||
$allConstructorArgs = array_merge(array('utf-8', 'A4'), $allConstructorArgs); | ||
} | ||
|
||
$reflection = new \ReflectionClass('\mPDF'); | ||
$mpdf = $reflection->newInstanceArgs($allConstructorArgs); | ||
|
||
return $mpdf; | ||
} | ||
$reflection = new \ReflectionClass('\mPDF'); | ||
$mpdf = $reflection->newInstanceArgs($allConstructorArgs); | ||
|
||
/** | ||
* Returns a string which content is a PDF document | ||
*/ | ||
public function generatePdf($html, array $argOptions = array()) | ||
{ | ||
//Calculate arguments | ||
$defaultOptions = array( | ||
'constructorArgs' => array(), | ||
'writeHtmlMode' => null, | ||
'writeHtmlInitialise' => null, | ||
'writeHtmlClose' => null, | ||
'outputFilename' => '', | ||
'outputDest' => 'S', | ||
'mpdf' => null | ||
); | ||
$options = array_merge($defaultOptions, $argOptions); | ||
extract($options); | ||
return $mpdf; | ||
} | ||
|
||
/** | ||
* Returns a string which content is a PDF document | ||
*/ | ||
public function generatePdf($html, array $argOptions = array()) | ||
{ | ||
//Calculate arguments | ||
$defaultOptions = array( | ||
'constructorArgs' => array(), | ||
'writeHtmlMode' => null, | ||
'writeHtmlInitialise' => null, | ||
'writeHtmlClose' => null, | ||
'outputFilename' => '', | ||
'outputDest' => 'S', | ||
'mpdf' => null | ||
); | ||
|
||
$options = array_merge($defaultOptions, $argOptions); | ||
extract($options); | ||
|
||
if(null==$mpdf) | ||
$mpdf = $this->getMpdf($constructorArgs); | ||
|
||
//Add argguments to AddHtml function | ||
$writeHtmlArgs = array($writeHtmlMode, $writeHtmlInitialise, $writeHtmlClose); | ||
$writeHtmlArgs = array_filter($writeHtmlArgs, function($x) { return !is_null($x); }); | ||
$writeHtmlArgs['html'] = $html; | ||
if(null==$mpdf) { | ||
$mpdf = $this->getMpdf($constructorArgs); | ||
} | ||
|
||
@call_user_func_array(array($mpdf, 'WriteHTML'), $writeHtmlArgs); | ||
//Add argguments to AddHtml function | ||
$writeHtmlArgs = array($writeHtmlMode, $writeHtmlInitialise, $writeHtmlClose); | ||
$writeHtmlArgs = array_filter($writeHtmlArgs, function($x) { | ||
return !is_null($x); | ||
}); | ||
|
||
$writeHtmlArgs['html'] = $html; | ||
|
||
//Add arguments to Output function | ||
@call_user_func_array(array($mpdf, 'WriteHTML'), $writeHtmlArgs); | ||
|
||
//Add arguments to Output function | ||
$content = $mpdf->Output($outputFilename, $outputDest); | ||
return $content; | ||
} | ||
|
||
/** | ||
* Generates an instance of Response class with PDF document | ||
* @param unknown $argContent | ||
* @param array $argOptions | ||
*/ | ||
public function generatePdfResponse($html, array $argOptions = array()) | ||
{ | ||
$response = new Response(); | ||
$response->headers->set('Content-Type', 'application/pdf'); | ||
$content = $this->generatePdf($html, $argOptions); | ||
$response->setContent($content); | ||
return $content; | ||
} | ||
|
||
/** | ||
* Generates an instance of Response class with PDF document | ||
* @param string $html | ||
* @param array $argOptions | ||
*/ | ||
public function generatePdfResponse($html, array $argOptions = array()) | ||
{ | ||
$response = new Response(); | ||
$response->headers->set('Content-Type', 'application/pdf'); | ||
|
||
return $response; | ||
} | ||
$content = $this->generatePdf($html, $argOptions); | ||
$response->setContent($content); | ||
|
||
//Getters and setters | ||
return $response; | ||
} | ||
|
||
public function setAddDefaultConstructorArgs($val) | ||
{ | ||
$this->addDefaultConstructorArgs = $val; | ||
return $this; | ||
} | ||
/** | ||
* Set the defaults argumnets to the constructor. | ||
* @param $array $val | ||
*/ | ||
public function setAddDefaultConstructorArgs($val) | ||
{ | ||
$this->addDefaultConstructorArgs = $val; | ||
|
||
return $this; | ||
} | ||
|
||
public function getAddDefaultConstructorArgs() | ||
{ | ||
return $this->addDefaultConstructorArgs; | ||
} | ||
/** | ||
* Get defaults arguntments. | ||
*/ | ||
public function getAddDefaultConstructorArgs() | ||
{ | ||
return $this->addDefaultConstructorArgs; | ||
} | ||
} |