Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
freekmurze committed Dec 29, 2023
1 parent b61e408 commit d9d4655
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 3 deletions.
50 changes: 50 additions & 0 deletions docs/advanced-usage/creating-pdfs-with-multiple-pages.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
---
title: Creating PDFs with multiple pages
weight: 2
---

This packages offers a couple of Blade directives to help you create PDFs with multiple pages.

## Setting a page break

To create a PDF with multiple pages, you can use the `@pageBreak` Blade directive in your view. Using this directive will result in a new page being created in the PDF document.

So if you have a view like this...

```blade
<div>
Page 1
</div>
@pageBreak
<div>
Page 2
</div>
```

... and you render this view using ...

```php
Pdf::view('view-with-multiple-pages')->save($path);
```

... the resulting PDF will have two pages, one with "Page 1" and one with "Page 2".

## Adding page numbers

To add page numbers to your PDF, you can use the `@pageNumber` and `@totalPages` Blade directive in your view.

Imagine you have this footer view...

```blade
<div>
This is page @pageNumber of @totalPages
</div>
```

... and you render this view using ...

```php
Pdf::view('view-with-multiple-pages')->footer('footer-view')->save($path);
```

... the resulting PDF will have a footer on each page, with the page number and the total number of pages.
2 changes: 1 addition & 1 deletion docs/advanced-usage/customizing-browsershot.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: Customizing Browsershot
weight: 1
weight: 2
---

Under the hood, Laravel PDF uses [Browsershot](https://spatie.be/docs/browsershot) to generate the PDFs. While Laravel PDF provides a simple interface to generate PDFs, you can still use Browsershot directly to customize the PDFs.
Expand Down
2 changes: 1 addition & 1 deletion docs/advanced-usage/generating-pdfs-on-aws-lambda.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: Generating PDFs on AWS Lambda
weight: 2
weight: 3
---

Coming soon...
16 changes: 16 additions & 0 deletions src/PdfServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Spatie\LaravelPdf;

use Illuminate\Support\Facades\Blade;
use Spatie\LaravelPackageTools\Package;
use Spatie\LaravelPackageTools\PackageServiceProvider;

Expand All @@ -11,4 +12,19 @@ public function configurePackage(Package $package): void
{
$package->name('laravel-pdf');
}

public function bootingPackage()
{
Blade::directive('pageBreak', function () {
return "<?php echo '<div style=\"page-break-after: always;\"></div>'; ?>";
});

Blade::directive('pageNumber', function () {
return "<?php echo '<span class=\"pageNumber\"></span>'; ?>";
});

Blade::directive('totalPages', function () {
return "<?php echo '<span class=\"totalPages\"></span>'; ?>";
});
}
}
22 changes: 22 additions & 0 deletions tests/BladeDirectivesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

use Spatie\LaravelPdf\Facades\Pdf;

beforeEach(function () {
$this->targetPath = getTempPath('test.pdf');
});

it('can set a page break', function () {
Pdf::view('blade-directives.body')
->save($this->targetPath);

expect($this->targetPath)->toHavePageCount(2);
});

it('can display the number of pages', function() {
Pdf::view('blade-directives.body')
->footerView('blade-directives.footer')
->save($this->targetPath);

expect($this->targetPath)->toContainText('page 1 of 2');
});
10 changes: 9 additions & 1 deletion tests/Pest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ function assertMatchesPdfSnapshot(string $pdfPath): void
? '/usr/bin/pdftotext'
: '/opt/homebrew/bin/pdftotext';

$actualText = \Spatie\PdfToText\Pdf::getText($this->value, $binPath);
$path = $this->value;

$actualText = \Spatie\PdfToText\Pdf::getText($path, $binPath);

if (is_string($expectedText)) {
$expectedText = [$expectedText];
Expand All @@ -73,6 +75,12 @@ function assertMatchesPdfSnapshot(string $pdfPath): void
}
});

expect()->extend('toHavePageCount', function(int $expectedNumberOfPages) {
$image = new Imagick();
$image->pingImage($this->value);
expect($image->getNumberImages())->toBe($expectedNumberOfPages);
});

function convertPdfToImage(string $pdfPath): string
{
$imagePath = getTempPath('test'.'.png');
Expand Down
7 changes: 7 additions & 0 deletions workbench/resources/views/blade-directives/body.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<div>
First page
</div>
@pageBreak
<div>
Second page
</div>
4 changes: 4 additions & 0 deletions workbench/resources/views/blade-directives/footer.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<div style="font-size: 20px">
<h1>Footer</h1>
This is page @pageNumber of @totalPages
</div>

0 comments on commit d9d4655

Please sign in to comment.