A Laravel wrapper for WeasyPrint, a powerful tool for generating PDFs. This package provides an easy-to-use interface for generating, saving, downloading, and displaying PDFs in Laravel applications.
WeasyPrint is a Python-based library that converts HTML and CSS documents into PDFs. It supports modern web standards, making it ideal for generating high-quality PDFs from complex HTML and CSS.
- High-Quality Output: Supports modern CSS features like Flexbox, Grid, and more.
- Cross-Platform: Works on Linux, macOS, and Windows (with some setup).
- Open Source: Free to use and actively maintained.
This Laravel package provides seamless integration with WeasyPrint, enabling easy PDF generation with various options:
- 📄Generate PDFs from Blade views, HTML strings, files, or URLs.
- 💾 Save PDFs to disk or display them directly to the browser.
- 📥 Download PDFs with custom filenames.
- ⚙ Set custom options for WeasyPrint (e.g., metadata, attachments).
- ⏳ Timeout configuration for long-running processes.
- 🔧 Disable/Enable the package dynamically via configuration.
With these features, generating and managing PDFs in your Laravel project becomes effortless! 🚀
Before using this package, ensure the following dependencies are installed:
- Python: WeasyPrint requires Python 3 or higher to work correctly.
- WeasyPrint: Install WeasyPrint using pip or a package manager.
composer require hkwise/laravel-weasyprintphp artisan vendor:publish --provider="HKWise\WeasyPrint\WeasyPrintServiceProvider"The easiest way to install WeasyPrint on Linux is to use the package manager of your distribution.
Install WeasyPrint using apt:
sudo apt install weasyprintInstall WeasyPrint using dnf:
sudo dnf install weasyprintInstall WeasyPrint using pacman:
sudo pacman -S python-weasyprintAlternatively, install via pip:
pip install weasyprintThe easiest way to install WeasyPrint on macOS is to use Homebrew:
brew install weasyprintTo use WeasyPrint on Windows, the easiest way is to use the executable of the latest release.
If you want to use WeasyPrint as a Python library, you’ll have to follow a few extra steps. Please read this carefully.
The first step is to install the latest version of Python.
When Python is installed, you have to install Pango and its dependencies. The easiest way to install these libraries is to use MSYS2. Here are the steps you have to follow:
- Install MSYS2 keeping the default options.
- After installation, in MSYS2’s shell, execute
pacman -S mingw-w64-x86_64-pango. - Close MSYS2’s shell.
You can then launch a Windows command prompt by clicking on the Start menu, typing cmd and clicking the “Command Prompt” icon. Install WeasyPrint in a virtual environment using pip:
python3 -m venv venv
venv\Scripts\activate.bat
python3 -m pip install weasyprint
python3 -m weasyprint --infoFor more detailed installation instructions, visit the WeasyPrint Documentation.
You can modify the package settings in config/weasyprint.php:
-
Enable/Disable Package: Set
'enabled' => false, to disable WeasyPrint without removing it. -
Change Default Save Path: Update
'default_path' => storage_path('app/custom-pdfs'), to save PDFs in a different directory. -
Adjust Timeout: Modify
'timeout' => 120, to increase the maximum execution time.
This configuration ensures flexibility while using WeasyPrint in your Laravel application. 🚀
use HKWise\WeasyPrint\Facades\WeasyPdf;
$data = ['name' => 'John Doe'];
return WeasyPdf::loadView('pdf.invoice', $data)->download('invoice.pdf');use HKWise\WeasyPrint\Facades\WeasyPdf;
WeasyPdf::loadHTML('<h1>Hello, World!</h1>')->save(storage_path('app/example.pdf'));use HKWise\WeasyPrint\Facades\WeasyPdf;
return WeasyPdf::generate('https://weasyprint.org')->inline();use HKWise\WeasyPrint\Facades\WeasyPdf;
return WeasyPdf::loadFile(public_path('myfile.html'))->download('myfile.pdf');5. You can chain multiple methods together. For example, you can save the PDF to disk first, then download it:
use HKWise\WeasyPrint\Facades\WeasyPdf;
return WeasyPdf::loadHtml('<h1>Hello, WeasyPrint!</h1>')
->save(storage_path('app/pdfs/myfile.pdf'))
->download('myfile.pdf');You can pass additional WeasyPrint options using the setOptions method:
use HKWise\WeasyPrint\Facades\WeasyPdf;
return WeasyPdf::loadHTML('<h1>Test</h1>')
->setOptions([
'custom-metadata' => 'metadata.json',
'attachment' => ['note.txt', 'photo.jpg'],
])
->download('document.pdf');This package does not support setting page size or document margins via configuration options. Instead, you must define these settings using the CSS @page at-rule in your HTML or stylesheets.
Example: Set Page Size and Margins
@page {
size: Letter; /* Change from the default size of A4 */
margin: 3cm; /* Set margin on each page */
}There is much more which can be achieved with the @page at-rule, such as page numbers, headers, etc. Read more about the page at-rule.
By default, the Facade is registered as WeasyPdf. If you want to use a different alias (e.g., WPdf), you can manually register it in config/app.php:
'aliases' => [
// Other aliases
'WPdf' => HKWise\WeasyPrint\Facades\WeasyPdf::class,
],After updating the alias, you can use WPdf instead of WeasyPdf:
use WPdf;
return WPdf::loadHTML('<h1>Hello, World!</h1>')->inline();Alternatively, you can register both aliases:
'aliases' => [
// Other aliases
'WeasyPdf' => HKWise\WeasyPrint\Facades\WeasyPdf::class,
'WPdf' => HKWise\WeasyPrint\Facades\WeasyPdf::class,
],Now, you can use either WeasyPdf or WPdf:
use WeasyPdf;
use WPdf;
// Both will work
return WeasyPdf::loadHTML('<h1>Hello, World!</h1>')->inline();
return WPdf::loadHTML('<h1>Hello, World!</h1>')->inline();| Method | Description |
|---|---|
| loadView | Load a Blade view with data. |
| loadHTML | Load raw HTML content. |
| loadFile | Load HTML content from a file. |
| generate | Fetch HTML content from a URL. |
| setOptions | Set additional WeasyPrint options (e.g., --custom-metadata, --attachment). |
| save | Save the PDF to a specified path (or default path). |
| download | Download the PDF with a specified filename. |
| inline | Display the PDF in the browser. |
- Ensure WeasyPrint is installed on your system:
pip install weasyprint- On Linux (Debian/Ubuntu), you can install WeasyPrint using:
sudo apt install weasyprint- Increase the
timeoutvalue inconfig/weasyprint.phpif PDF generation takes longer than expected.
This package may not work properly in local environments on Windows or macOS due to missing system dependencies for WeasyPrint. If you face issues while generating PDFs locally, consider disabling the package in non-production environments.
You can achieve this by modifying the enabled option in the configuration file (config/weasyprint.php):
'enabled' => app()->environment(['production', 'prod']),This ensures the package is only enabled in production environments.
Laravel’s app()->environment() method checks the current application environment based on the APP_ENV value set in your .env file.
For example, if your .env file contains:
APP_ENV=localThen app()->environment(['production', 'prod']) will return false, disabling the package.
However, in a production environment:
APP_ENV=productionapp()->environment(['production', 'prod']) will return true, enabling the package.
Contributions are welcome! Please open an issue or submit a pull request on GitHub.
This package is open-source software licensed under the MIT License.
- Developed by Hassan Khan
- Inspired by weasyprint.