Skip to content

Commit

Permalink
Made package extendable and PHP version update
Browse files Browse the repository at this point in the history
  • Loading branch information
misterspelik committed Jul 28, 2023
1 parent 7a89720 commit ea9af48
Show file tree
Hide file tree
Showing 19 changed files with 437 additions and 186 deletions.
15 changes: 15 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
# IDE
.idea/
.project/
nbproject/
.buildpath/
.settings/
*.sublime-*

# OS
.DS_Store
*.AppleDouble
*.AppleDB
*.AppleDesktop

# tooling
vendor/
composer.lock
.phpunit.result.cache
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
# Laravel PDF: mPDF wrapper for Laravel 5
# Laravel PDF: mPDF wrapper for Laravel

> Easily generate PDF documents from HTML right inside of Laravel using this mPDF wrapper.

## Supported versions

Minimum supported version is Laravel 5

## Installation

Expand Down Expand Up @@ -155,6 +158,17 @@ body {
}
```

## Custom Styles
You can use your own styles in the generated PDFs. The css file have to be located in one folder, e.g. `/public/css/`. Add this to your configuration file (`/config/pdf.php`):

```php
return [
...
'defaultCssFile' => base_path('public/css/pdf.css'),
...
];
```

## Set Protection

To set protection, you just call the `SetProtection()` method and pass an array with permissions, an user password and an owner password.
Expand Down
16 changes: 11 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,16 @@
"test": "phpunit --colors=always"
},
"require": {
"php": ">=7.4",
"php": "^7.4 || ^8.0",
"ext-gd": "*",
"ext-imagick": "*",
"ext-mbstring": "*",
"misterspelik/mpdf": "^8.1.6"
},
"require-dev": {
"phpunit/phpunit": "^9.6.0",
"orchestra/testbench": "^8.5.0"
},
"autoload": {
"psr-4": {
"misterspelik\\LaravelPdf\\": "src/LaravelPdf"
Expand All @@ -20,6 +27,9 @@
"misterspelik\\LaravelPdf\\Test\\": "tests/"
}
},
"scripts": {
"test": "vendor/bin/phpunit"
},
"extra": {
"laravel": {
"providers": [
Expand All @@ -29,9 +39,5 @@
"PDF": "misterspelik\\LaravelPdf\\Facades\\Pdf"
}
}
},
"require-dev": {
"phpunit/phpunit": "^7.4",
"orchestra/testbench": "^3.7"
}
}
1 change: 1 addition & 0 deletions imagick/policy.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<policy domain="coder" rights="read|write" pattern="PDF" />
33 changes: 17 additions & 16 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="vendor/autoload.php"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
verbose="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
bootstrap="vendor/autoload.php" backupGlobals="false"
backupStaticAttributes="false"
colors="true"
verbose="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage>
<include>
<directory suffix=".php">src/</directory>
</include>
</coverage>
<testsuites>
<testsuite name="default">
<directory>tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">src/</directory>
</whitelist>
</filter>
</phpunit>
6 changes: 3 additions & 3 deletions src/LaravelPdf/Facades/Pdf.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace misterspelik\LaravelPdf\Facades;

use Illuminate\Support\Facades\Facade as BaseFacade;

class Pdf extends BaseFacade {
use Illuminate\Support\Facades\Facade;

class Pdf extends Facade
{
/**
* Get the registered name of the component.
*
Expand Down
38 changes: 38 additions & 0 deletions src/LaravelPdf/LaravelPdfFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace misterspelik\LaravelPdf;

use Config;
use misterspelik\LaravelPdf\Wrapper\PdfWrapper;
use misterspelik\LaravelPdf\Wrapper\PdfWrapperInterface;

class LaravelPdfFactory
{
/**
* @var string
*/
protected const PDF_WRAPPER_CONFIG_KEY = 'pdfWrapper';

/**
* @return \misterspelik\LaravelPdf\Wrapper\PdfWrapperInterface
*/
public function createPdfWrapper(): PdfWrapperInterface
{
$class = $this->getConfigKey(static::PDF_WRAPPER_CONFIG_KEY);
if ($class !== null) {
return new $class;
}

return new PdfWrapper();
}

/**
* @param string $key
*
* @return mixed
*/
protected function getConfigKey(string $key)
{
return Config::get('pdf.' . $key);
}
}
78 changes: 53 additions & 25 deletions src/LaravelPdf/Pdf.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,28 @@

use Config;
use Mpdf;
use Mpdf\Output\Destination;

/**
* Laravel PDF: mPDF wrapper for Laravel 5
*
* @package laravel-pdf
* @author Niklas Ravnsborg-Gjertsen
*/
class Pdf {

class Pdf implements PdfInterface
{
/**
* @var array|mixed
*/
protected $config = [];

public function __construct($html = '', $config = [])
/**
* @param string $html
* @param array $config
*
* @throws \Mpdf\MpdfException
*/
public function __construct(string $html = '', array $config = [])
{
$this->config = $config;

Expand All @@ -32,9 +42,14 @@ public function __construct($html = '', $config = [])
'margin_header' => $this->getConfig('margin_header'), // Set the page margins for the new document.
'margin_footer' => $this->getConfig('margin_footer'), // Set the page margins for the new document.
'orientation' => $this->getConfig('orientation'), // This attribute specifies the default page orientation of the new document if format is defined as an array. This value will be ignored if format is a string value.
'tempDir' => $this->getConfig('tempDir') // temporary directory
'tempDir' => $this->getConfig('tempDir'), // Temporary directory
];

$defaultCssFile = $this->getConfig('defaultCssFile'); // Set Default Style Sheet
if (file_exists($defaultCssFile)) {
$mpdf_config['defaultCssFile'] = $defaultCssFile;
}

// Handle custom fonts
$mpdf_config = $this->addCustomFontsConfig($mpdf_config);

Expand Down Expand Up @@ -66,7 +81,12 @@ public function __construct($html = '', $config = [])
$this->mpdf->WriteHTML($html);
}

protected function getConfig($key)
/**
* @param string $key
*
* @return mixed
*/
protected function getConfig(string $key)
{
if (isset($this->config[$key])) {
return $this->config[$key];
Expand All @@ -75,40 +95,45 @@ protected function getConfig($key)
return Config::get('pdf.' . $key);
}

protected function addCustomFontsConfig($mpdf_config)
/**
* @param array $mpdfConfig
*
* @return array
*/
protected function addCustomFontsConfig(array $mpdfConfig): array
{
if (!Config::has('pdf.font_path') || !Config::has('pdf.font_data')) {
return $mpdf_config;
return $mpdfConfig;
}

// Get default font configuration
$fontDirs = (new Mpdf\Config\ConfigVariables())->getDefaults()['fontDir'];
$fontData = (new Mpdf\Config\FontVariables())->getDefaults()['fontdata'];

// Merge default with custom configuration
$mpdf_config['fontDir'] = array_merge($fontDirs, [Config::get('pdf.font_path')]);
$mpdf_config['fontdata'] = array_merge($fontData, Config::get('pdf.font_data'));
$mpdfConfig['fontDir'] = array_merge($fontDirs, [Config::get('pdf.font_path')]);
$mpdfConfig['fontdata'] = array_merge($fontData, Config::get('pdf.font_data'));

return $mpdf_config;
return $mpdfConfig;
}

/**
* Encrypts and sets the PDF document permissions
*
* @param array $permisson Permissons e.g.: ['copy', 'print']
* @param array $permission Permissons e.g.: ['copy', 'print']
* @param string $userPassword User password
* @param string $ownerPassword Owner password
* @return static
*
*
* @return mixed
*/
public function setProtection($permisson, $userPassword = '', $ownerPassword = '')
public function setProtection(array $permission, string $userPassword = '', string $ownerPassword = '')
{
if (func_get_args()[2] === NULL) {
$ownerPassword = bin2hex(openssl_random_pseudo_bytes(8));
};

return $this->mpdf->SetProtection(
$permisson,
$permission,
$userPassword,
$ownerPassword
);
Expand All @@ -121,39 +146,42 @@ public function setProtection($permisson, $userPassword = '', $ownerPassword = '
*/
public function output()
{
return $this->mpdf->Output('', 'S');
return $this->mpdf->Output('', Destination::STRING_RETURN);
}

/**
* Save the PDF to a file
*
* @param $filename
* @return static
*
* @return void
*/
public function save($filename)
public function save(string $filename)
{
return $this->mpdf->Output($filename, 'F');
return $this->mpdf->Output($filename, Destination::FILE);
}

/**
* Make the PDF downloadable by the user
*
* @param string $filename
* @return \Symfony\Component\HttpFoundation\Response
*
* @return void
*/
public function download($filename = 'document.pdf')
public function download(string $filename = 'document.pdf')
{
return $this->mpdf->Output($filename, 'D');
return $this->mpdf->Output($filename, Destination::DOWNLOAD);
}

/**
* Return a response with the PDF to show in the browser
*
* @param string $filename
* @return \Symfony\Component\HttpFoundation\Response
*
* @return void
*/
public function stream($filename = 'document.pdf')
public function stream(string $filename = 'document.pdf')
{
return $this->mpdf->Output($filename, 'I');
return $this->mpdf->Output($filename, Destination::INLINE);
}
}
Loading

0 comments on commit ea9af48

Please sign in to comment.