Skip to content

Commit

Permalink
Merge pull request #85 from ArielMejiaDev/fix/image-directive
Browse files Browse the repository at this point in the history
adding a fix to support variables as params
  • Loading branch information
freekmurze committed Feb 22, 2024
2 parents 8dfb007 + 917a207 commit 0371d5f
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 24 deletions.
9 changes: 7 additions & 2 deletions docs/basic-usage/formatting-pdfs.md
Expand Up @@ -52,10 +52,15 @@ Inside the footer, you can use the following Blade directives:

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

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

```php
@inlinedImage($url)
// using relative path
@php $logo = public_path('assets/logo.png'); @endphp
@inlinedImage($logo)

// using absolute path
@inlinedImage('https://some-url/assets/some-logo.png')
```

## Page orientation
Expand Down
41 changes: 21 additions & 20 deletions src/PdfServiceProvider.php
Expand Up @@ -3,9 +3,6 @@
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 @@ -31,23 +28,27 @@ public function bootingPackage()
});

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');
return "<?php
\$url = \Illuminate\Support\Str::of($url)->trim(\"'\")->trim('\"')->value();
if (! \Illuminate\Support\Str::of(\$url)->isUrl()) {
try {
\$imageContent = 'data:image/png;base64,' . base64_encode(file_get_contents(\$url));
echo '<img src=\"' . \$imageContent . '\">';
} catch(\Exception \$exception) {
throw new \Illuminate\View\ViewException('Image not found: ' . \$exception->getMessage());
}
} else {
\$response = \Illuminate\Support\Facades\Http::get(\$url);
if (! \$response->successful()) {
throw new \Illuminate\View\ViewException('Failed to fetch the image: ' . \$response->toException());
}
\$imageContent = 'data:image/png;base64,' . base64_encode(\$response->body());
echo '<img src=\"' . \$imageContent . '\">';
}
?>";
});

}
Expand Down
54 changes: 52 additions & 2 deletions tests/BladeDirectivesTest.php
Expand Up @@ -21,9 +21,59 @@
expect($this->targetPath)->toContainText('page 1 of 2');
});

it('can display an image', function () {
Pdf::view('blade-directives.inlined-image')
it('can display an image using a static string with an absolute path', function () {
Pdf::view('blade-directives.body')
->headerView('blade-directives.image-header-using-a-static-absolute-path')
->save($this->targetPath);

expect(true)->toBeTrue();
});

it('can display an image using a static string with an relative path', function () {
Pdf::view('blade-directives.body')
->headerView('blade-directives.image-header-using-a-static-relative-path')
->save($this->targetPath);

expect(true)->toBeTrue();
});

it('can display an image using a variable with an absolute path', function () {
Pdf::view('blade-directives.body')
->headerView('blade-directives.image-header-using-a-variable', [
'logo' => 'https://avatars.githubusercontent.com/u/7535935?s=200&v=4',
])
->save($this->targetPath);

expect(true)->toBeTrue();
});

it('can display an image using a variable with an relative path', function () {

$logoPath = \Orchestra\Testbench\workbench_path('public/assets/logo.png');

Pdf::view('blade-directives.body')
->headerView('blade-directives.image-header-using-a-variable', [
'logo' => "../../../../../../../../../$logoPath",
])
->save($this->targetPath);

expect(true)->toBeTrue();
});

it('can throw view exception with image relative path', function () {

Pdf::view('blade-directives.body')
->headerView('blade-directives.image-header-using-a-variable', [
'logo' => "./not-found.png",
])
->save($this->targetPath);
})->throws(\Illuminate\View\ViewException::class, 'Image not found:');

it('can throw view exception with image absolute path', function () {

Pdf::view('blade-directives.body')
->headerView('blade-directives.image-header-using-a-variable', [
'logo' => 'https://picsum.photos/not-found',
])
->save($this->targetPath);
})->throws(\Illuminate\View\ViewException::class, 'Failed to fetch the image:');
Binary file added workbench/public/assets/logo.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@@ -0,0 +1,33 @@
<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>

@php
$logo = \Orchestra\Testbench\workbench_path('public/assets/logo.png');
@endphp

<div class="header">
<div>
<h1>Another Header</h1>
<figure>
@inlinedImage($logo)
</figure>
</div>
</div>
@@ -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>Another Header</h1>
<figure>
@inlinedImage($logo)
</figure>
</div>
</div>

0 comments on commit 0371d5f

Please sign in to comment.