Skip to content

Commit

Permalink
make download name optional
Browse files Browse the repository at this point in the history
  • Loading branch information
freekmurze committed Mar 4, 2024
1 parent 592e9ea commit b263e83
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
4 changes: 1 addition & 3 deletions src/PdfBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,15 +137,13 @@ public function footerHtml(string $html): self

public function download(?string $downloadName = null): self
{
$this->name($downloadName);
$this->name($downloadName ?? 'download');

$this->addHeaders([
'Content-Type' => 'application/pdf',
'Content-Disposition' => 'attachment; filename="'.$this->downloadName.'"',
]);

$this->name($downloadName);

return $this;
}

Expand Down
9 changes: 8 additions & 1 deletion tests/PdfReponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,20 @@
->assertHeader('content-disposition', 'inline; filename="my-custom-name.pdf"');
});

it('can download the pdf', function () {
it('can download the pdf with a name', function () {
$this
->get('download-pdf')
->assertHeader('content-type', 'application/pdf')
->assertHeader('content-disposition', 'attachment; filename="my-custom-name.pdf"');
});

it('can download the pdf without a name', function () {
$this
->get('download-nameless-pdf')
->assertHeader('content-type', 'application/pdf')
->assertHeader('content-disposition', 'attachment; filename="download.pdf"');
});

it('will tack on pdf to the filename if it is missing', function (string $method) {
$headerMethod = $method === 'inline' ? 'inline' : 'attachment';

Expand Down
4 changes: 4 additions & 0 deletions workbench/routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,7 @@
Route::get('download-pdf', function () {
return pdf('test')->download('my-custom-name.pdf');
});

Route::get('download-nameless-pdf', function () {
return pdf('test')->download();
});

0 comments on commit b263e83

Please sign in to comment.