From 2b50ff5ebdb56c1b2c4882d2c844988f23839a28 Mon Sep 17 00:00:00 2001 From: Mansoor Khan Date: Tue, 2 Jan 2024 09:27:51 +0500 Subject: [PATCH 1/8] Add SnapPDF to alternatives.md --- docs/alternatives.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/alternatives.md b/docs/alternatives.md index 83a0d06..b4b69af 100644 --- a/docs/alternatives.md +++ b/docs/alternatives.md @@ -1,12 +1,13 @@ --- title: Alternatives -weight: 5 +weight: 6 --- Laravel PDF uses Chrome Headless to generate PDFs. This is a great solution for most use cases. You can use any CSS you want, and it will be rendered correctly. However, generating a PDF this way can be resource intensive. If you don't like the trade-off mentioned above, here are some alternatives to generate PDFs: +- [SnapPDF](https://github.com/beganovich/snappdf) - Convert webpages or HTML into the PDF file using Chromium-powered browsers. It communicates directly with browser itself and it takes less than .5s to generate PDFs (with cold start). - [laravel-dompdf](https://github.com/barryvdh/laravel-dompdf) - A DOMPDF Wrapper for Laravel - [wkhtmltopdf](http://wkhtmltopdf.org/) - A command line tool to render HTML into PDF and various image formats using the QT Webkit rendering engine. This is the engine used behind the scenes in Snappy. - [mPDF](http://www.mpdf1.com/mpdf/index.php) - A PHP class to generate PDF files from HTML with Unicode/UTF-8 and CJK support. From 48b12c1c1de6f0581ee3f2c9051a7a8d68cbaea5 Mon Sep 17 00:00:00 2001 From: Freek Van der Herten Date: Tue, 2 Jan 2024 08:58:53 +0100 Subject: [PATCH 2/8] Update alternatives.md --- docs/alternatives.md | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/docs/alternatives.md b/docs/alternatives.md index b4b69af..0f88f41 100644 --- a/docs/alternatives.md +++ b/docs/alternatives.md @@ -5,10 +5,14 @@ weight: 6 Laravel PDF uses Chrome Headless to generate PDFs. This is a great solution for most use cases. You can use any CSS you want, and it will be rendered correctly. However, generating a PDF this way can be resource intensive. -If you don't like the trade-off mentioned above, here are some alternatives to generate PDFs: +If you don't like the trade-off mentioned above, here are some alternatives to generate PDFs which don't use Chomium under the hood: -- [SnapPDF](https://github.com/beganovich/snappdf) - Convert webpages or HTML into the PDF file using Chromium-powered browsers. It communicates directly with browser itself and it takes less than .5s to generate PDFs (with cold start). - [laravel-dompdf](https://github.com/barryvdh/laravel-dompdf) - A DOMPDF Wrapper for Laravel - [wkhtmltopdf](http://wkhtmltopdf.org/) - A command line tool to render HTML into PDF and various image formats using the QT Webkit rendering engine. This is the engine used behind the scenes in Snappy. - [mPDF](http://www.mpdf1.com/mpdf/index.php) - A PHP class to generate PDF files from HTML with Unicode/UTF-8 and CJK support. - [FPDF](http://www.fpdf.org/) - A PHP class for generating PDF files on-the-fly. + + +These do use Chromium: + +- [SnapPDF](https://github.com/beganovich/snappdf) - Convert webpages or HTML into the PDF file using Chromium-powered browsers. From 330e7c019d0444321eedc50a269697d82fbf3038 Mon Sep 17 00:00:00 2001 From: Enzo Innocenzi Date: Tue, 2 Jan 2024 11:34:58 +0100 Subject: [PATCH 3/8] fix(functions): allow calling `pdf` without argument --- src/Support/functions.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Support/functions.php b/src/Support/functions.php index 007efd0..7a6c151 100644 --- a/src/Support/functions.php +++ b/src/Support/functions.php @@ -5,7 +5,7 @@ use Spatie\LaravelPdf\Facades\Pdf; use Spatie\LaravelPdf\PdfBuilder; -function pdf(string $viewPath, array $data = []): PdfBuilder +function pdf(string $viewPath = '', array $data = []): PdfBuilder { return Pdf::view($viewPath, $data); } From ebdfdd323991025ef2a09843aabb9ed387f0d04f Mon Sep 17 00:00:00 2001 From: Alex Vanderbist Date: Tue, 2 Jan 2024 11:36:49 +0100 Subject: [PATCH 4/8] Add note about browsershot dependencies to docs --- docs/requirements.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/requirements.md b/docs/requirements.md index d769b90..11aedd6 100644 --- a/docs/requirements.md +++ b/docs/requirements.md @@ -4,3 +4,5 @@ weight: 3 --- The laravel-pdf package requires **PHP 8.2+**, **Laravel 10+**. + +Under the hood this package uses [Browsershot](https://spatie.be/docs/browsershot) to generate PDFs. You can find the necessary requirements [here](https://spatie.be/docs/browsershot/v4/requirements). From 59c19851d4ee68e6c60cd2347441a655bc139fca Mon Sep 17 00:00:00 2001 From: Alex Vanderbist Date: Tue, 2 Jan 2024 11:37:21 +0100 Subject: [PATCH 5/8] Update docs wording --- docs/basic-usage/responding-with-pdfs.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/basic-usage/responding-with-pdfs.md b/docs/basic-usage/responding-with-pdfs.md index db1c65c..1484f31 100644 --- a/docs/basic-usage/responding-with-pdfs.md +++ b/docs/basic-usage/responding-with-pdfs.md @@ -3,7 +3,7 @@ title: Responding with PDFs weight: 2 --- -In a controller, you can create and return a PDF by using the `pdf()` function. +In a controller, you can create and return a PDF by using the `pdf()` helper function. ```php use function Spatie\LaravelPdf\Support\pdf; From 07c3c7ba7bfe8415b48e34472e21bd878a398c13 Mon Sep 17 00:00:00 2001 From: Enzo Innocenzi Date: Tue, 2 Jan 2024 11:39:09 +0100 Subject: [PATCH 6/8] test(functions): improve coverage --- tests/FunctionsTest.php | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 tests/FunctionsTest.php diff --git a/tests/FunctionsTest.php b/tests/FunctionsTest.php new file mode 100644 index 0000000..cebbb78 --- /dev/null +++ b/tests/FunctionsTest.php @@ -0,0 +1,26 @@ +toBeInstanceOf(PdfBuilder::class); +}); + +test('the `pdf` function respect fakes', function () { + Pdf::fake(); + + expect(pdf())->toBeInstanceOf(FakePdfBuilder::class); +}); + +test('the `pdf` function accepts a view and parameters', function () { + Pdf::fake(); + + expect(pdf('foo', ['bar' => 'bax'])) + ->toBeInstanceOf(FakePdfBuilder::class) + ->viewName->toBe('foo') + ->viewData->toBe(['bar' => 'bax']); +}); From bea48597a79a37b242bf12fe2fc9a791c4903295 Mon Sep 17 00:00:00 2001 From: Alex Vanderbist Date: Tue, 2 Jan 2024 11:39:57 +0100 Subject: [PATCH 7/8] Simplify FQCN --- src/FakePdfBuilder.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/FakePdfBuilder.php b/src/FakePdfBuilder.php index 8a8a909..2b6f195 100644 --- a/src/FakePdfBuilder.php +++ b/src/FakePdfBuilder.php @@ -8,10 +8,10 @@ class FakePdfBuilder extends PdfBuilder { - /** @var array */ + /** @var array */ protected array $respondedWithPdf = []; - /** @var array */ + /** @var array */ protected array $savedPdfs = []; public function save(string $path): self From e40efb960d9458fb41b1f2b16c368283c4928c65 Mon Sep 17 00:00:00 2001 From: Alex Vanderbist Date: Tue, 2 Jan 2024 11:52:21 +0100 Subject: [PATCH 8/8] Add docs on running tests --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index 17e7166..8f043b8 100644 --- a/README.md +++ b/README.md @@ -68,6 +68,12 @@ All documentation is available [on our documentation site](https://spatie.be/doc ## Testing +For running the testsuite, you'll need to have Puppeteer installed. Pleaser refer to the Browsershot requirements [here](https://spatie.be/docs/browsershot/v4/requirements). Usually `npm -g i puppeteer` will do the trick. + +Additionally, you'll need the `pdftotext` CLI which is part of the poppler-utils package. More info can be found in in the [spatie/pdf-to-text readme](https://github.com/spatie/pdf-to-text?tab=readme-ov-file#requirements). Usually `brew install poppler-utils` will suffice. + +Finally run the tests with: + ```bash composer test ```