Skip to content

Commit

Permalink
add javascript
Browse files Browse the repository at this point in the history
  • Loading branch information
freekmurze committed Dec 29, 2023
1 parent 16f40d9 commit c1713b2
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
25 changes: 25 additions & 0 deletions docs/basic-usage/creating-pdfs.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,28 @@ use Spatie\LaravelPdf\Facades\Pdf;

Pdf::html('<h1>Hello world!!</h1>')->save('/some/directory/invoice.pdf');
```

## Using Javascript

The JavaScript in your HTML will be executed when the PDF is created. You could use this to have a JavaScript charting library render a chart.

Here's a simple example. If you have this Blade view...

```blade
<div id="target"></div>
<script>
document.getElementById('target').innerHTML = 'hello';
</script>
```

... and render it with this code...

```php
use Spatie\LaravelPdf\Facades\Pdf;

Pdf::view('your-view')->save($pathToPdf);
```

... the text `hello` will be visible in the PDF.

6 changes: 6 additions & 0 deletions tests/PdfTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,9 @@
expect(getTempPath('second.pdf'))
->toHaveDimensions(612, 792);
});

it('will execute javascript', function() {
Pdf::view('javascript')->save($this->targetPath);

expect($this->targetPath)->toContainText('hello');
});
5 changes: 5 additions & 0 deletions workbench/resources/views/javascript.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<div id="target"></div>

<script>
document.getElementById('target').innerHTML = 'hello';
</script>

0 comments on commit c1713b2

Please sign in to comment.