Skip to content

Commit

Permalink
Merge pull request warrantgroup#1 from tasmanianfox/master
Browse files Browse the repository at this point in the history
mpdf has not been made a service
  • Loading branch information
yashmika authored Oct 11, 2018
2 parents f2165cb + 9ce3dca commit 37b817e
Show file tree
Hide file tree
Showing 7 changed files with 189 additions and 133 deletions.
2 changes: 1 addition & 1 deletion CREDITS.txt
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/
82 changes: 27 additions & 55 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
Installation
==============================================
### Using Composer (Symfony 2.1+)

* Add a new line to your `composer.json` file:
<pre><code>"require": {
...

"tfox/mpdf-port-bundle": "1.2.*"
}
</code></pre>
### Using Composer (Symfony 2.x, Symfony 3.0.x)

* Run a command
<pre><code>php composer.phar update
<pre><code>composer require tfox/mpdf-port-bundle
</code></pre>

* Add a new line to `app/AppKernel.php`:
Expand Down Expand Up @@ -47,60 +39,40 @@ A Quick Start guide
==============================================
### How to create a Response object
This small example creates a PDF document with format A4 and portrait orientation:
<pre><code>$mpdfService = $this->get('tfox.mpdfport');
$html = "<html><head></head><body>Hello World!</body></html>";
$response = $mpdfService->generatePdfResponse($html);
<pre><code>public function indexAction()
{
return new \TFox\MpdfPortBundle\Response\PDFResponse($this->getMpdfService()->generatePdf('Hello World'));
}

/**
* @return \TFox\MpdfPortBundle\Service\PDFService
*/
private function getMpdfService()
{
return $this->get('t_fox_mpdf_port.pdf');
}
</code></pre>

### Generate a variable with PDF content
Sometimes it is necessary to get a variabe which content is PDF document. Obviously, you might generate a response from the previous example and then call a method:
<pre><code>$response->getContent()
</code></pre>
But there is a shorter way to get a raw content:
<pre><code>$mpdfService = $this->get('tfox.mpdfport');
$html = "<html><head></head><body>Hello World!</body></html>";
$content = $mpdfService->generatePdf($html);
Sometimes it is necessary to get a variable which content is PDF document.
<pre><code>$myVar = $this->getMpdfService()->generatePdf('Hello World');
</code></pre>

### How to get an instance of \mPDF class
If you would like to work with mPDF class itself, you can use a getMpdf method:
<pre><code>$mpdfService = $this->get('tfox.mpdfport');
$mPDF = $mpdfService->getMpdf();
</code></pre>

<pre><code>$mpdf = new \Mpdf\Mpdf();</code></pre>


Warning
Additional options
==============================================
* By default the bundle adds the two attributes 'utf-8' and 'A4' to the mPDF class constructor. To turn off these options, use the `setAddDefaultConstructorArgs` method:
<pre><code>$mpdfService->setAddDefaultConstructorArgs(false);
</code></pre>

* As the bundle inserts the first two arguments to the mPDF constructor by default, additional constructor arguments should start from the 3rd argument (default_font_size).

* If the `setAddDefaultConstructorArgs(false)` method is called, additional arguments for constructor should start from the first one (mode).
Additional options might be passed via the second argument:

<pre><code>public function indexAction()
{
return new \TFox\MpdfPortBundle\Response\PDFResponse($this->getMpdfService()->generatePdf('Hello World', array(
'format' => 'A4-L' // A4 page, landscape orientation
)));
}
</code></pre>

Detailed description is available on official manual page: https://mpdf.github.io/

Additional arguments
==============================================
As the bundle uses methods of mPDF class, some additional parameters can be added to these methods. There are 3 mPDF methods used in the bundle:
* Constructor. Documentation: http://mpdf1.com/manual/index.php?tid=184
* WriteHTML. Documentation: http://mpdf1.com/manual/index.php?tid=121
* Output. Documentation: http://mpdf1.com/manual/index.php?tid=125

To pass additional arguments, an array with arguments should be created:
<pre><code>$arguments = array(
'constructorArgs' => array(), //Constructor arguments. Numeric array. Don't forget about points 2 and 3 in Warning section!
'writeHtmlMode' => null, //$mode argument for WriteHTML method
'writeHtmlInitialise' => null, //$mode argument for WriteHTML method
'writeHtmlClose' => null, //$close argument for WriteHTML method
'outputFilename' => null, //$filename argument for Output method
'outputDest' => null //$dest argument for Output method
);
</code></pre>
It is NOT necessary to have all the keys in array.
This array might be passed to the `generatePdf` and `generatePdfResponse` methods as the second argument:
<pre><code>$mpdfService->generatePdf($html, $arguments);
$mpdfService->generatePdfResponse($html, $arguments);
</code></pre>
6 changes: 4 additions & 2 deletions Resources/config/services.yml
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
18 changes: 18 additions & 0 deletions Response/PDFResponse.php
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);
}

}
162 changes: 90 additions & 72 deletions Service/MpdfService.php
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;
}
}
45 changes: 45 additions & 0 deletions Service/PDFService.php
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);
}
}
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
"MIT"
],
"require" : {
"mpdf/mpdf" : ">=5.7",
"symfony/framework-bundle" : "~2.1"
"mpdf/mpdf" : "^7.0",
"symfony/framework-bundle" : ">=2.3",
"symfony/options-resolver": ">=2.3"
},
"require-dev" : {
"symfony/security-bundle" : "*",
Expand All @@ -34,7 +35,7 @@
"target-dir" : "TFox/MpdfPortBundle",
"extra" : {
"branch-alias" : {
"dev-master" : "1.2-dev"
"dev-master" : "2.0-dev"
}
}
}

0 comments on commit 37b817e

Please sign in to comment.