Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add PdfImageAsset #79

Merged
merged 6 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions docs/basic-usage/formatting-pdfs.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ Inside the footer, you can use the following Blade directives:
- `@pageNumber`: The current page number
- `@totalPages`: The total number of pages

### Display Images in Headers and Footers

You can add an image using the blade directive `@inlinedImage`

It supports absolute and relative paths inside your app `public` directory

```php
@inlinedImage($url)
```

## Page orientation

By default, all PDFs are created in portrait mode. You can change this by calling the `landscape` method.
Expand Down
22 changes: 22 additions & 0 deletions src/PdfServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
namespace Spatie\LaravelPdf;

use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Str;
use RuntimeException;
use Spatie\LaravelPackageTools\Package;
use Spatie\LaravelPackageTools\PackageServiceProvider;

Expand All @@ -26,5 +29,24 @@ public function bootingPackage()
Blade::directive('totalPages', function () {
return "<?php echo '<span class=\"totalPages\"></span>'; ?>";
});

Blade::directive('inlinedImage', function ($url) {
$url = Str::of($url)->trim("'")->trim('"')->value();

if (!Str::of($url)->isUrl()) {
$imageContent = 'data:image/png;base64,' . base64_encode(file_get_contents(public_path($url)));
return "<?php echo '<img src=\"$imageContent\">'; ?>";
}

$response = Http::get($url);

if ($response->successful()) {
$imageContent = 'data:image/png;base64,' . base64_encode($response->body());
return "<?php echo '<img src=\"$imageContent\">'; ?>";
}

throw new RuntimeException('Failed to fetch the image');
});

}
}
8 changes: 8 additions & 0 deletions tests/BladeDirectivesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,11 @@

expect($this->targetPath)->toContainText('page 1 of 2');
});

it('can display an image', function () {
Pdf::view('blade-directives.body')
->headerView('blade-directives.header')
->save($this->targetPath);

expect($this->targetPath)->toContainText('<img src="data:image/png;base64,');
});
29 changes: 29 additions & 0 deletions workbench/resources/views/blade-directives/header.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<style>
.header {
font-size: 12px;
margin: 0 auto;
}

.header figure {
margin-top: 10px;
}

.header div {
display: flex;
align-items: center;
}

.header figure img {
height: 40px;
width: 40px;
}
</style>

<div class="header">
<div>
<h1>Header</h1>
<figure>
@inlinedImage('https://avatars.githubusercontent.com/u/7535935?s=200&v=4')
</figure>
</div>
</div>