> ## Documentation Index
> Fetch the complete documentation index at: https://cloud.laravel.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Generating PDFs

> Learn how to generate PDFs with Laravel in Laravel Cloud.

<Note>This guide is for Laravel applications.</Note>

You may generate PDFs in Laravel Cloud by leveraging [Cloudflare's Browser Rendering API](https://developers.cloudflare.com/browser-rendering/rest-api/pdf-endpoint/#convert-custom-html-to-pdf). To get started, generate a Cloudflare API token that has the `Account.Browser Rendering` permission and locate your Account ID from your Cloudflare dashboard URL.

## Using Laravel PDF

[Spatie's Laravel PDF](https://spatie.be/docs/laravel-pdf) package provides a convenient wrapper around Cloudflare's Browser Rendering API via its built-in Cloudflare driver. No Node.js or Chrome binary is required on your instances.

First, install the package via Composer:

```bash theme={null}
composer require spatie/laravel-pdf
```

Then, add the following environment variables to your Cloud environment's environment variable settings:

```env theme={null}
LARAVEL_PDF_DRIVER=cloudflare
CLOUDFLARE_API_TOKEN=your-api-token
CLOUDFLARE_ACCOUNT_ID=your-account-id
```

Once configured, you may generate PDFs using the `Pdf` facade. For example, the following controller method renders a view as a PDF and streams it as a download to the browser:

```php theme={null}
use Spatie\LaravelPdf\Facades\Pdf;

public function download()
{
    return Pdf::view('pdfs.invoice', ['invoice' => $invoice])
        ->format('a4')
        ->download('invoice.pdf');
}
```

For more information on available options such as custom paper sizes, margins, headers, and footers, please refer to the [Laravel PDF documentation](https://spatie.be/docs/laravel-pdf).

## Using Laravel Cashier with Laravel PDF

Cashier's default invoice PDF renderer uses Dompdf, which does not work in Laravel Cloud.

To generate Cashier invoices on Cloud, install Spatie's Laravel PDF package and configure the Cloudflare environment variables as described [above](#using-laravel-pdf):

```bash theme={null}
composer require spatie/laravel-pdf
```

Then, set Cashier to use its built-in Laravel PDF invoice renderer by adding the following environment variable to your Cloud environment's environment variable settings:

```env theme={null}
CASHIER_INVOICE_RENDERER=Laravel\Cashier\Invoices\LaravelPdfInvoiceRenderer
```

Cashier will automatically resolve this renderer at runtime and use Spatie Laravel PDF when generating invoice downloads.

## Manual Usage

If you prefer to call the Cloudflare API directly without an additional dependency, add a `cloudflare` entry to your `services.php` config file:

```php theme={null}
'cloudflare' => [
    'api_token' => env('CLOUDFLARE_API_TOKEN'),
    'account_id' => env('CLOUDFLARE_ACCOUNT_ID'),
],
```

Next, set these environment variables in your Cloud environment's environment variable settings.

Now you may call the Cloudflare PDF API using Laravel's built-in `Http` facade. The following example demonstrates how to generate HTML from a view and stream the response as a downloadable PDF to the browser from a controller method:

```php theme={null}
use Illuminate\Support\Facades\Http;

public function download()
{
    return response()->streamDownload(function () {
        $token = config('services.cloudflare.api_token');
        $accountId = config('services.cloudflare.account_id');

        echo Http::withToken($token)
            ->post('https://api.cloudflare.com/client/v4/accounts/'.$accountId.'/browser-rendering/pdf', [
                'html' => view('view-to-render')->render(),
            ])
            ->body();
    }, 'file.pdf', [
        'Content-Type' => 'application/pdf',
    ]);
}
```
