omaralalwi / Gpdf

Open Source HTML to PDF converter for PHP & Laravel Applications, Support store to s3 & supports Arabic content out-of-the-box and other languages.
MIT License
57 stars 2 forks source link

Displaying Arabic Number issue #2

Closed smahi closed 1 month ago

smahi commented 2 months ago

Screenshot from 2024-08-12 18-56-49 Screenshot from 2024-08-12 18-57-26 Assalam Alekum, Thank you so much for this awesome package, it works properly, but i am not able to display Arabic numbers. How to configure the package so that it display number in Arabic [1,2,3,4,5,6,7,8,9,0] instead of Hindu [١،٢،٣،٤،٥،٦،٧،٨،٩،٠]

Thank you in advance.

omaralalwi commented 2 months ago

we will working on this feature and update ASAP

smahi commented 2 months ago

Screenshot from 2024-08-12 22-03-29 I fixed the issue using the following hack

// added some entries to the config 

    // utf8Glyphs
    GpdfSet::UTF8GLYPHS_MAX_CHARS => 50,
    GpdfSet::UTF8GLYPHS_HINDO => false,
    GpdfSet::UTF8GLYPHS_FORCERTL => false,

// added consts to original class

<?php

namespace Omaralalwi\Gpdf\Enums;

class GpdfDefaultSettings
{
    // ... original consts

    const UTF8GLYPHS_MAX_CHARS = 50;
    const UTF8GLYPHS_HINDO = true;
    const UTF8GLYPHS_FORCERTL = false;
}
// added consts to original class

class GpdfSettingKeys
{
    // ... consts ...

    const UTF8GLYPHS_MAX_CHARS = 'utf8GlyphsMaxChars';
    const UTF8GLYPHS_HINDO = 'utf8GlyphsHindo';
    const UTF8GLYPHS_FORCERTL = 'utf8GlyphsForceRtl';
// changed a method in the original class

class PdfBuilder
{

// ... code ...

    public function formatArabic($htmlContent)
    {
        $config = config('gpdf');  // added this line
        $config = new GpdfConfig($config); // added this line
        $Arabic = new Arabic();
        $p = $Arabic->arIdentify($htmlContent);

        for ($i = count($p)-1; $i >= 0; $i-=2) {

           // passed some params to utf8Glyphs method
            $utf8ar = $Arabic->utf8Glyphs(substr($htmlContent, $p[$i-1], $p[$i] - $p[$i-1]), $config->get('utf8GlyphsMaxChars'), $config->get('utf8GlyphsHindo'), $config->get('utf8GlyphsForceRtl')); 
            $htmlContent   = substr_replace($htmlContent, $utf8ar, $p[$i-1], $p[$i] - $p[$i-1]);
        }

        return $this->convertEntities($htmlContent);
    }

// ..code..

}

In laravel controller i did touch anything


class PDFReportsController extends Controller
{
    public function download(Request $request, int $id)
    {
        $receipt = ReceiptOfBenefit::findOrFail($id);

        $html = view('filament.reports.receipt-of-benefit', ['receipt' => $receipt])->render();
        $pdfContent = GpdfFacade::generate($html);
        return response($pdfContent, 200, ['Content-Type' => 'application/pdf']);
    }
}

Helpfully this will help someone.

smahi commented 2 months ago

By the way the config entry responsible of displaying hindi or arabic number is GpdfSet::UTF8GLYPHS_HINDO=true, by default is set to display Hindi numbers. the signature of the original method from ar-php is

public utf8Glyphs(string $text[, int $max_chars = 50 ][, bool $hindo = true ][, bool $forcertl = false ]) : string
omaralalwi commented 2 months ago

can you make Pull request in package ?

smahi commented 2 months ago

I will try

omaralalwi commented 1 month ago

solved in this pr https://github.com/omaralalwi/Gpdf/pull/3