Skip to content

Commit

Permalink
update mpdf version deps
Browse files Browse the repository at this point in the history
  • Loading branch information
SinuZite committed Apr 28, 2017
0 parents commit 451b980
Show file tree
Hide file tree
Showing 9 changed files with 370 additions and 0 deletions.
16 changes: 16 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Eclipse Files
/.classpath
/.buildpath
/.project
/.settings/

/vendor/

# PHPUnit
/phpunit.xml

# Composer PHAR
/composer.phar

# Composer lock
/composer.lock
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2016 Salah Abdelkader Seif Eddine

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
sasedev/mpdf-bundle
=========================

Mpdf Bundle for Symfony2 (3.0)
36 changes: 36 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"name" : "sasedev/mpdf-bundle",
"description" : "Mpdf for Symfony2 (3.0)",
"require" : {
"php" : ">=5.5",
"mpdf/mpdf" : "~6.0|~7.0",
"symfony/twig-bridge" : "~2.8|~3.0",
"symfony/monolog-bridge" : "~2.8|~3.0",
"symfony/http-foundation" : "~2.8|~3.0",
"symfony/config" : "~2.8|~3.0",
"symfony/dependency-injection" : "~2.8|~3.0",
"symfony/yaml" : "~2.8|~3.0",
"symfony/http-kernel" : "~2.8|~3.0"
},
"license" : "MIT",
"keywords" : [
"symfony2",
"form"
],
"extra" : {
"branch-alias" : {
"dev-master" : "1.0.x-dev"
}
},
"autoload" : {
"psr-4" : {
"" : "src"
}
},
"type" : "symfony-bundle",
"authors" : [{
"name" : "Salah Abdelkader Seif Eddine",
"email" : "[email protected]"
}
]
}
31 changes: 31 additions & 0 deletions src/Sasedev/MpdfBundle/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
namespace Sasedev\MpdfBundle\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

/**
* This is the class that validates and merges configuration from your app/config files.
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/configuration.html}
*/
class Configuration implements ConfigurationInterface
{

/**
*
* {@inheritdoc}
*
*/
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
// $rootNode = $treeBuilder->root('sasedev_mpdf');
$treeBuilder->root('sasedev_mpdf');

// Here you should define the parameters that are allowed to
// configure your bundle. See the documentation linked above for
// more information on that topic.

return $treeBuilder;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
namespace Sasedev\MpdfBundle\DependencyInjection;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;

/**
* This is the class that loads and manages your bundle configuration.
*
* @link http://symfony.com/doc/current/cookbook/bundles/extension.html
*/
class SasedevMpdfExtension extends Extension
{

/**
*
* {@inheritdoc}
*
*/
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
// $config = $this->processConfiguration($configuration, $configs);
$this->processConfiguration($configuration, $configs);

$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
$loader->load('services.yml');
}
}
7 changes: 7 additions & 0 deletions src/Sasedev/MpdfBundle/Resources/config/services.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
parameters:
sasedev_mpdf.class: Sasedev\MpdfBundle\Service\MpdfService

services:
sasedev_mpdf:
class: "%sasedev_mpdf.class%"
arguments: ["@templating", "@logger", "%kernel.cache_dir%"]
8 changes: 8 additions & 0 deletions src/Sasedev/MpdfBundle/SasedevMpdfBundle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
namespace Sasedev\MpdfBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class SasedevMpdfBundle extends Bundle
{
}
216 changes: 216 additions & 0 deletions src/Sasedev/MpdfBundle/Service/MpdfService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
<?php
namespace Sasedev\MpdfBundle\Service;

use mPDF;
use Symfony\Bridge\Monolog\Logger;
use Symfony\Bridge\Twig\TwigEngine;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\StreamedResponse;

