Skip to content

Commit 480ecdb

Browse files
committed
Init
1 parent 3d0a4f0 commit 480ecdb

File tree

11 files changed

+366
-0
lines changed

11 files changed

+366
-0
lines changed

.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/vendor
2+
/.idea
3+
.DS_Store
4+
/samples
5+
/ZZ_samples
6+
/pdf-parser/var
7+
/.phpunit.result.cache
8+
.php_cs.cache
9+
composer.lock

.php_cs.dist

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
$finder = PhpCsFixer\Finder::create()
4+
->in(__DIR__ . '/src')
5+
;
6+
7+
return PhpCsFixer\Config::create()
8+
->setRules([
9+
'@PSR2' => true,
10+
'@Symfony' => true,
11+
//'strict_param' => true,
12+
'cast_spaces' => ['space' => 'none'],
13+
'array_syntax' => ['syntax' => 'short'],
14+
'no_unneeded_curly_braces' => false,
15+
'no_unset_on_property' => false,
16+
'php_unit_method_casing' => ['case' => 'snake_case'],
17+
'phpdoc_align' => ['align' => 'left'],
18+
'yoda_style' => false,
19+
])
20+
->setFinder($finder)
21+
;

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Typeset.sh wrapper for laravel 7
2+

composer.json

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"name": "typesetsh/laravel-wrapper",
3+
"description": "Typeset.sh wrapper for laravel 7",
4+
"type": "library",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "Jacob Siefer",
9+
"email": "[email protected]"
10+
}
11+
],
12+
"minimum-stability": "stable",
13+
"require": {
14+
"laravel/framework": "^7.0",
15+
"typesetsh/typesetsh": "^0.9"
16+
},
17+
"require-dev": {
18+
"friendsofphp/php-cs-fixer": "^2.17@dev",
19+
"phan/phan": "dev-master"
20+
},
21+
"autoload": {
22+
"psr-4": {
23+
"Typesetsh\\LaravelWrapper\\": "src/"
24+
},
25+
"files": ["src/helper.php"]
26+
},
27+
"autoload-dev": {
28+
"psr-4": {
29+
"Typesetsh\\LaravelWrapper\\": "tests/unit/"
30+
}
31+
},
32+
"repositories": [{
33+
"type": "composer",
34+
"url": "https://packages.typeset.sh"
35+
}
36+
]
37+
}

config/typesetsh.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
return [
4+
'allowed_directories' => [public_path()],
5+
6+
'allowed_protocols' => ['http', 'https'],
7+
8+
'cache_dir' => storage_path('framework/cache/typesetsh'),
9+
10+
'timeout' => 15,
11+
];

src/Facades/Pdf.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
/**
3+
* @copyright Copyright (c) 2020 Jacob Siefer
4+
*
5+
* @see LICENSE
6+
*/
7+
8+
namespace Typesetsh\LaravelWrapper\Facades;
9+
10+
use Typesetsh\LaravelWrapper\PdfView;
11+
12+
/**
13+
* @method static PdfView make($view, array $data = [], array $mergeData = [])
14+
*/
15+
class Pdf extends \Illuminate\Support\Facades\Facade
16+
{
17+
public static function getFacadeAccessor()
18+
{
19+
return 'typesetsh.pdf';
20+
}
21+
}

src/Pdf/Factory.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
/**
3+
* @copyright Copyright (c) 2020 Jacob Siefer
4+
*
5+
* @see LICENSE
6+
*/
7+
declare(strict_types=1);
8+
9+
namespace Typesetsh\LaravelWrapper\Pdf;
10+
11+
use Illuminate\Contracts\View\Factory as ViewFactory;
12+
use Typesetsh\LaravelWrapper\Typesetsh;
13+
14+
class Factory
15+
{
16+
/**
17+
* @var Typesetsh
18+
*/
19+
private $typesetsh;
20+
21+
/**
22+
* @var ViewFactory
23+
*/
24+
private $view;
25+
26+
public function __construct(Typesetsh $pdf, ViewFactory $view)
27+
{
28+
$this->typesetsh = $pdf;
29+
$this->view = $view;
30+
}
31+
32+
/**
33+
* Get the evaluated view contents for the given view.
34+
*
35+
* @param string $view
36+
* @param \Illuminate\Contracts\Support\Arrayable|array $data
37+
* @param array $mergeData
38+
*/
39+
public function make($view, $data = [], $mergeData = []): View
40+
{
41+
$view = $this->viewInstance($this->view->make($view, $data, $mergeData));
42+
43+
return $view;
44+
}
45+
46+
protected function viewInstance(\Illuminate\Contracts\View\View $view): View
47+
{
48+
return new View($view, $this->typesetsh);
49+
}
50+
}

