Gpdf : HTML to PDF Converter for PHP & Laravel
Open Source PHP Package for converting HTML to PDF in PHP & Laravel applications, support store to s3, with out-of-the-box support for Arabic content and other languages. Extends dompdf to add new features and solve issues like Arabic language support.
- PHP version 8.1 or higher
- DOM extension
- MBString extension
- php-font-lib
- php-svg-lib
composer require omaralalwi/gpdf
After installation, publish the config and fonts resources by running the following commands in the root project path:
php vendor/omaralalwi/gpdf/scripts/publish_fonts.php
php vendor/omaralalwi/gpdf/scripts/publish_config.php
Note for Publish Issues: If you encounter any issues while publishing, manually copy the vendor/omaralalwi/gpdf/assets/fonts
folder to public/vendor/gpdf
and ensure the fonts are in public/vendor/gpdf/fonts
. Also, copy vendor/omaralalwi/gpdf/config/gpdf.php
to the /config
folder in the root path.
After installing the package and publishing resources, include autoload.php
and use the Gpdf
class.
require_once __DIR__ . '/vendor/autoload.php';
use Omaralalwi\Gpdf\Gpdf;
use Omaralalwi\Gpdf\GpdfConfig;
$htmlFile = __DIR__ . '/contents/example-1.html';
$content = file_get_contents($htmlFile);
$gpdfConfigFile = require_once 'config/gpdf.php';
$config = new GpdfConfig($gpdfConfigFile);
$gpdf = new Gpdf($config);
$pdfContent = $gpdf->generate($content);
header('Content-Type: application/pdf');
echo $pdfContent;
Note: Customize the settings file as needed.
Stream a PDF directly to the browser using generateWithStream
:
require_once __DIR__ . '/vendor/autoload.php';
use Omaralalwi\Gpdf\Gpdf;
use Omaralalwi\Gpdf\GpdfConfig;
$htmlFile = __DIR__ . '/contents/example-1.html';
$content = file_get_contents($htmlFile);
$gpdfConfigFile = require_once 'config/gpdf.php';
$config = new GpdfConfig($gpdfConfigFile);
$gpdf = new Gpdf($config);
$pdfContent = $gpdf->generateWithStream($content, 'demo-file-name', true);
header('Content-Type: application/pdf');
echo $pdfContent;
Save a PDF files to local storage using generateWithStore
:
Note By default it store files to local driver.
<?php
require_once __DIR__ . '/vendor/autoload.php';
use Omaralalwi\Gpdf\Gpdf;
use Omaralalwi\Gpdf\GpdfConfig;
use Omaralalwi\Gpdf\Enums\{GpdfDefaultSettings, GpdfSettingKeys, GpdfDefaultSupportedFonts};
$htmlFile = __DIR__ . '/contents/example-1.html';
$content = file_get_contents($htmlFile);
$gpdfConfigFile = require_once ('config/gpdf.php');
$config = new GpdfConfig($gpdfConfigFile);
$gpdf = new Gpdf($config);
$sslVerify = false;
$file = $gpdf->generateWithStore($content,null,null, false , $sslVerify); // $sslVerify must be true in production
$fileUrl = $file['ObjectURL'];
return $fileUrl; // get file url as string to store it in db or do any action
Parameter | Type | Description |
---|---|---|
html file |
string | The HTML content to be stored. |
store path or bucket name with s3 |
string | The path where the file will be stored, with S3 store this should bucket name. |
file name |
string | The name of the file. |
with stream |
bool | If you need to stream the file to the browser after storing, set this to true . |
sslVerify |
bool | If with stream is set to true , you should set this to true in production to verify SSL. |
same to store in local, just replace local path with bucket name, and replace generateWithStore
with generateWithStoreToS3
.
<?php
require_once __DIR__ . '/vendor/autoload.php';
use Omaralalwi\Gpdf\Gpdf;
use Omaralalwi\Gpdf\GpdfConfig;
use Omaralalwi\Gpdf\Enums\{GpdfDefaultSettings, GpdfSettingKeys, GpdfDefaultSupportedFonts};
$htmlFile = __DIR__ . '/contents/example-1.html';
$content = file_get_contents($htmlFile);
$gpdfConfigFile = require_once ('config/gpdf.php');
$config = new GpdfConfig($gpdfConfigFile);
$gpdf = new Gpdf($config);
$fileName = "pdf-file-with-store-to-s3";
$sslVerify = true;
$file = $gpdf->generateWithStoreToS3($content,null,$fileName, true, $sslVerify);
$fileUrl = $file['ObjectURL'];
please see this Demo Native PHP app contain more detailed examples and cases like pass dynamic parameters for html file & pass inline configs , .. and another cases.
use Omaralalwi\Gpdf\Facade\Gpdf as GpdfFacade;
public function generatePdf()
{
$html = view('pdf.example-1')->render();
$pdfContent = GpdfFacade::generate($html);
return response($pdfContent, 200, ['Content-Type' => 'application/pdf']);
}
use Omaralalwi\Gpdf\Gpdf;
public function generateSecondWayPdf(Gpdf $gpdf)
{
$html = view('pdf.example-2')->render();
$pdfFile = $gpdf->generate($html);
return response($pdfFile, 200, ['Content-Type' => 'application/pdf']);
}
Stream a PDF directly to the browser using generateWithStream
:
// by default it store files to local driver (path should in public path).
public function generateAndStream()
{
$html = view('pdf.example-2')->render();
$gpdf = app(Gpdf::class);
$gpdf->generateWithStream($html, 'test-streamed-pdf', true);
return response(null, 200, ['Content-Type' => 'application/pdf']);
}
Save a PDF to storage using generateWithStore
:
Note By default it store files to local driver (ensure that: the store path is access able for read and write).
please see generateWithStore params .
public function generateAndStore()
{
$html = view('pdf.example-2')->render();
$gpdf = app(Gpdf::class);
$storePath = storage_path('app/downloads/users/');
$gpdf->generateWithStore($html, $storePath, 'test-stored-pdf-file', true, false); // ssl verify should be true in production .
return $file['ObjectURL']; // return file url as string , to store in db or do any action
}
// may be you will face problems with stream in local, so you can disable ssl verify in local, but should enable it in production.
same to store in local, just replace local path with bucket name, and replace generateWithStore
with generateWithStoreToS3
.
Note Ensure you setup s3 configs in config file.
public function generateAndStoreToS3()
{
$data = $this->getDynamicParams();
$html = view('pdf.example-2',$data)->render();
$gpdf = app(Gpdf::class);
$bucketName = 'your_s3_bucket_name'; // should be read abel and write able .
$file = $gpdf->generateWithStoreToS3($html, $bucketName, 'test-store-pdf-fle', true, true); // with s36 the ssl verify will work in local or production (always secure).
return $file['ObjectURL']; // return file url as string , to store in db or do any action
}
please see this example if you need to add fixed header to all pages
this Demo Laravel app contain more detailed examples and cases.
Gpdf supports the following installed fonts (ready to use without any additional configurations):
Gpdf supports Arabic content out-of-the-box. Simply pass Arabic text within your HTML content. Make sure to use Arabic fonts, which are included by default.
The following built-in fonts support Arabic:
DejaVu Sans Mono
, Tajawal
, Almarai
, Cairo
, Noto Naskh Arabic
, Markazi Text
.
We Recommended to Use font name from Omaralalwi\Gpdf\Enums\GpdfDefaultSupportedFonts
Enum class , like default font name
in config file .
To install a new font, follow these steps:
- Ensure the default fonts are published to
public/vendor/gpdf/fonts
. - Prepare at least one font (Normal) for each family
(Normal, Bold, Italic, BoldItalic)
. - Copy the fonts to any path (not the default fonts path).
- The font family name must be enclosed in double quotes and written in lowercase.
- fonts names must be in kebab case with capitalize.
- Run install font script with the following command:
php vendor/omaralalwi/gpdf/scripts/install_font.php "family name" ./path_to_font/Font-Normal.ttf ./path_to_font/Font-Bold.ttf ./resources/fonts/Tajawal-Italic.ttf ./path_to_font/Font-BoldItalic.ttf
For example, to install the Tajawal
font family:
php vendor/omaralalwi/gpdf/scripts/install_font.php "tajawal" ./resources/fonts/Tajawal-Normal.ttf ./resources/fonts/Tajawal-Bold.ttf ./resources/fonts/Tajawal-Italic.ttf ./resources/fonts/Tajawal-BoldItalic.ttf
- Compatibility with any Standard PHP application, Or framework.
- store pdf files to S3 or local storage directly.
- stream pdf files from urls (local or s3).
- Supports 17 fonts by default, including 7 that support Arabic.
- Allows for the installation of custom fonts.
- Provides easy integration with Laravel applications.
- Offers customizable options for PDF generation.
- Includes detailed documentation.
- provide demo applications for quick start-up Demo Native PHP App , Demo Laravel App .
- Unit Tests Includes unit tests.
composer test
or
php run-tests.php
See CHANGELOG for recent changes.
See CONTRIBUTING for details.
If you discover any security-related issues, please email [email protected]
.
The MIT License (MIT). See LICENSE for more information.
-
Lexi Translate simplify managing translations for multilingual Eloquent models with power of morph relationships and caching .
-
laravel Taxify Laravel Taxify provides a set of helper functions and classes to simplify tax (VAT) calculations within Laravel applications.
-
laravel Deployer Streamlined Deployment for Laravel and Node.js apps, with Zero-Downtime and various environments and branches.
-
laravel Trash Cleaner clean logs and debug files for debugging packages.
-
laravel Time Craft simple trait and helper functions that allow you, Effortlessly manage date and time queries in Laravel apps.
-
Laravel Startkit Laravel Admin Dashboard, Admin Template with Frontend Template, for scalable Laravel projects.