This commit is contained in:
RafficMohammed
2023-02-17 13:25:50 +05:30
parent 2381fd7cf5
commit 67950fc78f
891 changed files with 102300 additions and 138477 deletions

View File

@@ -1,3 +1,4 @@
composer.lock
/vendor
.idea
.phpunit.result.cache

View File

@@ -1,9 +1,8 @@
language: php
php:
- 5.4
- 5.5
- 5.6
- hhvm
- 7.0
- 7.1
before_script:
- composer install --prefer-source

View File

@@ -1,14 +1,13 @@
# pdf-laravel5
DOMPDF module for Laravel 5
DOMPDF module for Laravel 5. Export your views as PDFs - with css support.
[![Build Status](https://api.travis-ci.org/vsmoraes/pdf-laravel5.svg)](https://travis-ci.org/vsmoraes/pdf-laravel5)
[![License](https://poser.pugx.org/vsmoraes/laravel-pdf/license.svg)](https://packagist.org/packages/vsmoraes/laravel-pdf)
[![Build Status](https://api.travis-ci.org/vsmoraes/pdf-laravel5.svg)](https://travis-ci.org/vsmoraes/pdf-laravel5) [![Latest Stable Version](https://poser.pugx.org/vsmoraes/laravel-pdf/v/stable)](https://packagist.org/packages/vsmoraes/laravel-pdf) [![Total Downloads](https://poser.pugx.org/vsmoraes/laravel-pdf/downloads)](https://packagist.org/packages/vsmoraes/laravel-pdf) [![Latest Unstable Version](https://poser.pugx.org/vsmoraes/laravel-pdf/v/unstable)](https://packagist.org/packages/vsmoraes/laravel-pdf) [![License](https://poser.pugx.org/vsmoraes/laravel-pdf/license)](https://packagist.org/packages/vsmoraes/laravel-pdf)
## Instalation
Add:
```
"vsmoraes/laravel-pdf": "dev-master"
"vsmoraes/laravel-pdf": "^2.0"
```
To your `composer.json`
@@ -19,7 +18,7 @@ composer require vsmoraes/laravel-pdf
Then add:
```php
'Vsmoraes\Pdf\PdfServiceProvider'
Vsmoraes\Pdf\PdfServiceProvider::class
```
To the `providers` array on your `config/app.php`
@@ -33,7 +32,7 @@ To the `aliases` array on yout `config/app.php` in order to enable the PDF facad
## Usage
```php
$router->get('/pdf/view', function() {
Route::get('/pdf/view', function() {
$html = view('pdfs.example')->render();
return PDF::load($html)->show();
@@ -42,16 +41,36 @@ $router->get('/pdf/view', function() {
### Force download
```php
$router->get('/pdf/download', function() {
Route::get('/pdf/download', function() {
$html = view('pdfs.example')->render();
return PDF::load($html)->download();
});
```
### Return PDF as string
```php
Route::get('/pdf/output', function() {
$html = view('pdfs.example')->render();
return PDF::load($html)
->output();
});
```
### Set paper size and orientation
```php
Route::get('/pdf/output', function() {
$html = view('pdfs.example')->render();
return PDF::load($html, 'A4', 'landscape')
->output();
});
```
### Output to a file
```php
$router->get('/pdf/output', function() {
Route::get('/pdf/output', function() {
$html = view('pdfs.example')->render();
PDF::load($html)
@@ -87,3 +106,19 @@ class HomeController extends BaseControler
}
}
```
## Configuration
Dompdf allows you to configure a bunch of things on your PDF file. In previous versions we used to accomplish this through environment vars, now you can change this configuration keys on the fly:
```php
Route::get('/pdf/view', function() {
$html = view('pdfs.example')->render();
$defaultOptions = PDF::getOptions();
$defaultOptions->setDefaultFont('Courier');
return PDF::setOptions($defaultOptions)->load($html)->download();
});
```
For the complete configuration reference: [Dompdf options](https://github.com/dompdf/dompdf/blob/master/src/Options.php)

View File

@@ -1,17 +1,18 @@
{
"name": "vsmoraes/laravel-pdf",
"description": "DOMPDF module for Laravel 5",
"keywords": ["laravel", "dompdf", "pdf"],
"keywords": ["laravel", "dompdf", "pdf", "html", "css", "html2pdf"],
"license": "MIT",
"authors": [
{ "name": "Vinicius Moraes", "email": "vinicius@archlinux.com.br" }
],
"require": {
"php": ">=5.4.0",
"dompdf/dompdf": "0.6.*"
"php": ">=8.1",
"dompdf/dompdf": "0.8.*|0.9.*|1.0.*|1.1.*",
"illuminate/http": "^9.0"
},
"require-dev": {
"phpunit/phpunit": "4.4.*"
"phpunit/phpunit": "^9.5.10"
},
"autoload": {
"psr-4": {

View File

@@ -1,8 +1,16 @@
<?xml version="1.0" encoding="utf-8" ?>
<phpunit colors="true" bootstrap="tests/bootstrap.php">
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="tests/bootstrap.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false">
<testsuites>
<testsuite name="DomPdf tests">
<directory suffix=".php">tests/</directory>
<testsuite name="Dompdf Test Suite">
<directory>./tests/src</directory>
</testsuite>
</testsuites>
</phpunit>

View File

@@ -1,10 +1,13 @@
<?php
namespace Vsmoraes\Pdf;
use Dompdf\Options;
use Illuminate\Http\Response;
class Dompdf implements Pdf
{
CONST DOWNLOAD_FILENAME = 'dompdf_out.pdf';
/**
* DOMPDF instance
*
@@ -17,26 +20,24 @@ class Dompdf implements Pdf
*
* @var string
*/
protected $filename = 'dompdf_out.pdf';
protected $filename;
/**
* Inject the DOMPDF object
*
* @param \DOMPDF $dompdf
* @param \Dompdf\Dompdf $dompdf
*/
public function __construct(\DOMPDF $dompdf)
public function __construct(\Dompdf\Dompdf $dompdf)
{
$this->dompdfInstance = $dompdf;
}
/**
* {@inheritdoc}
*/
public function load($html, $size = 'A4', $orientation = 'portrait')
public function load($html, $size = Pdf::DEFAULT_SIZE, $orientation = Pdf::DEFAULT_ORIENTATION)
{
$this->dompdfInstance->load_html($html);
$this->dompdfInstance->loadHtml($html);
$this->setPaper($size, $orientation);
return $this;
@@ -45,16 +46,11 @@ class Dompdf implements Pdf
/**
* {@inheritdoc}
*/
public function filename($filename = null)
public function filename($filename)
{
if ($filename) {
$this->filename = $filename;
$this->filename = $filename;
// chain when the filename is set
return $this;
}
return $this->filename;
return $this;
}
/**
@@ -62,7 +58,9 @@ class Dompdf implements Pdf
*/
public function setPaper($size, $orientation)
{
return $this->dompdfInstance->set_paper($size, $orientation);
$this->dompdfInstance->setPaper($size, $orientation);
return $this;
}
/**
@@ -70,36 +68,52 @@ class Dompdf implements Pdf
*/
public function render()
{
return $this->dompdfInstance->render();
$this->dompdfInstance->render();
return $this;
}
/**
* {@inheritdoc}
*/
public function clear()
public function setOptions(Options $options)
{
\Image_Cache::clear();
$this->dompdfInstance->setOptions($options);
return true;
return $this;
}
/**
* {@inheritdoc}
*/
public function show($options = ['compress' => 1, 'Attachment' => 0])
public function getOptions()
{
return $this->dompdfInstance->getOptions();
}
/**
* {@inheritdoc}
*/
public function show($acceptRanges = false, $compress = true, $attachment = true)
{
$options = [
'Accept-Ranges' => (int) $acceptRanges,
'compress' => (int) $compress,
'Attachment' => (int) $attachment,
];
$this->render();
$this->clear();
$filename = !is_null($this->filename) ? $this->filename : static::DOWNLOAD_FILENAME;
return $this->dompdfInstance->stream($this->filename(), $options);
$this->dompdfInstance->stream($filename, $options);
}
/**
* {@inheritdoc}
*/
public function download($options = ['compress' => 1, 'Attachment' => 1])
public function download()
{
return new Response($this->show($options), 200, [
return new Response($this->show(false, true, true), 200, [
'Content-Type' => 'application/pdf',
]);
}
@@ -107,10 +121,19 @@ class Dompdf implements Pdf
/**
* {@inheritdoc}
*/
public function output($options = ['compress' => 1])
public function output($compress = true)
{
$this->render();
$options = [
'compress' => (int) $compress,
];
return $this->dompdfInstance->output($options);
$this->render();
$output = $this->dompdfInstance->output($options);
if (!is_null($this->filename)) {
file_put_contents($this->filename, $output);
}
return $output;
}
}

View File

@@ -1,71 +1,92 @@
<?php
namespace Vsmoraes\Pdf;
use Dompdf\Options;
interface Pdf
{
const DEFAULT_SIZE = 'A4';
const DEFAULT_ORIENTATION = 'portrait';
/**
* Loads the HTML to the DOMPDF class
*
* @param $html
* @param string $size
* @param string $orientation
* @return mixed
*
* @return $this
*/
public function load($html, $size = 'A4', $orientation = 'portrait');
public function load($html, $size = self::DEFAULT_SIZE, $orientation = self::DEFAULT_ORIENTATION);
/**
* Set the filename (full path) to where the file should be saved
*
* @param null $filename
* @return mixed
* @param string $filename
*
* @return $this
*/
public function filename($filename = null);
public function filename($filename);
/**
* Set paper size
*
* @param $size
* @param $orientation
* @return mixed
* @param string $size
* @param string $orientation
*
* @return $this
*/
public function setPaper($size, $orientation);
/**
* Render the pdf
* Renders the HTML to PDF
*
* @return mixed
* @return $this
*/
public function render();
/**
* Clear the pdf
*
* @return mixed
*/
public function clear();
/**
* Render the pdf on the browser
*
* @param array $options
* @return mixed
* @param bool $acceptRanges
* @param bool $compress
* @param bool $attachment
*
* @return void
*/
public function show($options = ['compress' => 1, 'Attachment' => 0]);
public function show($acceptRanges = false, $compress = true, $attachment = true);
/**
* Forces the pdf to download
*
* @param array $options
* @return mixed
*/
public function download($options = ['compress' => 1, 'Attachment' => 0]);
public function download();
/**
* Output the pdf the to file speficied on $this->filename()
*
* @param array $options
* @return mixed
* @param bool $compress
*
* @return string
*/
public function output($options = ['compress' => 1]);
public function output($compress = true);
/**
* Define Dompdf Options
* @see https://github.com/dompdf/dompdf/blob/master/src/Options.php
*
* @param Options $options
*
* @return $this
*/
public function setOptions(Options $options);
/**
* Return the current Dompdf options
* @see https://github.com/dompdf/dompdf/blob/master/src/Options.php
*
* @return Options
*/
public function getOptions();
}

View File

@@ -1,6 +1,7 @@
<?php
namespace Vsmoraes\Pdf;
use Dompdf\Dompdf;
use Illuminate\Support\ServiceProvider;
use Vsmoraes\Pdf\Dompdf as MyDompdf;
@@ -11,6 +12,7 @@ class PdfServiceProvider extends ServiceProvider
*
* @var bool
*/
protected $defer = true;
/**
* Register the classes on the IoC container
@@ -19,16 +21,12 @@ class PdfServiceProvider extends ServiceProvider
*/
public function register()
{
$this->app->bind('DOMPDF', function() {
return new \DOMPDF();
$this->app->bind(Dompdf::class, function() {
return new Dompdf();
});
$this->app->bind('Vsmoraes\Pdf\Pdf', function() {
define('DOMPDF_ENABLE_AUTOLOAD', false);
require_once base_path() . '/vendor/dompdf/dompdf/dompdf_config.inc.php';
return new Dompdf(new \DOMPDF());
$this->app->bind(Pdf::class, function() {
return new MyDompdf(new Dompdf());
});
}
@@ -39,6 +37,8 @@ class PdfServiceProvider extends ServiceProvider
*/
public function provides()
{
return ['Vsmoraes\Pdf\Pdf'];
return [
Pdf::class,
];
}
}

View File

@@ -1,6 +1,3 @@
<?php
define('DOMPDF_ENABLE_AUTOLOAD', false);
require_once __DIR__ . '/../vendor/dompdf/dompdf/dompdf_config.inc.php';
require_once __DIR__ . '/../vendor/autoload.php';
require __DIR__ . '/../vendor/autoload.php';

View File

@@ -1,34 +1,54 @@
<?php
namespace Vsmoraes\Pdf;
class DompdfTest extends PHPUnit_Framework_TestCase
use PHPUnit\Framework\TestCase;
class DompdfTest extends TestCase
{
public function testLoadHtml()
{
$stub = $this->getMockBuilder('\DOMPDF')
->setMethods(['load_html'])
$html = '<html><head></head><body></body></html>';
$size = Pdf::DEFAULT_SIZE;
$orientation = Pdf::DEFAULT_ORIENTATION;
$stub = $this->getMockBuilder(\Dompdf\Dompdf::class)
->setMethods(['loadHtml', 'setPaper'])
->getMock();
$pdf = new \Vsmoraes\Pdf\Dompdf($stub);
$stub->expects($this->once())
->method('loadHtml')
->with($html);
$stub->expects($this->once())
->method('load_html');
->method('setPaper')
->with($size, $orientation);
$pdf->load('<html><head></head><body></body></html>');
$pdf = new Dompdf($stub);
$result = $pdf->load($html);
$this->assertEquals($pdf, $result);
}
public function testShowHtml()
{
$stub = $this->getMockBuilder('\DOMPDF')
->getMock();
$html = '<html><head></head><body></body></html>';
$size = Pdf::DEFAULT_SIZE;
$orientation = Pdf::DEFAULT_ORIENTATION;
$pdf = new \Vsmoraes\Pdf\Dompdf($stub);
$stub = $this->getMockBuilder(\Dompdf\Dompdf::class)
->setMethods(['setPaper', 'stream'])
->getMock();
$stub->expects($this->once())
->method('stream');
$pdf->load('<html><head></head><body></body></html>');
$stub->expects($this->once())
->method('setPaper')
->with($size, $orientation);
$pdf = new Dompdf($stub);
$pdf->load($html);
$pdf->show();
}
}