-
Notifications
You must be signed in to change notification settings - Fork 1
/
GpdfController.php
83 lines (63 loc) · 2.51 KB
/
GpdfController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Traits\HasViewDynamicParams;
use Omaralalwi\Gpdf\Gpdf;
use Omaralalwi\Gpdf\Facade\Gpdf as GpdfFacAde;
class GpdfController extends Controller
{
use HasViewDynamicParams;
/*
* this is first way using Gpdf facade .
* also you can use facade like this
* use Gpdf;
* alternate of full path like this use Omaralalwi\Gpdf\Facade\Gpdf;
* no need to add facade alias , it is pre config.
*/
public function generatePdf()
{
// optional if you have dynamic params need to passed to view file
$data = $this->getDynamicParams();
$html = view('pdf.example-1', $data)->render();
// $html = view('pdf.example-1')->render(); // or like this without params
$pdfContent = GpdfFacAde::generate($html);
return response($pdfContent, 200, ['Content-Type' => 'application/pdf']);
}
/*
* this is second way depend on injection in service provider. (no need to inject it, it ready injected in GpdfServiceProvider)
*/
public function generateSecondWayPdf()
{
$data = $this->getDynamicParams();
$html = view('pdf.example-2',$data)->render();
$gpdf = app(Gpdf::class);
$pdfFile = $gpdf->generate($html);
return response($pdfFile, 200, ['Content-Type' => 'application/pdf']);
}
public function generateAndStream()
{
$data = $this->getDynamicParams();
$html = view('pdf.example-2',$data)->render();
$gpdf = app(Gpdf::class);
$gpdf->generateWithStream($html,'test-pdf-files', true);
$pdfFile = $gpdf->generate($html); // optional
return response($pdfFile, 200, ['Content-Type' => 'application/pdf']);
}
public function generateAndStore()
{
$data = $this->getDynamicParams();
$html = view('pdf.example-2',$data)->render();
$gpdf = app(Gpdf::class);
$storePath = storage_path('/app/downloads/users/');
$gpdf->generateWithStore($html, $storePath, 'test-store-pdf-fle'); // file name optionals
$pdfFile = $gpdf->generate($html); // optional
return response($pdfFile, 200, ['Content-Type' => 'application/pdf']);
}
public function generatePdfWithArabicContent()
{
$data = $this->getDynamicParams();
$html = view('pdf.example-2-with-arabic', $data)->render();
$pdfContent = GpdfFacAde::generate($html);
return response($pdfContent, 200, ['Content-Type' => 'application/pdf']);
}
}