Skip to content

Commit 3dee77b

Browse files
Initial Commit
1 parent 0516e39 commit 3dee77b

File tree

9 files changed

+361
-0
lines changed

9 files changed

+361
-0
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
vendor
2+
composer.lock
3+
.idea

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) [year] [fullname]
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

composer.json

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"name": "websolutionstuff/pdf-laravel",
3+
"description": "Websolutionstuff PDF support for Laravel 6, Laravel 7, Laravel 8, Laravel 9",
4+
"minimum-stability": "stable",
5+
"license": "MIT",
6+
"keywords": [
7+
"laravel",
8+
"websolutionstuff",
9+
"pdf"
10+
],
11+
"authors": [
12+
{
13+
"name": "websolutionstuff",
14+
"email": "[email protected]"
15+
}
16+
],
17+
"require": {
18+
"illuminate/support": "^6.0|^7.0|^8.0|^9.0",
19+
"tecnickcom/tcpdf": "6.2.*|6.3.*|6.4.*|6.5.*|6.6.*|dev-main"
20+
},
21+
"autoload": {
22+
"psr-4": {
23+
"Websolutionstuff\\PDF\\": "src/"
24+
}
25+
},
26+
"extra": {
27+
"laravel": {
28+
"providers": [
29+
"Websolutionstuff\\PDF\\ServiceProvider"
30+
],
31+
"aliases": {
32+
"PDF": "Websolutionstuff\\PDF\\Facades\\PDF"
33+
}
34+
}
35+
}
36+
}

config/pdf.php

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
return [
3+
'page_format' => 'A4',
4+
'page_orientation' => 'P',
5+
'page_units' => 'mm',
6+
'unicode' => true,
7+
'encoding' => 'UTF-8',
8+
'font_directory' => '',
9+
'image_directory' => '',
10+
'tcpdf_throw_exception' => false,
11+
'use_fpdi' => false,
12+
'use_original_header' => false,
13+
'use_original_footer' => false,
14+
'pdfa' => false,
15+
16+
// See more info at the tcpdf_config.php file in TCPDF (if you do not set this here, TCPDF will use it default)
17+
// https://raw.githubusercontent.com/tecnickcom/TCPDF/master/config/tcpdf_config.php
18+
];

src/Facades/PDF.php

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
namespace Websolutionstuff\PDF\Facades;
3+
4+
use Illuminate\Support\Facades\Facade;
5+
6+
class PDF extends Facade
7+
{
8+
protected static function getFacadeAccessor(){
9+
return 'pdf';
10+
}
11+
}

src/FpdiPDFHelper.php

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace Websolutionstuff\PDF;
4+
5+
use Illuminate\Support\Facades\Config;
6+
use setasign\Fpdi\TcpdfFpdi;
7+
8+
class FpdiPDFHelper extends TcpdfFpdi
9+
{
10+
protected $headerCallback;
11+
12+
protected $footerCallback;
13+
14+
public function Header()
15+
{
16+
if ($this->headerCallback != null && is_callable($this->headerCallback)) {
17+
$cb = $this->headerCallback;
18+
$cb($this);
19+
} else {
20+
if (Config::get('pdf.use_original_header')) {
21+
parent::Header();
22+
}
23+
}
24+
}
25+
26+
public function Footer()
27+
{
28+
if ($this->footerCallback != null && is_callable($this->footerCallback)) {
29+
$cb = $this->footerCallback;
30+
$cb($this);
31+
} else {
32+
if (Config::get('pdf.use_original_footer')) {
33+
parent::Footer();
34+
}
35+
}
36+
}
37+
38+
public function setHeaderCallback($callback)
39+
{
40+
$this->headerCallback = $callback;
41+
}
42+
43+
public function setFooterCallback($callback)
44+
{
45+
$this->footerCallback = $callback;
46+
}
47+
48+
}