src/Pdf/View.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
/**
3+
* @copyright Copyright (c) 2020 Jacob Siefer
4+
*
5+
* @see LICENSE
6+
*/
7+
declare(strict_types=1);
8+
9+
namespace Typesetsh\LaravelWrapper\Pdf;
10+
11+
use Illuminate\Contracts\Support\Renderable;
12+
use Illuminate\Contracts\Support\Responsable;
13+
use Illuminate\Contracts\View\View as HtmlView;
14+
use Symfony\Component\HttpFoundation\StreamedResponse;
15+
use Typesetsh\LaravelWrapper\Typesetsh;
16+
17+
class View implements Renderable, Responsable
18+
{
19+
/**
20+
* @var Typesetsh
21+
*/
22+
private $pdf;
23+
24+
/**
25+
* @var HtmlView
26+
*/
27+
private $view;
28+
29+
/**
30+
* @var string
31+
*/
32+
private $filename;
33+
34+
public function __construct(HtmlView $view, Typesetsh $pdf)
35+
{
36+
$this->view = $view;
37+
$this->pdf = $pdf;
38+
}
39+
40+
public function forceDownload(string $filename): void
41+
{
42+
$this->filename = $filename;
43+
}
44+
45+
public function render(): string
46+
{
47+
$html = $this->view->render();
48+
49+
return $this->pdf->render($html)->asString();
50+
}
51+
52+
/**
53+
* Create an HTTP response that represents the object.
54+
*
55+
* @param \Illuminate\Http\Request $request
56+
*/
57+
public function toResponse($request): StreamedResponse
58+
{
59+
$cb = function () {
60+
echo $this->render();
61+
};
62+
63+
$headers = [
64+
'Content-Type' => 'application/pdf',
65+
];
66+
67+
if ($this->filename) {
68+
$headers['Content-Disposition'] = 'attachment; filename='.$this->filename;
69+
}
70+
71+
return response()->stream($cb, 200, $headers);
72+
}
73+
}

src/ServiceProvider.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
/**
3+
* @copyright Copyright (c) 2020 Jacob Siefer
4+
*
5+
* @see LICENSE
6+
*/
7+
declare(strict_types=1);
8+
9+
namespace Typesetsh\LaravelWrapper;
10+
11+
use Illuminate\Contracts\Support\DeferrableProvider;
12+
use Illuminate\Support;
13+
use typesetsh\UriResolver;
14+
15+
class ServiceProvider extends Support\ServiceProvider implements DeferrableProvider
16+
{
17+
const CONFIG_PATH = __DIR__.'/../config/typesetsh.php';
18+
19+
/**
20+
* Register the service provider.
21+
*/
22+
public function register()
23+
{
24+
$this->mergeConfigFrom(self::CONFIG_PATH, 'typesetsh');
25+
26+
$this->registerPdfRenderer();
27+
}
28+
29+
protected function registerPdfRenderer(): void
30+
{
31+
$this->app->singleton('typesetsh', function ($app) {
32+
$allowedDirectories = $app['config']['typesetsh.allowed_directories'] ?? [];
33+
$allowProtocols = $app['config']['typesetsh.allowed_protocols'] ?? [];
34+
$cacheDir = $app['config']['typesetsh.cache_dir'] ?? null;
35+
$timeout = $app['config']['typesetsh.timeout'] ?? 15;
36+
37+
$schemes = [];
38+
if ($allowProtocols) {
39+
$http = new UriResolver\Http($cacheDir, $timeout);
40+
foreach ($allowProtocols as $protocol) {
41+
$schemes[$protocol] = $http;
42+
}
43+
}
44+
if ($allowedDirectories) {
45+
$schemes['file'] = new UriResolver\File($allowedDirectories);
46+
}
47+
48+
return new Typesetsh(new UriResolver($schemes));
49+
});
50+
51+
$this->app->singleton('typesetsh.pdf', function ($app) {
52+
return new Pdf\Factory($app['typesetsh'], $app['view']);
53+
});
54+
}
55+
56+
/**
57+
* Get the services provided by the provider.
58+
*/
59+
public function provides(): array
60+
{
61+
return ['typesetsh', 'typesetsh.pdf'];
62+
}
63+
}

src/Typesetsh.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
/**
3+
* @copyright Copyright (c) 2020 Jacob Siefer
4+
*
5+
* @see LICENSE
6+
*/
7+
declare(strict_types=1);
8+
9+
namespace Typesetsh\LaravelWrapper;
10+
11+
use typesetsh\HtmlToPdf;
12+
use typesetsh\Result;
13+
use typesetsh\UriResolver;
14+
15+
/**
16+
* Typeset.sh pdf wrapper with a pre provided uri resolver.
17+
*/
18+
class Typesetsh
19+
{
20+
/**
21+
* @var HtmlToPdf
22+
*/
23+
private $html2pdf;
24+
25+
/**
26+
* @var callable|UriResolver
27+
*/
28+
private $uriResolver;
29+
30+
public function __construct(callable $uriResolver = null, HtmlToPdf $html2pdf = null)
31+
{
32+
$this->uriResolver = $uriResolver ?? UriResolver::httpOnly();
33+
$this->html2pdf = $html2pdf ?? new HtmlToPdf();
34+
}
35+
36+
public function render(string $html): Result
37+
{
38+
return $this->html2pdf->render($html, $this->uriResolver);
39+
}
40+
41+
/**
42+
* @param string[] $html
43+
*/
44+
public function renderMultiple(array $html): Result
45+
{
46+
return $this->html2pdf->renderMultiple($html, $this->uriResolver);
47+
}
48+
}

0 commit comments

Comments
 (0)