baseDir = storage_path('app/public/proceeding-paper'); $this->tempDir = $this->baseDir . DIRECTORY_SEPARATOR . $event->slug; $this->papersDir = $this->tempDir . DIRECTORY_SEPARATOR . 'papers'; } protected function ensureDirectoryExists() { if (!File::exists($this->papersDir)) { File::makeDirectory($this->papersDir, 0777, true, true); chmod($this->baseDir, 0777); chmod($this->tempDir, 0777); chmod($this->papersDir, 0777); } } protected function mapFont() { if ($this->font === 'font-serif') { $this->font = "'Times New Roman', Times, serif"; } else { $this->font = 'Arial, Helvetica, sans-serif'; } } protected function setFormatting(Browsershot $browsershot) { return $browsershot ->timeout(120) // ->setNodeBinary('/home/sanket/.nvm/versions/node/v14.18.1/bin/node') // ->setNpmBinary('/home/sanket/.nvm/versions/node/v14.18.1/bin/npm') // ->setIncludePath('$PATH:/home/sanket/.nvm/versions/node/v14.18.1/bin') // ->setNodeModulePath(base_path('node-modules')) // ->setChromePath('/usr/bin/google-chrome') ->waitUntilNetworkIdle() ->margins(10, 10, 10, 10) ->setOption('addStyleTag', json_encode(['content' => 'body { font-family: ' . $this->font . '}'])) ->format('A4') ->showBackground(); } protected function makeFrontPage() { $this->setFormatting( HeadlessBrowser::html(view('pdf.front', [ 'font' => $this->font, 'event' => $this->event, ])->render()) )->save($this->tempDir . DIRECTORY_SEPARATOR . 'frontpage.pdf'); } protected function countPages($path) { $pdftext = file_get_contents($path); $num = preg_match_all("/\/Page\W/", $pdftext, $dummy); return $num; } protected function mergePDF() { $pdfMerger = PDFMerger::init(); $pdfMerger->addPDF($this->tempDir . DIRECTORY_SEPARATOR . 'frontpage.pdf', 'all'); $pdfMerger->addPDF($this->tempDir . DIRECTORY_SEPARATOR . 'index.pdf', 'all'); $files = File::files($this->papersDir); foreach ($files as $file) { //dd($file->getPathname()); $pdfMerger->addPDF($file->getPathname(), 'all'); } $mergedFilePath = $this->baseDir . DIRECTORY_SEPARATOR . $this->event->slug . '.pdf'; $pdfMerger->merge(orientation: null, meta: [ 'title' => $this->event->title, 'author' => $this->event->user->name, 'subject' => $this->event->subtitle, 'keywords' => implode(', ', $this->event->themes), 'creator' => config('app.name'), ]); $pdfMerger->save($mergedFilePath); File::deleteDirectory($this->tempDir); } protected function getPapers(): Collection { return $this->event ->papers() ->whereNotNull('submitted_at') ->whereNotNull('accepted_at') ->whereHas('firstAuthorUser', function ($query) { $query->join('enrollments', function ($join) { $join->on('users.id', '=', 'enrollments.user_id'); }); }) ->with([ 'user.profile', 'firstAuthorUser.profile', 'authorUsers.profile', ]) ->withCount('attachments') ->get(); } protected function getPaperFilePath(Paper $paper): string { return $this->papersDir . DIRECTORY_SEPARATOR . $paper->id . '.pdf'; } protected function makePapersPage(Collection $papers) { foreach ($papers as $paper) { $filePath = $this->getPaperFilePath($paper); $this->setFormatting( HeadlessBrowser::html( view('pdf.proceeding', ['paper' => $paper, 'font' => $this->font])->render() ) )->save($filePath); } } protected function makeIndexingPage(array $indexing) { $this->setFormatting( HeadlessBrowser::html(view('pdf.indexFile', ['indexingArray' => $indexing, 'font' => $this->font])->render()) )->save($this->tempDir . DIRECTORY_SEPARATOR . 'index.pdf'); } protected function generateIndexing(Collection $papers): array { return $papers->map(function ($paper) { $filePath = $this->getPaperFilePath($paper); return (object) ['count' => $this->countPages($filePath), 'title' => $paper->title]; })->toArray(); } /** * Execute the job. */ public function handle() { $this->ensureDirectoryExists(); $this->mapFont(); $this->makeFrontPage(); $papers = $this->getPapers(); $this->makePapersPage($papers); $indexing = $this->generateIndexing($papers); $this->makeIndexingPage($indexing); $this->mergePDF(); // event(new App\Events\ProceedingPdfGenerated()); } }