/**
*
* @author sasedev <[email protected]>
*/
class MpdfService
{

/**
*
* @var mPDF $mpdf
*/
protected $mpdf;

/**
*
* @var TwigEngine $renderer;
*/
protected $renderer;

/**
*
* @var Logger $logger
*/
protected $logger;

/**
*
* @var float
*/
protected $start_time;

/**
*
* @param
* $renderer
* @param
* $logger
* @param
* $cache_dir
*/
public function __construct($renderer, $logger, $cache_dir)
{
// vendor folder probably doesn't have write access,
// so put the temp folder under the cache folder which should have write access
$tmp_folder = $cache_dir . '/tmp/';
if (!is_dir($tmp_folder)) {
mkdir($tmp_folder);
}
$font_folder = $cache_dir . '/ttfontdata/';
if (!is_dir($font_folder)) {
mkdir($font_folder);
}
if (!defined('_MPDF_TEMP_PATH')) {
define("_MPDF_TEMP_PATH", $tmp_folder);
}
if (!defined('_MPDF_TTFONTDATAPATH')) {
define("_MPDF_TTFONTDATAPATH", $font_folder);
}
$this->renderer = $renderer;
$this->logger = $logger;
}

/**
*
* @param string $mode
* @param string $format
* @param string $default_font_size
* @param string $default_font
* @param string $margin_left
* @param string $margin_right
* @param string $margin_top
* @param string $margin_bottom
* @param string $margin_header
* @param string $margin_footer
* @param string $orientation
* @see mPDF
*/
public function init($mode = '', $format = '', $default_font_size = '', $default_font = '', $margin_left = '', $margin_right = '', $margin_top = '', $margin_bottom = '', $margin_header = '', $margin_footer = '', $orientation = '')
{
$this->start_time = microtime(true);
$this->mpdf = new mPDF($mode, $format, $default_font_size, $default_font, $margin_left, $margin_right, $margin_top, $margin_bottom, $margin_header, $margin_footer, $orientation);
$this->logger->debug("sasedev_mpdf: Using temp folder " . _MPDF_TEMP_PATH);
$this->logger->debug("sasedev_mpdf: Using font folder " . _MPDF_TTFONTDATAPATH);
}

/**
* Set property of mPDF
*
* @param
* $name
* @param
* $value
*/
public function setProperty($name, $value)
{
$this->mpdf->{$name} = $value;
}

/**
* Call method of mPDF
*
* @param
* $name
* @param array $data
*/
public function callMethod($name, array $data)
{
return call_user_func_array(array(
$this->mpdf,
$name
), $data);
}

/**
* Get instance of mPDF object
*
* @return mPDF
*/
public function getMpdf()
{
return $this->mpdf;
}

/**
* Set html
*
* @param string $html
*/
public function setHtml($html)
{
if (!$this->mpdf) {
$this->init();
}
$this->mpdf->WriteHTML($html);
}

/**
* Renders and set as html the template with the given context
*
* @param
* $template
* @param array $data
*/
public function useTwigTemplate($template, array $data = array())
{
$html = $this->renderer->render($template, $data);
$this->setHtml($html);
return $this;
}

/**
* Generate pdf document and return it as string
*
* @return string
*/
public function generate()
{
if (!$this->mpdf) {
$this->init();
}
// Better to avoid having mpdf set any headers as these can interfer with symfony responses
$output = $this->mpdf->Output('', 'S');
$time = microtime(true) - $this->start_time;
$this->logger->debug("sasedev_mpdf: pdf generation took " . $time . " seconds");
return $output;
}

/**
* Generate pdf document and returns it as Response object
*
* @param string $filename
* @return Response
*/
public function generateInlineFileResponse($filename)
{
$headers = array(
'content-type' => 'application/pdf',
'content-disposition' => sprintf('inline; filename="%s"', $filename)
);
$content = $this->generate();
return new Response($content, 200, $headers);
}

/**
* Generate pdf document and returns it as Response object
*
* @param string $filename
* @return Response
*/
public function generateStreamedResponse($status = 200, $headers = array())
{
$content = $this->generate();
return new StreamedResponse(function () use ($content) {
file_put_contents('php://output', $content);
}, $status, $headers);
}

/**
* Return mPDF version
*
* @return null|string
*/
public function getVersion()
{
return defined('mPDF_VERSION') ? mPDF_VERSION : null;
}
}

0 comments on commit 451b980

Please sign in to comment.