Skip to content

Commit

Permalink
Rename PDF facade to Pdf (#899)
Browse files Browse the repository at this point in the history
* Update composer.json

* Update PdfTest.php

* Update TestCase.php

* Update composer.json

* Update PdfTest.php

* Update phpstan.neon

* Update PdfTest.php

* Update readme.md
  • Loading branch information
barryvdh authored Jul 6, 2022
1 parent f7384cf commit a230bdf
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 10 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"Barryvdh\\DomPDF\\ServiceProvider"
],
"aliases": {
"Pdf": "Barryvdh\\DomPDF\\Facade\\Pdf",
"PDF": "Barryvdh\\DomPDF\\Facade\\Pdf"
}
}
Expand Down
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ parameters:
ignoreErrors:
# This is a global alias that cannot be detected by Larastan.
- '#Call to static method loadHtml\(\) on an unknown class PDF\.#'
- '#Call to static method loadHtml\(\) on an unknown class Pdf\.#'
# This is a magic method that cannot be detected by Larastan.
- '#Call to an undefined method Dompdf\\Dompdf::loadView\(\)\.#'
22 changes: 12 additions & 10 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ To change the configuration, copy the config file to your config folder and enab

You can create a new DOMPDF instance and load a HTML string, file or view name. You can save it to a file, or stream (show in browser) or download.

```php
use Barryvdh\DomPDF\Facade\Pdf;

$pdf = Pdf::loadView('pdf.invoice', $data);
return $pdf->download('invoice.pdf');
```

or use the App container:

```php
$pdf = App::make('dompdf.wrapper');
$pdf->loadHTML('<h1>Test</h1>');
Expand All @@ -41,23 +50,16 @@ You can create a new DOMPDF instance and load a HTML string, file or view name.

Or use the facade:

```php
use PDF;

$pdf = PDF::loadView('pdf.invoice', $data);
return $pdf->download('invoice.pdf');
```

You can chain the methods:

```php
return PDF::loadFile(public_path().'/myfile.html')->save('/path-to/my_stored_file.pdf')->stream('download.pdf');
return Pdf::loadFile(public_path().'/myfile.html')->save('/path-to/my_stored_file.pdf')->stream('download.pdf');
```

You can change the orientation and paper size, and hide or show errors (by default, errors are shown when debug is on)

```php
PDF::loadHTML($html)->setPaper('a4', 'landscape')->setWarnings(false)->save('myfile.pdf')
Pdf::loadHTML($html)->setPaper('a4', 'landscape')->setWarnings(false)->save('myfile.pdf')
```

If you need the output as a string, you can get the rendered PDF with the output() function, so you can save/output it yourself.
Expand All @@ -73,7 +75,7 @@ The defaults configuration settings are set in `config/dompdf.php`. Copy this fi

You can still alter the dompdf options in your code before generating the pdf using this command:
```php
PDF::setOption(['dpi' => 150, 'defaultFont' => 'sans-serif']);
Pdf::setOption(['dpi' => 150, 'defaultFont' => 'sans-serif']);
```

Available options and their defaults:
Expand Down
12 changes: 12 additions & 0 deletions tests/PdfTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@
class PdfTest extends TestCase
{
public function testAlias(): void
{
$pdf = \Pdf::loadHtml('<h1>Test</h1>');
/** @var Response $response */
$response = $pdf->download('test.pdf');

$this->assertInstanceOf(Response::class, $response);
$this->assertNotEmpty($response->getContent());
$this->assertEquals('application/pdf', $response->headers->get('Content-Type'));
$this->assertEquals('attachment; filename="test.pdf"', $response->headers->get('Content-Disposition'));
}

public function testAliasCaps(): void
{
$pdf = \PDF::loadHtml('<h1>Test</h1>');
/** @var Response $response */
Expand Down
1 change: 1 addition & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ protected function getPackageAliases($app)
{
return [
'PDF' => Pdf::class,
'Pdf' => Pdf::class,
];
}
}

0 comments on commit a230bdf

Please sign in to comment.