A Laravel package for generating PDF files with support for both English and Arabic languages without relying on external libraries.
To install the laravel-arpdf
package, follow these steps:
-
Add the package to your Laravel project using Composer:
composer require baidouabdellah/laravel-arpdf:dev-main
-
Register the Service Provider (if using Laravel < 5.5):
In your
config/app.php
file, add the following line to theproviders
array:Baidouabdellah\LaravelArpdf\ArPDFServiceProvider::class,
-
Publish the configuration file (optional):
You can publish the configuration file to customize the package settings:
php artisan vendor:publish --provider="Baidouabdellah\LaravelArpdf\ArPDFServiceProvider"
To use the package, you can access the PDF generation functionality in your controller:
Demo Controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Baidouabdellah\LaravelArpdf\ArPDF;
class PdfController extends Controller
{
public function generatePdf()
{
$pdf = app(ArPDF::class);
// Example of adding text
$pdf->addText(50, 800, 12, 'Hello World');
$pdf->addArabicText(50, 780, 12, 'مرحبا بالعالم');
// Save or stream the PDF
$pdf->save('output/sample.pdf');
// Or stream directly to the browser
// $pdf->stream('document.pdf');
}
}
Here’s an example of how to generate a simple PDF:
$pdf = app('ArPDF');
$pdf->setTitle('Sample PDF');
$pdf->addPage();
$pdf->writeHTML('<h1>Hello World</h1>');
$pdf->output('sample.pdf');
If you need to customize the Arabic font used in the PDFs, follow these steps:
-
Add the Arabic font files: Place your Arabic font files (e.g., TTF or OTF) in the
resources/fonts
directory of your Laravel project. -
Configure the font in your code: In your PDF generation code, you can specify the font like this:
$pdf = app('ArPDF'); $pdf->setFont('path/to/your/font.ttf'); // Specify the path to your Arabic font
-
Ensure the font supports Arabic characters: Make sure the font you are using supports Arabic characters to display them correctly in the PDF.
-
Example of setting the Arabic font: Here’s an example of how to set an Arabic font in your PDF:
$pdf = app('ArPDF');
$pdf->setTitle('Sample PDF');
$pdf->addPage();
$pdf->setFont('resources/fonts/YourArabicFont.ttf');
$pdf->writeHTML('<h1>مرحبا بالعالم</h1>'); // Example of Arabic text
$pdf->output('sample.pdf');
If you encounter any issues, please open an issue on the GitHub repository.
- Abdellah Baidou
- Phone: +212 661-176711
- Email: baidou.abd@gmail.com
This package is licensed under the MIT License. See the LICENSE file for more information.