PHPackages                             hkwise/laravel-weasyprint - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [PDF &amp; Document Generation](/categories/documents)
4. /
5. hkwise/laravel-weasyprint

ActiveLibrary[PDF &amp; Document Generation](/categories/documents)

hkwise/laravel-weasyprint
=========================

A Laravel wrapper for WeasyPrint to generate PDFs

v1.0.0(1y ago)320MITPHPPHP ^8.1

Since Mar 16Pushed 1y ago1 watchersCompare

[ Source](https://github.com/hassankhan-wise/laravel-weasyprint)[ Packagist](https://packagist.org/packages/hkwise/laravel-weasyprint)[ Docs](https://github.com/hassankhan-wise/laravel-weasyprint)[ RSS](/packages/hkwise-laravel-weasyprint/feed)WikiDiscussions main Synced 3w ago

READMEChangelogDependencies (4)Versions (2)Used By (0)

Laravel WeasyPrint
==================

[](#laravel-weasyprint)

[![Latest Stable Version](https://camo.githubusercontent.com/fbcbf59276fbff7f42c0a47e7a46af37d2f19eba31ee524606695ca6727d553b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f686b776973652f6c61726176656c2d77656173797072696e742e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/hkwise/laravel-weasyprint)[![Total Downloads](https://camo.githubusercontent.com/14b18946707c7e47e6aafd9c294a18461410ea5165d9036d0ef4d5339c1b2dd6/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f686b776973652f6c61726176656c2d77656173797072696e742e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/hkwise/laravel-weasyprint)

A Laravel wrapper for [WeasyPrint](https://weasyprint.org "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.

About WeasyPrint
----------------

[](#about-weasyprint)

[WeasyPrint](https://weasyprint.org "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.

#### Key Features of WeasyPrint:

[](#key-features-of-weasyprint)

- **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.

Features
--------

[](#features)

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! 🚀

Prerequisites
-------------

[](#prerequisites)

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.

Installation
------------

[](#installation)

##### 1. Install the package via Composer:

[](#1-install-the-package-via-composer)

```
composer require hkwise/laravel-weasyprint
```

##### 2. Publish the Configuration File (Optional)

[](#2-publish-the-configuration-file-optional)

```
php artisan vendor:publish --provider="HKWise\WeasyPrint\WeasyPrintServiceProvider"
```

##### 3. Install WeasyPrint

[](#3-install-weasyprint)

##### Linux

[](#linux)

The easiest way to install WeasyPrint on Linux is to use the package manager of your distribution.

- ##### Debian/Ubuntu

    [](#debianubuntu)

Install WeasyPrint using `apt`:

```
sudo apt install weasyprint
```

- ##### Fedora

    [](#fedora)

Install WeasyPrint using `dnf`:

```
sudo dnf install weasyprint
```

- ##### Archlinux

    [](#archlinux)

Install WeasyPrint using `pacman`:

```
sudo pacman -S python-weasyprint
```

Alternatively, install via `pip`:

```
pip install weasyprint
```

##### macOS

[](#macos)

The easiest way to install WeasyPrint on macOS is to use [Homebrew](https://brew.sh/ "Homebrew"):

```
brew install weasyprint
```

##### Windows

[](#windows)

To use WeasyPrint on Windows, the easiest way is to use the executable of the [latest release](https://github.com/Kozea/WeasyPrint/releases "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](https://www.msys2.org/#installation "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](https://packaging.python.org/guides/installing-using-pip-and-virtual-environments/ "virtual environment") using [pip](https://pip.pypa.io/ "pip"):

```
python3 -m venv venv
venv\Scripts\activate.bat
python3 -m pip install weasyprint
python3 -m weasyprint --info
```

For more detailed installation instructions, visit the [WeasyPrint Documentation](https://doc.courtbouillon.org/weasyprint/stable/first_steps.html "WeasyPrint Documentation").

Customizing the Configuration
-----------------------------

[](#customizing-the-configuration)

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. 🚀

Usage
-----

[](#usage)

##### 1. Generate PDF from a Blade View and Download

[](#1-generate-pdf-from-a-blade-view-and-download)

```
use HKWise\WeasyPrint\Facades\WeasyPdf;

$data = ['name' => 'John Doe'];
return WeasyPdf::loadView('pdf.invoice', $data)->download('invoice.pdf');
```

##### 2. Generate PDF from HTML and Save to Disk

[](#2-generate-pdf-from-html-and-save-to-disk)

```
use HKWise\WeasyPrint\Facades\WeasyPdf;

WeasyPdf::loadHTML('Hello, World!')->save(storage_path('app/example.pdf'));
```

##### 3. Generate PDF from a URL and Display in the browser

[](#3-generate-pdf-from-a-url-and-display-in-the-browser)

```
use HKWise\WeasyPrint\Facades\WeasyPdf;

return WeasyPdf::generate('https://weasyprint.org')->inline();
```

##### 4. Generate PDF from a File and Download

[](#4-generate-pdf-from-a-file-and-download)

```
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:

[](#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('Hello, WeasyPrint!')
    ->save(storage_path('app/pdfs/myfile.pdf'))
    ->download('myfile.pdf');
```

##### 6. Set Custom Options

[](#6-set-custom-options)

You can pass additional WeasyPrint options using the setOptions method:

```
use HKWise\WeasyPrint\Facades\WeasyPdf;

return WeasyPdf::loadHTML('Test')
    ->setOptions([
        'custom-metadata' => 'metadata.json',
        'attachment' => ['note.txt', 'photo.jpg'],
    ])
    ->download('document.pdf');
```

Adjust Document Dimensions
--------------------------

[](#adjust-document-dimensions)

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](https://developer.mozilla.org/en-US/docs/Web/CSS/@page "page") at-rule.

Customizing the Facade Alias
----------------------------

[](#customizing-the-facade-alias)

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('Hello, World!')->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('Hello, World!')->inline();
return WPdf::loadHTML('Hello, World!')->inline();
```

Methods
-------

[](#methods)

MethodDescriptionloadViewLoad a Blade view with data.loadHTMLLoad raw HTML content.loadFileLoad HTML content from a file.generateFetch HTML content from a URL.setOptionsSet additional WeasyPrint options (e.g., --custom-metadata, --attachment).saveSave the PDF to a specified path (or default path).downloadDownload the PDF with a specified filename.inlineDisplay the PDF in the browser.Troubleshooting
---------------

[](#troubleshooting)

#### 1. WeasyPrint Command Not Found

[](#1-weasyprint-command-not-found)

- Ensure WeasyPrint is installed on your system:

```
pip install weasyprint
```

- On Linux (Debian/Ubuntu), you can install WeasyPrint using:

```
sudo apt install weasyprint
```

#### 2. Timeout Errors

[](#2-timeout-errors)

- Increase the `timeout` value in `config/weasyprint.php` if PDF generation takes longer than expected.

#### 3. Local Environment Issues

[](#3-local-environment-issues)

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.

##### How It Works

[](#how-it-works)

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=local
```

Then` app()->environment(['production', 'prod'])` will return `false`, disabling the package. However, in a production environment:

```
APP_ENV=production
```

`app()->environment(['production', 'prod'])` will return `true`, enabling the package.

Contributing
------------

[](#contributing)

Contributions are welcome! Please open an issue or submit a pull request on [GitHub](https://github.com/hassankhan-wise/laravel-weasyprint "GitHub").

License
-------

[](#license)

This package is open-source software licensed under the [MIT License](http://opensource.org/licenses/MIT "MIT License").

Credits
-------

[](#credits)

- Developed by [Hassan Khan](https://github.com/hassankhan-wise "Hassan Khan")
- Inspired by [weasyprint](https://weasyprint.org "weasyprint").

###  Health Score

28

—

LowBetter than 52% of packages

Maintenance42

Moderate activity, may be stable

Popularity10

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity46

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

468d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/77392509?v=4)[Hassan Khan](/maintainers/hassankhan-wise)[@hassankhan-wise](https://github.com/hassankhan-wise)

---

Top Contributors

[![hassankhan-wise](https://avatars.githubusercontent.com/u/77392509?v=4)](https://github.com/hassankhan-wise "hassankhan-wise (1 commits)")

---

Tags

laravelpdfWeasyPrinthtml-to-pdfpdf-generationcss3 pdfblade to pdf

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/hkwise-laravel-weasyprint/health.svg)

```
[![Health](https://phpackages.com/badges/hkwise-laravel-weasyprint/health.svg)](https://phpackages.com/packages/hkwise-laravel-weasyprint)
```

###  Alternatives

[laravel/horizon

Dashboard and code-driven configuration for Laravel queues.

4.1k91.3M280](/packages/laravel-horizon)[barryvdh/laravel-dompdf

A DOMPDF Wrapper for Laravel

7.3k95.2M358](/packages/barryvdh-laravel-dompdf)[rockett/weasyprint

A feature-rich WeasyPrint wrapper for generating PDFs from HTML and CSS, with support for PDF/A, PDF/UA, attachments, and optional Laravel integration.

29217.1k](/packages/rockett-weasyprint)[spatie/laravel-health

Monitor the health of a Laravel application

87411.3M153](/packages/spatie-laravel-health)[pontedilana/php-weasyprint

PHP library allowing PDF generation from an url or a html page. Wrapper for Kozea/WeasyPrint.

781.2M14](/packages/pontedilana-php-weasyprint)[tarfin-labs/easy-pdf

Makes pdf processing easy.

1719.4k](/packages/tarfin-labs-easy-pdf)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