src/PDF.php

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace Websolutionstuff\PDF;
4+
5+
use Illuminate\Support\Facades\Config;
6+
7+
class PDF
8+
{
9+
protected static $format;
10+
11+
protected $app;
12+
/** @var PDFHelper */
13+
protected $pdf;
14+
15+
public function __construct($app)
16+
{
17+
$this->app = $app;
18+
$this->reset();
19+
}
20+
21+
public function reset()
22+
{
23+
$class = Config::get('pdf.use_fpdi') ? FpdiPDFHelper::class : PDFHelper::class;
24+
25+
$this->pdf = new $class(
26+
Config::get('pdf.page_orientation', 'P'),
27+
Config::get('pdf.page_units', 'mm'),
28+
static::$format ? static::$format : Config::get('pdf.page_format', 'A4'),
29+
Config::get('pdf.unicode', true),
30+
Config::get('pdf.encoding', 'UTF-8'),
31+
false, // Diskcache is deprecated
32+
Config::get('pdf.pdfa', false)
33+
);
34+
}
35+
36+
public static function changeFormat($format)
37+
{
38+
static::$format = $format;
39+
}
40+
41+
public function __call($method, $args)
42+
{
43+
if (method_exists($this->pdf, $method)) {
44+
return call_user_func_array([$this->pdf, $method], $args);
45+
}
46+
throw new \RuntimeException(sprintf('the method %s does not exists in PDF', $method));
47+
}
48+
49+
public function setHeaderCallback($headerCallback)
50+
{
51+
$this->pdf->setHeaderCallback($headerCallback);
52+
}
53+
54+
public function setFooterCallback($footerCallback)
55+
{
56+
$this->pdf->setFooterCallback($footerCallback);
57+
}
58+
}

src/PDFHelper.php

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
namespace Websolutionstuff\PDF;
4+
5+
use Illuminate\Support\Facades\Config;
6+
7+
class PDFHelper extends \TCPDF
8+
{
9+
protected $headerCallback;
10+
11+
protected $footerCallback;
12+
13+
public function Header()
14+
{
15+
if ($this->headerCallback != null && is_callable($this->headerCallback)) {
16+
$cb = $this->headerCallback;
17+
$cb($this);
18+
} else {
19+
if (Config::get('pdf.use_original_header')) {
20+
parent::Header();
21+
}
22+
}
23+
}
24+
25+
public function Footer()
26+
{
27+
if ($this->footerCallback != null && is_callable($this->footerCallback)) {
28+
$cb = $this->footerCallback;
29+
$cb($this);
30+
} else {
31+
if (Config::get('pdf.use_original_footer')) {
32+
parent::Footer();
33+
}
34+
}
35+
}
36+
37+
public function setHeaderCallback($callback)
38+
{
39+
$this->headerCallback = $callback;
40+
}
41+
42+
public function setFooterCallback($callback)
43+
{
44+
$this->footerCallback = $callback;
45+
}
46+
47+
public function addTOC($page = '', $numbersfont = '', $filler = '.', $toc_name = 'TOC', $style = '', $color = array(0, 0, 0))
48+
{
49+
// sort bookmarks before generating the TOC
50+
parent::sortBookmarks();
51+
52+
parent::addTOC($page, $numbersfont, $filler, $toc_name, $style, $color);
53+
}
54+
55+
public function addHTMLTOC($page = '', $toc_name = 'TOC', $templates = array(), $correct_align = true, $style = '', $color = array(0, 0, 0))
56+
{
57+
// sort bookmarks before generating the TOC
58+
parent::sortBookmarks();
59+
60+
parent::addHTMLTOC($page, $toc_name, $templates, $correct_align, $style, $color);
61+
}
62+
63+
public function checkPageBreak($h = 0, $y = null, $addpage = true)
64+
{
65+
parent::checkPageBreak($h, $y, $addpage);
66+
}
67+
68+
public function getPageBreakTrigger()
69+
{
70+
return $this->PageBreakTrigger;
71+
}
72+
}

