omaralalwi / gpdf
Custom PDF wrapper supporting Arabic language
Requires
- php: ^8.1|^8.2|^8.3
- aws/aws-sdk-php: ^3.308
- dompdf/dompdf: ^3.0
- khaled.alshamaa/ar-php: ^6.3
Requires (Dev)
- phpunit/phpunit: ^9.3
This package is auto-updated.
Last update: 2025-01-21 17:25:26 UTC
README
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.
Table of Contents
- Requirements
- Installation
- Publish Resources
- 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
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 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.
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
Store Files To S3
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'];
Demo Native PHP App
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.
Supported Fonts
Gpdf supports the following installed fonts (ready to use without any additional configurations):
Support for Arabic
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.
Supported Arabic Fonts
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 .
Examples
Installing Custom Fonts
To install custom 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
Features
- 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.
Thanks
Testing
composer test
or
php run-tests.php
Changelog
See CHANGELOG for recent changes.
Contributors ✨
Thanks to these wonderful people for contributing to this project! 💖
Want to contribute? Check out the contributing guidelines and submit a pull request! 🚀
Security
If you discover any security-related issues, please email omaralwi2010@gmail.com
.
Credits
License
The MIT License (MIT). See LICENSE for more information.