Creating PDFs | laravel-pdf | Spatie

 SPATIE

  Laravel PDF
==============

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-pdf](https://spatie.be/docs/laravel-pdf/v1)  Basic-usage  Creating PDFs

 Version   v2   v1

 Other versions for crawler [v2](https://spatie.be/docs/laravel-pdf/v2) [v1](https://spatie.be/docs/laravel-pdf/v1)

- [ Introduction ](https://spatie.be/docs/laravel-pdf/v1/introduction)
- [ Support us ](https://spatie.be/docs/laravel-pdf/v1/support-us)
- [ Requirements ](https://spatie.be/docs/laravel-pdf/v1/requirements)
- [ Installation &amp; setup ](https://spatie.be/docs/laravel-pdf/v1/installation-setup)
- [ Questions and issues ](https://spatie.be/docs/laravel-pdf/v1/questions-issues)
- [ Alternatives ](https://spatie.be/docs/laravel-pdf/v1/alternatives)
- [ Changelog ](https://spatie.be/docs/laravel-pdf/v1/changelog)
- [ About us ](https://spatie.be/docs/laravel-pdf/v1/about-us)

Basic usage
-----------

- [ Creating PDFs ](https://spatie.be/docs/laravel-pdf/v1/basic-usage/creating-pdfs)
- [ Responding with PDFs ](https://spatie.be/docs/laravel-pdf/v1/basic-usage/responding-with-pdfs)
- [ Formatting PDFs ](https://spatie.be/docs/laravel-pdf/v1/basic-usage/formatting-pdfs)
- [ Saving PDFs to disks ](https://spatie.be/docs/laravel-pdf/v1/basic-usage/saving-pdfs-to-disks)
- [ Testing PDFs ](https://spatie.be/docs/laravel-pdf/v1/basic-usage/testing-pdfs)
- [ Setting defaults ](https://spatie.be/docs/laravel-pdf/v1/basic-usage/setting-defaults)

Advanced usage
--------------

- [ Configuration ](https://spatie.be/docs/laravel-pdf/v1/advanced-usage/configuration)
- [ Creating PDFs with multiple pages ](https://spatie.be/docs/laravel-pdf/v1/advanced-usage/creating-pdfs-with-multiple-pages)
- [ Customizing Browsershot ](https://spatie.be/docs/laravel-pdf/v1/advanced-usage/customizing-browsershot)
- [ Generating PDFs on AWS Lambda ](https://spatie.be/docs/laravel-pdf/v1/advanced-usage/generating-pdfs-on-aws-lambda)
- [ Using Tailwind ](https://spatie.be/docs/laravel-pdf/v1/advanced-usage/using-tailwind)
- [ Extending with Macros ](https://spatie.be/docs/laravel-pdf/v1/advanced-usage/extending-with-macros)

      You are viewing the documentation for **an older version** of this package. You can check the version you are using with the following command:

 `                                    composer show spatie/laravel-pdf                                                                                                                                                                                                                                    `

Creating PDFs
=============

###  On this page

1. [ Using Javascript ](#content-using-javascript)

This package can be used to create PDFs from HTML. In a Laravel application the easiest way to generate some HTML is to use a Blade view.

Here's an example where we are going to create a PDF from a Blade view.

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

Pdf::view('pdf.invoice')->save('/some/directory/invoice.pdf');
```

As a second parameter you can pass an array of data that will be made available in the view. You might use that to pass an Eloquent model, such as an invoice, to the view.

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

Pdf::view('pdf.invoice', ['invoice' => $invoice])
    ->save('/some/directory/invoice.pdf');
```

You can also create a PDF from a string of HTML.

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

Pdf::html('Hello world!!')->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...

```

    document.getElementById('target').innerHTML = 'hello';

```

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

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

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

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