Gpdf
Gpdf: PHP PDF Generator - HTML to PDF Converter for Laravel & PHP with native Arabic/RTL support, with built-in 17 fonts, and S3 storage. Extends DomPDF to fix Arabic limitations and add advanced features for documents, invoices, and reports.
Install / Use
/learn @omaralalwi/GpdfREADME
GPDF: Multilingual & Arabic PDF Generator for PHP/Laravel Applications
<p align="center"> <a href="https://github.com/omaralalwi/Gpdf"> <img src="https://raw.githubusercontent.com/omaralalwi/Gpdf/master/public/images/gpdf-banner-bg.jpg" alt="GPDF: Arabic PDF Generator with S3 Storage | PHP/Laravel Package"> </a> </p>GPDF is a PHP/Laravel package for generating Arabic, RTL, and multilingual PDFs with native support for 17 built-in Arabic fonts, S3 cloud storage, and enterprise-ready features. Built as a DomPDF extension, it solves Arabic rendering issues while adding modern capabilities for documents, invoices, and reports.
Table of Contents
- Requirements
- Installation
- Important Note
- Publish Resources
- Publish Config
- Usage with Laravel
- Usage with Native PHP Apps
- Supported Fonts
- Install Custom Fonts
- Features
- Thanks
- Testing
- Changelog
- Contributors
- Security
- Credits
- License
Requirements
- PHP version 8.1 or higher
- DOM extension
- MBString extension
- php-font-lib
- php-svg-lib
Installation
composer require omaralalwi/gpdf
IMPORTANT NOTE:
if you have old version with some issues, please Do following :-
- delete old config file.
- delete package from composer file.
- re-install package.
- publish config file again.
- then delete config cache.
Publish Resources
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
⚠️ Important Notes:
- This step must be executed in production environments as well — otherwise, the package will not function properly (fonts will be missing).
- If you can not run scripts in production, you can manually copy the
public/vendor/gpdffolder to your production environment.- Ensure the
public/vendor/gpdffolder is writable and publicly accessible by the web server.
Publish Config File
php vendor/omaralalwi/gpdf/scripts/publish_config.php
**Note: You must add generated files to git by git add public/vendor/gpdf/fonts/*.* -f to ensure the fonts added to production | or run publish command again in production.
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.
Usage with Laravel
Using the Gpdf Facade
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']);
}
Using Dependency Injection
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 Generated PDF Files
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']);
}
Storing Generated PDF Files
Store Files To local
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.
Store Files To S3
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
}
Generate Advance With Fixed Header
please see this example if you need to add fixed header to all pages
Demo Laravel App
this Demo Laravel app contain more detailed examples and cases.
Usage with Native PHP Apps
After installing the package and publishing resources, include autoload.php and use the Gpdf class.
Basic Usage
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 Generated PDF Files
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;
Store Files To Local
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
generateWithStore params
| 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
