Skip to content

Commit

Permalink
Merge pull request #196 from aalyusuf/main
Browse files Browse the repository at this point in the history
Fix inconsistent behavior in save() method when using vs not using a disk
  • Loading branch information
freekmurze authored Dec 9, 2024
2 parents 3b6692f + 58fb198 commit b5e7306
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/PdfBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Spatie\LaravelPdf\Enums\Format;
use Spatie\LaravelPdf\Enums\Orientation;
use Spatie\LaravelPdf\Enums\Unit;
use Spatie\TemporaryDirectory\TemporaryDirectory;
use Wnx\SidecarBrowsershot\BrowsershotLambda;

class PdfBuilder implements Responsable
Expand Down Expand Up @@ -257,10 +258,19 @@ public function disk(string $diskName, string $visibility = 'private'): self

protected function saveOnDisk(string $diskName, string $path): self
{
$pdfContent = $this->getBrowsershot()->pdf();
$visibility = $this->visibility;
$fileName = pathinfo($path, PATHINFO_BASENAME);

Storage::disk($diskName)->put($path, $pdfContent, $visibility);
$temporaryDirectory = (new TemporaryDirectory)->create();

$this->getBrowsershot()->save($temporaryDirectory->path($fileName));

$content = file_get_contents($temporaryDirectory->path($fileName));

$temporaryDirectory->delete();

$visibility = $this->visibility;

Storage::disk($diskName)->put($path, $content, $visibility);

return $this;
}
Expand Down
19 changes: 19 additions & 0 deletions tests/PdfTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,22 @@

expect($this->targetPath)->toContainText('hello');
});

it('can save as png in local and disk', function () {
Storage::fake('local');

$firstPath = getTempPath('first.png');
Pdf::view('test')
->save($firstPath);

expect(mime_content_type($firstPath))
->toBe('image/png');

Pdf::view('test')
->disk('local')
->save('second.png');

expect(Storage::disk('local')
->mimeType('second.png'))
->toBe('image/png');
});

0 comments on commit b5e7306

Please sign in to comment.