1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Traincase\LaravelPdfTinker\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Illuminate\Support\Str; |
7
|
|
|
use League\Flysystem\FileNotFoundException; |
8
|
|
|
use League\Flysystem\Filesystem; |
9
|
|
|
use League\Flysystem\FilesystemInterface; |
10
|
|
|
use Symfony\Component\HttpFoundation\Response; |
11
|
|
|
use Traincase\HtmlToPdfTinker\DTO\PdfToGenerateDTO; |
12
|
|
|
use Traincase\LaravelPdfTinker\Http\Requests\PreviewRequest; |
13
|
|
|
use Traincase\HtmlToPdfTinker\PdfTinkerManager; |
14
|
|
|
|
15
|
|
|
class PlaygroundController |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @param PdfTinkerManager $manager |
19
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
20
|
|
|
*/ |
21
|
|
|
public function index(PdfTinkerManager $manager) |
22
|
|
|
{ |
23
|
|
|
return view('laravel-pdf-tinker::playground', [ |
24
|
|
|
'drivers' => $manager->getRegisteredDrivers(), |
25
|
|
|
]); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function preview(PreviewRequest $request, Filesystem $filesystem, PdfTinkerManager $tinkerManager) |
29
|
|
|
{ |
30
|
|
|
try { |
31
|
|
|
$filename = Str::random('10'); |
32
|
|
|
|
33
|
|
|
$options = json_decode($request->input('options', '{}'), true); |
34
|
|
|
if (!is_array($options)) { |
35
|
|
|
throw new \Exception('Driver options are not in valid json format.'); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
$tinkerManager->resolve($request->input('driver')) |
39
|
|
|
->storeOnFilesystem($filesystem, new PdfToGenerateDTO([ |
40
|
|
|
'orientation' => $request->input('mode', 'portrait'), |
41
|
|
|
'html' => $request->input('html'), |
42
|
|
|
'options' => $options, |
43
|
|
|
'filename' => "{$filename}.pdf", |
44
|
|
|
'path' => 'vendor/laravel-pdf-tinker', |
45
|
|
|
])); |
46
|
|
|
|
47
|
|
|
return response()->json([ |
48
|
|
|
'url' => route('vendor.laravel-pdf-tinker.download', ['alias' => $filename]) |
49
|
|
|
]); |
50
|
|
|
} catch (Exception $e) { |
51
|
|
|
return response()->json([ |
52
|
|
|
'message' => $e->getMessage() |
53
|
|
|
], 500); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param string $alias |
59
|
|
|
* @param FilesystemInterface $filesystem |
60
|
|
|
* @return Response |
61
|
|
|
*/ |
62
|
|
|
public function download(string $alias, Filesystem $filesystem) |
63
|
|
|
{ |
64
|
|
|
try { |
65
|
|
|
return new Response( |
66
|
|
|
$filesystem->readAndDelete("vendor/laravel-pdf-tinker/{$alias}.pdf"), |
67
|
|
|
200, |
68
|
|
|
[ |
69
|
|
|
'Content-Type' => 'application/pdf', |
70
|
|
|
'Content-Disposition' => "inline; filename=pdf-tinker-{$alias}.pdf", |
71
|
|
|
] |
72
|
|
|
); |
73
|
|
|
} catch (FileNotFoundException $exception) { |
74
|
|
|
return response('PDF not found', 404); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Serve our own css, so people don't have to publish/build assets |
80
|
|
|
*/ |
81
|
|
|
public function css() |
82
|
|
|
{ |
83
|
|
|
return new Response( |
84
|
|
|
file_get_contents(__DIR__ . '/../../../build/playground.css'), |
85
|
|
|
200, |
86
|
|
|
[ |
87
|
|
|
'Content-Type' => 'text/css', |
88
|
|
|
] |
89
|
|
|
); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Serve our own js, so people don't have to publish/build assets |
94
|
|
|
*/ |
95
|
|
|
public function js() |
96
|
|
|
{ |
97
|
|
|
return new Response( |
98
|
|
|
file_get_contents(__DIR__ . '/../../../build/playground.js'), |
99
|
|
|
200, |
100
|
|
|
[ |
101
|
|
|
'Content-Type' => 'text/javascript', |
102
|
|
|
] |
103
|
|
|
); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|