Skip to content

Commit

Permalink
Update MpdfService.php
Browse files Browse the repository at this point in the history
Just adding some readability
  • Loading branch information
pabgaran authored Apr 3, 2017
1 parent 0641960 commit 589f07f
Showing 1 changed file with 83 additions and 69 deletions.
152 changes: 83 additions & 69 deletions Service/MpdfService.php
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;
}
}

0 comments on commit 589f07f

Please sign in to comment.