Skip to content

Commit

Permalink
Merge pull request #79 from ArielMejiaDev/feature/pdf-image-asset
Browse files Browse the repository at this point in the history
add PdfImageAsset
  • Loading branch information
freekmurze authored Feb 12, 2024
2 parents b3af1cc + 90f321d commit ebe2e03
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 0 deletions.
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>

0 comments on commit ebe2e03

Please sign in to comment.