src/ServiceProvider.php

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
3+
namespace Websolutionstuff\PDF;
4+
5+
use Illuminate\Support\Facades\Config;
6+
use Illuminate\Support\ServiceProvider as LaravelServiceProvider;
7+
8+
/**
9+
* Class ServiceProvider
10+
* @version 1.0
11+
* @package Websolutionstuff\PDF
12+
*/
13+
class ServiceProvider extends LaravelServiceProvider
14+
{
15+
protected $constantsMap = [
16+
'K_PATH_MAIN' => 'path_main',
17+
'K_PATH_URL' => 'path_url',
18+
'K_PATH_FONTS' => 'font_directory',
19+
'K_PATH_IMAGES' => 'image_directory',
20+
'PDF_HEADER_LOGO' => 'header_logo',
21+
'PDF_HEADER_LOGO_WIDTH' => 'header_logo_width',
22+
'K_PATH_CACHE' => 'path_cache',
23+
'K_BLANK_IMAGE' => 'blank_image',
24+
'PDF_PAGE_FORMAT' => 'page_format',
25+
'PDF_PAGE_ORIENTATION' => 'page_orientation',
26+
'PDF_CREATOR' => 'creator',
27+
'PDF_AUTHOR' => 'author',
28+
'PDF_HEADER_TITLE' => 'header_title',
29+
'PDF_HEADER_STRING' => 'header_string',
30+
'PDF_UNIT' => 'page_units',
31+
'PDF_MARGIN_HEADER' => 'margin_header',
32+
'PDF_MARGIN_FOOTER' => 'margin_footer',
33+
'PDF_MARGIN_TOP' => 'margin_top',
34+
'PDF_MARGIN_BOTTOM' => 'margin_bottom',
35+
'PDF_MARGIN_LEFT' => 'margin_left',
36+
'PDF_MARGIN_RIGHT' => 'margin_right',
37+
'PDF_FONT_NAME_MAIN' => 'font_name_main',
38+
'PDF_FONT_SIZE_MAIN' => 'font_size_main',
39+
'PDF_FONT_NAME_DATA' => 'font_name_data',
40+
'PDF_FONT_SIZE_DATA' => 'font_size_data',
41+
'PDF_FONT_MONOSPACED' => 'foto_monospaced',
42+
'PDF_IMAGE_SCALE_RATIO' => 'image_scale_ratio',
43+
'HEAD_MAGNIFICATION' => 'head_magnification',
44+
'K_CELL_HEIGHT_RATIO' => 'cell_height_ratio',
45+
'K_TITLE_MAGNIFICATION' => 'title_magnification',
46+
'K_SMALL_RATIO' => 'small_ratio',
47+
'K_THAI_TOPCHARS' => 'thai_topchars',
48+
'K_TCPDF_CALLS_IN_HTML' => 'tcpdf_calls_in_html',
49+
'K_TCPDF_THROW_EXCEPTION_ERROR' => 'tcpdf_throw_exception',
50+
'K_TIMEZONE' => 'timezone',
51+
];
52+
53+
/**
54+
* Register the service provider.
55+
*
56+
* @return void
57+
*/
58+
public function register()
59+
{
60+
$configPath = dirname(__FILE__) . '/../config/pdf.php';
61+
$this->mergeConfigFrom($configPath, 'pdf');
62+
$this->app->singleton('pdf', function ($app) {
63+
return new PDF($app);
64+
});
65+
}
66+
67+
public function boot()
68+
{
69+
if (!defined('K_TCPDF_EXTERNAL_CONFIG')) {
70+
define('K_TCPDF_EXTERNAL_CONFIG', true);
71+
}
72+
foreach ($this->constantsMap as $key => $value) {
73+
$value = Config::get('pdf.' . $value, null);
74+
if (!is_null($value) && !defined($key)) {
75+
if (is_string($value) && strlen($value) == 0) {
76+
continue;
77+
}
78+
define($key, $value);
79+
}
80+
}
81+
$configPath = dirname(__FILE__) . '/../config/pdf.php';
82+
if (function_exists('config_path')) {
83+
$targetPath = config_path('pdf.php');
84+
} else {
85+
$targetPath = app()->basePath() . '/config/pdf.php';
86+
}
87+
$this->publishes(array($configPath => $targetPath), 'config');
88+
}
89+
90+
public function provides()
91+
{
92+
return ['pdf'];
93+
}
94+
}

0 commit comments

Comments
 (0)