|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Jobs; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Bus\Queueable; |
|
6
|
|
|
use Illuminate\Queue\SerializesModels; |
|
7
|
|
|
use Illuminate\Queue\InteractsWithQueue; |
|
8
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue; |
|
9
|
|
|
use Illuminate\Foundation\Bus\Dispatchable; |
|
10
|
|
|
use App\Models\Eloquent\Contest; |
|
11
|
|
|
use Imtigger\LaravelJobStatus\Trackable; |
|
12
|
|
|
use Nesk\Puphpeteer\Puppeteer; |
|
13
|
|
|
use Nesk\Rialto\Data\JsFunction; |
|
14
|
|
|
use Cache; |
|
15
|
|
|
use Exception; |
|
16
|
|
|
use Str; |
|
17
|
|
|
use PDF; |
|
18
|
|
|
|
|
19
|
|
|
class GeneratePDF implements ShouldQueue |
|
20
|
|
|
{ |
|
21
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, Trackable; |
|
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
public $tries = 1; |
|
24
|
|
|
protected $cid; |
|
25
|
|
|
protected $config; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Create a new job instance. |
|
29
|
|
|
* |
|
30
|
|
|
* @return void |
|
31
|
|
|
*/ |
|
32
|
|
|
|
|
33
|
|
|
public function __construct($cid, $config) |
|
34
|
|
|
{ |
|
35
|
|
|
$this->prepareStatus(); |
|
36
|
|
|
$this->cid = $cid; |
|
37
|
|
|
$default = [ |
|
38
|
|
|
'cover' => false, |
|
39
|
|
|
'advice' => false, |
|
40
|
|
|
]; |
|
41
|
|
|
$this->config = array_merge($default, $config); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Execute the job. |
|
46
|
|
|
* |
|
47
|
|
|
* @return void |
|
48
|
|
|
*/ |
|
49
|
|
|
public function handle() |
|
50
|
|
|
{ |
|
51
|
|
|
$cid = $this->cid; |
|
52
|
|
|
$config = $this->config; |
|
53
|
|
|
$accessToken = Str::random(32); |
|
54
|
|
|
|
|
55
|
|
|
Cache::tags(['contest', 'pdfViewAccess', $cid])->put($accessToken, $config); |
|
56
|
|
|
|
|
57
|
|
|
if (!is_dir(storage_path("app/contest/pdf/"))) { |
|
58
|
|
|
mkdir(storage_path("app/contest/pdf/"), 0777, true); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
$record = Contest::find($cid); |
|
62
|
|
|
|
|
63
|
|
|
$puppeteer = new Puppeteer; |
|
64
|
|
|
$browser = $puppeteer->launch([ |
|
65
|
|
|
'args' => ['--no-sandbox', '--disable-setuid-sandbox'], |
|
66
|
|
|
]); |
|
67
|
|
|
|
|
68
|
|
|
$page = $browser->newPage(); |
|
69
|
|
|
|
|
70
|
|
|
$response = $page->goto(route('contest.board.admin.pdf.view', [ |
|
71
|
|
|
'cid' => $cid, |
|
72
|
|
|
'accessToken' => $accessToken, |
|
73
|
|
|
]), [ |
|
74
|
|
|
'waitUntil' => 'networkidle0' |
|
75
|
|
|
]); |
|
76
|
|
|
|
|
77
|
|
|
if($response->status() != '200') { |
|
78
|
|
|
throw new Exception('Cannot Access PDF Generated View Stream'); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
$page->waitForSelector('body.rendered', [ |
|
82
|
|
|
'timeout' => 120000 |
|
83
|
|
|
]); |
|
84
|
|
|
|
|
85
|
|
|
if($config['renderer'] == 'blink') { |
|
86
|
|
|
$page->pdf([ |
|
87
|
|
|
'format' => 'A4', |
|
88
|
|
|
'path' => storage_path("app/contest/pdf/$cid.pdf"), |
|
89
|
|
|
'printBackground' => true |
|
90
|
|
|
]); |
|
91
|
|
|
|
|
92
|
|
|
$browser->close(); |
|
93
|
|
|
|
|
94
|
|
|
$record->pdf = 1; |
|
95
|
|
|
$record->save(); |
|
96
|
|
|
return; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
$parsedHTML = $page->content(); |
|
100
|
|
|
|
|
101
|
|
|
$browser->close(); |
|
102
|
|
|
|
|
103
|
|
|
$pdf=PDF::setOptions([ |
|
104
|
|
|
'dpi' => 96, |
|
105
|
|
|
'isPhpEnabled' => true, |
|
106
|
|
|
'isHtml5ParserEnabled' => true, |
|
107
|
|
|
'isRemoteEnabled' => true |
|
108
|
|
|
])->setWarnings(false)->loadHTML($parsedHTML); |
|
109
|
|
|
|
|
110
|
|
|
$pdf->output(); |
|
111
|
|
|
|
|
112
|
|
|
$pdf->addInfo([ |
|
113
|
|
|
'Subject' => "$record->name ProblemSet", |
|
114
|
|
|
'Producer' => config('app.displayName'), |
|
115
|
|
|
'Creator' => config('app.name').' Contest PDF Auto-Generater', |
|
116
|
|
|
'CreatorTool' => config('app.url'), |
|
117
|
|
|
'BaseURL' => route('contest.detail', ['cid' => $cid]), |
|
118
|
|
|
])->save(storage_path("app/contest/pdf/$cid.pdf")); |
|
119
|
|
|
|
|
120
|
|
|
$record->pdf = 1; |
|
121
|
|
|
$record->save(); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
public function failed() |
|
125
|
|
|
{ |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|