Skip to content

Commit

Permalink
remake pdf generation using cpdf and blink with skia
Browse files Browse the repository at this point in the history
  • Loading branch information
ZsgsDesign committed Oct 21, 2021
1 parent c2cec17 commit b08ab87
Showing 1 changed file with 26 additions and 39 deletions.
65 changes: 26 additions & 39 deletions app/Jobs/GeneratePDF.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
use Imtigger\LaravelJobStatus\Trackable;
use Nesk\Puphpeteer\Puppeteer;
use Nesk\Rialto\Data\JsFunction;
use Cache;
use Exception;
use Str;
use PDF;

class GeneratePDF implements ShouldQueue
Expand Down Expand Up @@ -47,6 +50,9 @@ public function handle()
{
$cid = $this->cid;
$config = $this->config;
$accessToken = Str::random(32);

Cache::tags(['contest', 'pdfViewAccess', $cid])->put($accessToken, $config);

if (!is_dir(storage_path("app/contest/pdf/"))) {
mkdir(storage_path("app/contest/pdf/"), 0777, true);
Expand All @@ -61,54 +67,35 @@ public function handle()

$page = $browser->newPage();

$page->goto(route('contest.board.admin.pdf.view', [
'cid' => $cid
$response = $page->goto(route('contest.board.admin.pdf.view', [
'cid' => $cid,
'accessToken' => $accessToken,
]), [
'waitUntil' => 'networkidle0'
]);

// $sourceHTML = view('pdf.contest.main', [
// 'conf' => $config,
// 'contest' => [
// 'cid' => $cid,
// 'name' => $record->name,
// 'shortName' => $record->name,
// 'date' => date("F j, Y", strtotime($record->begin_time)),
// ],
// 'problemset' => $record->getProblemSet(false),
// ])->render();

// file_put_contents(__DIR__."/source.html", $sourceHTML);

// return;

// $page->setContent(, [
// 'waitUntil' => 'networkidle0'
// ]);

$page->waitFor(5000);

// $page->waitForSelector('.MathJaxLoadingComplete', [
// 'timeout' => 50000,
// ]);
if($response->status() != '200') {
throw new Exception('Cannot Access PDF Generated View Stream');
}

// $page->screenshot(['path' => 'example.png']);
$page->waitForSelector('body.rendered', [
'timeout' => 120000
]);

// $page->pdf([
// 'format' => 'A4',
// 'path' => storage_path("app/contest/pdf/$cid.pdf"),
// ]);
if($config['renderer'] == 'blink') {
$page->pdf([
'format' => 'A4',
'path' => storage_path("app/contest/pdf/$cid.pdf"),
'printBackground' => true
]);

// $parsedHTML = $page->evaluate(JsFunction::createWithBody("
// return document.querySelector('*').outerHTML;
// "));
$browser->close();
return;
}

$parsedHTML = $page->content();

// dump($parsedHTML);

$browser->close();
// return;

// $pdf=PDF::setOptions([
// 'dpi' => 150,
Expand All @@ -123,14 +110,14 @@ public function handle()
// 'shortName' => $record->name,
// 'date' => date("F j, Y", strtotime($record->begin_time)),
// ],
// 'problemset' => $record->getProblemSet(false),
// 'problemset' => $record->getProblemSet(),
// ]);

// file_put_contents(__DIR__."/lalala.html", $parsedHTML);
// return;

$pdf=PDF::setOptions([
'dpi' => 150,
'dpi' => 96,
'isPhpEnabled' => true,
'isHtml5ParserEnabled' => true,
'isRemoteEnabled' => true
Expand Down

0 comments on commit b08ab87

Please sign in to comment.