Skip to content

Commit

Permalink
Merge branch 'main' of github.com:spatie/laravel-pdf
Browse files Browse the repository at this point in the history
  • Loading branch information
freekmurze committed Jan 2, 2024
2 parents a8a073f + e35e1dd commit c14cd18
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 6 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Expand Down
9 changes: 7 additions & 2 deletions docs/alternatives.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
---
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:
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:

- [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.
2 changes: 1 addition & 1 deletion docs/basic-usage/responding-with-pdfs.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions docs/requirements.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
4 changes: 2 additions & 2 deletions src/FakePdfBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@

class FakePdfBuilder extends PdfBuilder
{
/** @var array<int, \Spatie\LaravelPdf\PdfBuilder> */
/** @var array<int, PdfBuilder> */
protected array $respondedWithPdf = [];

/** @var array<int, \Spatie\LaravelPdf\PdfBuilder> */
/** @var array<int, PdfBuilder> */
protected array $savedPdfs = [];

public function save(string $path): self
Expand Down
2 changes: 1 addition & 1 deletion src/Support/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
26 changes: 26 additions & 0 deletions tests/FunctionsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

use Spatie\LaravelPdf\Facades\Pdf;
use Spatie\LaravelPdf\FakePdfBuilder;
use Spatie\LaravelPdf\PdfBuilder;

use function Spatie\LaravelPdf\Support\pdf;

test('the `pdf` function returns the pdf builder instance', function () {
expect(pdf())->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']);
});

0 comments on commit c14cd18

Please sign in to comment.