355 lines
10 KiB
PHP
Executable File
355 lines
10 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use Spatie\Browsershot\Browsershot;
|
|
use Illuminate\Http\Response;
|
|
|
|
class PdfWrapper
|
|
{
|
|
protected Browsershot $pdfGenerator;
|
|
protected string $html;
|
|
protected string $headerHtml;
|
|
protected string $footerHtml;
|
|
public function __construct()
|
|
{
|
|
$this->pdfGenerator = new Browsershot();
|
|
$this->headerHtml = view('projectsClients.pdf._header')->render();
|
|
$this->footerHtml = view('projectsClients.pdf._footer')->render();
|
|
}
|
|
// Load view and render HTML
|
|
public function loadView(string $bladeFile, array $data = []): self
|
|
{
|
|
$this->html = view($bladeFile, $data)->render();
|
|
return $this;
|
|
}
|
|
// Load raw HTML
|
|
public function loadHtml(string $html): self
|
|
{
|
|
$this->html = $html;
|
|
return $this;
|
|
}
|
|
// Load HTML from a URL
|
|
public function loadUrl(string $url): self
|
|
{
|
|
$this->html = file_get_contents(url($url));
|
|
return $this;
|
|
}
|
|
// Generate the PDF using Browsershot
|
|
public function generate(): Browsershot
|
|
{
|
|
return $this->pdfGenerator
|
|
->html($this->html)
|
|
->format('A4')
|
|
->margins(10, 20, 10, 20)
|
|
->showBrowserHeaderAndFooter()
|
|
->headerHtml($this->headerHtml)
|
|
->footerHtml($this->footerHtml)
|
|
->waitUntilNetworkIdle();
|
|
}
|
|
// Save the PDF to the server in the public folder
|
|
public function save(string $path): void
|
|
{
|
|
$this->generate()->savePdf($path);
|
|
}
|
|
// Download the PDF with a specified filename
|
|
public function download(string $filename)
|
|
{
|
|
$pdf = $this->generate()->pdf();
|
|
return new Response($pdf, 200, [
|
|
'Content-Type' => 'application/pdf',
|
|
'Content-Disposition' => 'attachment; filename="' . $filename . '"',
|
|
'Content-Length' => strlen($pdf)
|
|
]);
|
|
}
|
|
// Stream the PDF in the browser
|
|
public function stream(string $filename)
|
|
{
|
|
$pdf = $this->generate()->pdf();
|
|
return new Response($pdf, 200, [
|
|
'Content-Type' => 'application/pdf',
|
|
'Content-Disposition' => 'inline; filename="' . $filename . '"'
|
|
]);
|
|
}
|
|
}
|
|
|
|
|
|
// namespace App\Services;
|
|
// use Spatie\Browsershot\Browsershot;
|
|
// use Illuminate\Http\Response;
|
|
// class PdfWrapper
|
|
// {
|
|
// protected Browsershot $pdfGenerator;
|
|
// protected string $html;
|
|
// public function __construct()
|
|
// {
|
|
// $this->pdfGenerator = new Browsershot();
|
|
// }
|
|
// // Load view and render HTML
|
|
// public function loadView(string $bladeFile, array $data = []): self
|
|
// {
|
|
// $this->html = view($bladeFile, $data)->render();
|
|
// return $this;
|
|
// }
|
|
// // Load raw HTML
|
|
// public function loadHtml(string $html): self
|
|
// {
|
|
// $this->html = $html;
|
|
// return $this;
|
|
// }
|
|
// // Load HTML from a URL
|
|
// public function loadUrl(string $url): self
|
|
// {
|
|
// $this->html = file_get_contents(url($url));
|
|
// return $this;
|
|
// }
|
|
// // Generate the PDF using Browsershot
|
|
// public function generate(): Browsershot
|
|
// {
|
|
// return $this->pdfGenerator
|
|
// ->html($this->html)
|
|
// ->format('A4')
|
|
// // ->margins(10, 10, 10, 10)
|
|
// ->scale(0.7)
|
|
// ->fullPage()
|
|
// ->setOption('printBackground',true)
|
|
// ->waitUntilNetworkIdle();
|
|
// }
|
|
// // Save the PDF to the server in the public folder
|
|
// public function save(string $path): void
|
|
// {
|
|
// $this->generate()->savePdf($path);
|
|
// }
|
|
// // Download the PDF with a specified filename
|
|
// public function download(string $filename)
|
|
// {
|
|
// $pdf = $this->generate()->pdf();
|
|
// return new Response($pdf, 200, [
|
|
// 'Content-Type' => 'application/pdf',
|
|
// 'Content-Disposition' => 'attachment; filename="'.$filename.'"',
|
|
// 'Content-Length' => strlen($pdf)
|
|
// ]);
|
|
// }
|
|
// // Stream the PDF in the browser
|
|
// public function stream(string $filename)
|
|
// {
|
|
// $pdf = $this->generate()->pdf();
|
|
// return new Response($pdf, 200, [
|
|
// 'Content-Type' => 'application/pdf',
|
|
// 'Content-Disposition' => 'inline; filename="'.$filename.'"'
|
|
// ]);
|
|
// }
|
|
// }
|
|
|
|
|
|
|
|
// namespace App\Services;
|
|
|
|
// use Spatie\Browsershot\Browsershot;
|
|
// use Illuminate\Http\Response;
|
|
// use Illuminate\Support\Facades\File;
|
|
|
|
// class PdfWrapper
|
|
// {
|
|
// protected Browsershot $pdfGenerator;
|
|
// protected string $html;
|
|
|
|
// public function __construct()
|
|
// {
|
|
// $this->pdfGenerator = new Browsershot();
|
|
// }
|
|
|
|
// // Load view and render HTML
|
|
// public function loadView(string $bladeFile, array $data = []): self
|
|
// {
|
|
// // Convert image paths to base64
|
|
// if (isset($data['imagePaths']) && is_array($data['imagePaths'])) {
|
|
// $data['imagesBase64'] = array_map(function($path) {
|
|
// return $this->getImageBase64($path);
|
|
// }, $data['imagePaths']);
|
|
// }
|
|
|
|
// $this->html = view($bladeFile, $data)->render();
|
|
// return $this;
|
|
// }
|
|
|
|
// // Load raw HTML
|
|
// public function loadHtml(string $html): self
|
|
// {
|
|
// $this->html = $html;
|
|
// return $this;
|
|
// }
|
|
|
|
// // Load HTML from a URL
|
|
// public function loadUrl(string $url): self
|
|
// {
|
|
// $this->html = file_get_contents(url($url));
|
|
// return $this;
|
|
// }
|
|
|
|
// // Generate the PDF using Browsershot
|
|
// public function generate(): Browsershot
|
|
// {
|
|
// return $this->pdfGenerator
|
|
// ->html($this->html)
|
|
// ->format('A4')
|
|
// ->scale(0.7)
|
|
// ->fullPage()
|
|
// ->setOption('printBackground', true)
|
|
// ->waitUntilNetworkIdle();
|
|
// }
|
|
|
|
// // Save the PDF to the server in the public folder
|
|
// public function save(string $path): void
|
|
// {
|
|
// $this->generate()->savePdf($path);
|
|
// }
|
|
|
|
// // Download the PDF with a specified filename
|
|
// public function download(string $filename)
|
|
// {
|
|
// $pdf = $this->generate()->pdf();
|
|
// return new Response($pdf, 200, [
|
|
// 'Content-Type' => 'application/pdf',
|
|
// 'Content-Disposition' => 'attachment; filename="'.$filename.'"',
|
|
// 'Content-Length' => strlen($pdf)
|
|
// ]);
|
|
// }
|
|
|
|
// // Stream the PDF in the browser
|
|
// public function stream(string $filename)
|
|
// {
|
|
// $pdf = $this->generate()->pdf();
|
|
// return new Response($pdf, 200, [
|
|
// 'Content-Type' => 'application/pdf',
|
|
// 'Content-Disposition' => 'inline; filename="'.$filename.'"'
|
|
// ]);
|
|
// }
|
|
|
|
// // Function to convert image to base64
|
|
// protected function getImageBase64($imagePath)
|
|
// {
|
|
// $path = public_path($imagePath);
|
|
// $type = pathinfo($path, PATHINFO_EXTENSION);
|
|
// $data = File::get($path);
|
|
// $base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);
|
|
|
|
// return $base64;
|
|
// }
|
|
// }
|
|
|
|
|
|
|
|
// namespace App\Services;
|
|
|
|
// use Spatie\Browsershot\Browsershot;
|
|
// use Illuminate\Http\Response;
|
|
// use Illuminate\Support\Facades\File;
|
|
|
|
// class PdfWrapper
|
|
// {
|
|
// protected Browsershot $pdfGenerator;
|
|
// protected string $html;
|
|
|
|
// public function __construct()
|
|
// {
|
|
// $this->pdfGenerator = new Browsershot();
|
|
// }
|
|
|
|
// // Load view and render HTML
|
|
// public function loadView(string $bladeFile, array $data = []): self
|
|
// {
|
|
// // Convert image paths to base64
|
|
// if (isset($data['imagePaths']) && is_array($data['imagePaths'])) {
|
|
// $data['imagesBase64'] = array_map(function ($path) {
|
|
// return $this->getImageBase64($path);
|
|
// }, $data['imagePaths']);
|
|
// }
|
|
|
|
// $this->html = view($bladeFile, $data)->render();
|
|
// return $this;
|
|
// }
|
|
|
|
|
|
|
|
// // Load raw HTML
|
|
// public function loadHtml(string $html): self
|
|
// {
|
|
// $this->html = $html;
|
|
// return $this;
|
|
// }
|
|
|
|
// // Load HTML from a URL
|
|
// public function loadUrl(string $url): self
|
|
// {
|
|
// $this->html = file_get_contents(url($url));
|
|
// return $this;
|
|
// }
|
|
|
|
// // Generate the PDF using Browsershot
|
|
// public function generate(): Browsershot
|
|
// {
|
|
// return $this->pdfGenerator
|
|
// ->html($this->html)
|
|
// ->format('A4')
|
|
// ->scale(0.7)
|
|
// ->fullPage()
|
|
// ->setOption('printBackground', true)
|
|
// ->showBackground()
|
|
// ->waitUntilNetworkIdle()
|
|
// ->timeout(60000);
|
|
// }
|
|
|
|
// // Save the PDF to the server in the public folder
|
|
// public function save(string $path): void
|
|
// {
|
|
// $this->generate()->savePdf($path);
|
|
// }
|
|
|
|
// // Download the PDF with a specified filename
|
|
// public function download(string $filename)
|
|
// {
|
|
// $pdf = $this->generate()->pdf();
|
|
// return new Response($pdf, 200, [
|
|
// 'Content-Type' => 'application/pdf',
|
|
// 'Content-Disposition' => 'attachment; filename="' . $filename . '"',
|
|
// 'Content-Length' => strlen($pdf)
|
|
// ]);
|
|
// }
|
|
|
|
// // Stream the PDF in the browser
|
|
// public function stream(string $filename)
|
|
// {
|
|
// $pdf = $this->generate()->pdf();
|
|
// return new Response($pdf, 200, [
|
|
// 'Content-Type' => 'application/pdf',
|
|
// 'Content-Disposition' => 'inline; filename="' . $filename . '"'
|
|
// ]);
|
|
// }
|
|
|
|
// // Function to convert image to base64
|
|
// protected function getImageBase64($imagePath)
|
|
// {
|
|
// $path = public_path($imagePath);
|
|
// if (!File::exists($path)) {
|
|
// return null; // Retorna null se o arquivo não existir
|
|
// }
|
|
|
|
// $type = pathinfo($path, PATHINFO_EXTENSION);
|
|
// $data = File::get($path);
|
|
// $base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);
|
|
|
|
// return $base64;
|
|
// }
|
|
|
|
// // Function to convert image paths in a collection to base64
|
|
// public function convertImagePathsToBase64($collection)
|
|
// {
|
|
// foreach ($collection as $item) {
|
|
// if (isset($item->image_paths) && is_array($item->image_paths)) {
|
|
// $item->image_paths = array_map([$this, 'getImageBase64'], $item->image_paths);
|
|
// }
|
|
// }
|
|
// }
|
|
// }
|