Building the Output
Now that you have prepared a source, you are ready to build the output and return it to your user or save it to disk (or do whatever you'd like to do with it).
To do this, you can call the ->build() method on the factory, which will return an instance of WeasyPrint\Objects\Output:
$output = $factory->build();Using the Output
The Output object makes the following methods available:
stream()
This method creates a Symfony StreamedResponse that may be used to download or inline the PDF to the client (such as a browser).
public function stream(
string $filename,
array $headers = [],
StreamMode $mode = StreamMode::INLINE,
): StreamedResponseThe $headers parameter allows you to set custom HTTP response headers. For example, you might add cache control headers or custom application headers.
The $mode parameter sets the Content-Disposition HTTP header, which controls how the browser handles the PDF:
| Mode | Value | Behavior |
|---|---|---|
StreamMode::INLINE (default) | inline | Display the PDF in the browser |
StreamMode::DOWNLOAD | attachment | Force the browser to download the PDF |
Examples:
use Symfony\Component\HttpFoundation\StreamedResponse;
use WeasyPrint\WeasyPrintFactory;
public function generatePdf(): StreamedResponse
{
$factory = new WeasyPrintFactory()
->prepareSource('<p>WeasyPrint rocks!</p>');
$output = $factory->build();
return $output->stream('weasyprint-rocks.pdf');
}For more control, use stream() with custom headers or a different mode:
use WeasyPrint\Enums\StreamMode;
// Force the browser to download the PDF instead of displaying it:
$response = $output->stream(
'weasyprint-rocks.pdf',
mode: StreamMode::DOWNLOAD,
);
// Add custom headers (while keeping default inline behavior):
$response = $output->stream(
'weasyprint-rocks.pdf',
headers: ['X-Custom-Header' => 'value'],
);
// Do both things at the same time:
$response = $output->stream(
'weasyprint-rocks.pdf',
headers: ['X-Custom-Header' => 'value'],
mode: StreamMode::DOWNLOAD,
);Working with Streamed Responses
For more information on working with the $response from these examples, see the Symfony StreamedResponse documentation.
download()
This is a shorthand for stream() using StreamMode::DOWNLOAD.
public function download(
string $filename,
array $headers = [],
): StreamedResponseExamples:
// Force the browser to download the PDF:
$response = $output->download('weasyprint-rocks.pdf');
// With custom headers:
$response = $output->download(
'weasyprint-rocks.pdf',
headers: ['X-Custom-Header' => 'value'],
);inline()
This is a shorthand for stream() using StreamMode::INLINE (the default behavior of stream()).
public function inline(
string $filename,
array $headers = [],
): StreamedResponseExamples:
// Display the PDF in the browser:
$response = $output->inline('weasyprint-rocks.pdf');
// With custom headers:
$response = $output->inline(
'weasyprint-rocks.pdf',
headers: ['X-Custom-Header' => 'value'],
);As a string
Beyond streaming the PDF to the browser, you can also get the underlying string data and do whatever you'd like with it.
Stringable
The Output object implements \Stringable, and so may be consumed as a string. This can be useful in cases where you want to pass it to a class, method or function that expects a string.
For example, you can write directly to disk:
file_put_contents('weasyprint-rocks.pdf', $output);When assigning to a variable, you can also cast it:
$pdfData = (string) $output;Flysystem + AWS
You can use Flysystem to store PDFs directly to AWS S3:
use League\Flysystem\AwsS3V3\AwsS3V3Adapter;
use League\Flysystem\Filesystem;
use Aws\S3\S3Client;
$s3Client = new S3Client([
'version' => 'latest',
'region' => 'us-east-1',
]);
$adapter = new AwsS3V3Adapter($s3Client, 'bucket-name');
$filesystem = new Filesystem($adapter);
$filesystem->write('path/to/weasyprint-rocks.pdf', $output);To learn more about how to use Flysystem with AWS S3, see the official documentation.
Laravel Storage
With Laravel, use the Storage facade:
use Illuminate\Support\Facades\Storage;
Storage::put('path/to/weasyprint-rocks.pdf', $output);To learn more about how to use Laravel’s File Storage features, see the official documentation.
getData()
If you don't want to take advantage of the Stringable interface, then you can simply get the data directly:
$pdfData = $output->getData();Skipping build()
The factory also exposes stream(), download(), inline(), and getData() directly, so you can skip the explicit build() call when you don't need to interact with the Output object:
$factory = new WeasyPrintFactory();
return $factory
->prepareSource('<p>WeasyPrint rocks!</p>')
->download('weasyprint-rocks.pdf');These methods have the same signatures as their Output counterparts - they call build() internally and forward the result.