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.
Merge pull request warrantgroup#1 from tasmanianfox/master
mpdf has not been made a service
- Loading branch information
Showing
7 changed files
with
189 additions
and
133 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,4 +1,4 @@ | ||
CREDITS | ||
============================= | ||
The main component of this bundle, mPDF, was developed by Ian Back. | ||
mPDF webpage is http://www.mpdf1.com/ | ||
mPDF webpage is https://mpdf.github.io/ |
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
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,3 +1,5 @@ | ||
services: | ||
tfox.mpdfport: | ||
class: TFox\MpdfPortBundle\Service\MpdfService | ||
t_fox_mpdf_port.pdf: | ||
class: TFox\MpdfPortBundle\Service\PDFService | ||
arguments: ["%kernel.cache_dir%"] | ||
public: true |
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
namespace TFox\MpdfPortBundle\Response; | ||
|
||
use Symfony\Component\Config\Definition\Exception\Exception; | ||
use Symfony\Component\HttpFoundation\Response; | ||
|
||
class PDFResponse extends Response | ||
{ | ||
public function __construct($content, $filename = null, int $status = 200, array $headers = array()) | ||
{ | ||
$headers = array_merge($headers, array('Content-Type' => 'application/pdf')); | ||
if(false == is_null($filename)) { | ||
$headers['Content-Disposition'] = sprintf('attachment; filename="%s"', $filename); | ||
} | ||
parent::__construct($content, $status, $headers); | ||
} | ||
|
||
} |
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,106 @@ | ||
<?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(array_merge(array('utf-8', 'A4'), $allConstructorArgs)); | ||
} | ||
|
||
$reflection = new \ReflectionClass('Mpdf\Mpdf'); | ||
$mpdf = $reflection->newInstanceArgs($allConstructorArgs); | ||
|
||
$reflection = new \ReflectionClass('\mPDF'); | ||
$mpdf = $reflection->newInstanceArgs($allConstructorArgs); | ||
|
||
return $mpdf; | ||
} | ||
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); | ||
/** | ||
* Returns a string which content is a PDF document | ||
* @param string $html Html content | ||
* @param array $argOptions Options for the constructor. | ||
* | ||
* @return PDF file. | ||
*/ | ||
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); | ||
} | ||
|
||
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; | ||
@call_user_func_array(array($mpdf, 'WriteHTML'), $writeHtmlArgs); | ||
//Add arguments to Output function | ||
//Add arguments to AddHtml function | ||
$writeHtmlArgs = array($writeHtmlMode, $writeHtmlInitialise, $writeHtmlClose); | ||
$writeHtmlArgs = array_filter($writeHtmlArgs, function($x) { | ||
return !is_null($x); | ||
}); | ||
|
||
$writeHtmlArgs['html'] = $html; | ||
|
||
@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 $response; | ||
} | ||
return $content; | ||
} | ||
|
||
//Getters and setters | ||
/** | ||
* 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'); | ||
|
||
$content = $this->generatePdf($html, $argOptions); | ||
$response->setContent($content); | ||
|
||
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; | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
namespace TFox\MpdfPortBundle\Service; | ||
|
||
use Mpdf\Mpdf; | ||
use Mpdf\Output\Destination; | ||
use Symfony\Component\OptionsResolver\OptionsResolver; | ||
|
||
class PDFService { | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $cacheDir; | ||
|
||
/** | ||
* MpdfService constructor. | ||
* @param string $cacheDir | ||
*/ | ||
public function __construct(string $cacheDir) | ||
{ | ||
$this->cacheDir = $cacheDir; | ||
} | ||
|
||
/** | ||
* @param string $html | ||
* @param array $options | ||
* @return string | ||
* @throws \Mpdf\MpdfException | ||
*/ | ||
public function generatePdf($html, $options = array()) | ||
{ | ||
$resolver = new OptionsResolver(); | ||
$resolver->setDefaults(array( | ||
'mode' => 'utf-8', | ||
'format' => 'A4', | ||
'tempDir' => $this->cacheDir | ||
)); | ||
$options = $resolver->resolve($options); | ||
$mpdf = new Mpdf($options); | ||
$mpdf->WriteHTML($html); | ||
|
||
return $mpdf->Output(null, Destination::STRING_RETURN); | ||
} | ||
} |
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