Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Malformed UTF-8 characters, possibly incorrectly encoded #243

Open
mustafaakalin opened this issue Jan 20, 2025 · 0 comments
Open

Malformed UTF-8 characters, possibly incorrectly encoded #243

mustafaakalin opened this issue Jan 20, 2025 · 0 comments

Comments

@mustafaakalin
Copy link

Describe the bug
A clear and concise description of what the bug is.
lang tr

Malformed UTF-8 characters, possibly incorrectly encoded

To Reproduce
Steps to reproduce the behavior:
filamentphp create order resource and add this function on ViewOrder.php /app/Filament/Resources/OrderResource/Pages/ViewOrder.php

use LaravelDaily\Invoices\Invoice;
use LaravelDaily\Invoices\Classes\Buyer;
use LaravelDaily\Invoices\Classes\InvoiceItem;
use App\Models\Order;


    public function generateInvoice(Order $order)
    {
        // Configure PDF options
        $defaultConfig = (new \Dompdf\Dompdf())->getOptions();
        $defaultConfig->set('defaultFont', 'DejaVu Sans');
        $defaultConfig->set('isRemoteEnabled', true);
        $defaultConfig->set('isHtml5ParserEnabled', true);

        // UTF-8 encoding for customer data
        $customerName = mb_convert_encoding($order->user->name, 'UTF-8', 'auto');


        // Site sahibi bilgileri config/invoices.php'den alınır
        $customer = new Buyer([
            'name' => htmlspecialchars($order->user->name, ENT_QUOTES, 'UTF-8'),
            'custom_fields' => [
                'email' => $order->user->email,
                'phone' => $order->user->phone ?? '',
                'address' => htmlspecialchars($order->address->address ?? '', ENT_QUOTES, 'UTF-8'),
            ],
        ]);

        $items = [];
        foreach ($order->items as $item) {
            $items[] = (new InvoiceItem())
                ->title(mb_convert_encoding($item->product->name, 'UTF-8', 'auto'))
                ->pricePerUnit($item->price)
                ->quantity($item->quantity);
        }

        $invoice = Invoice::make()
            ->buyer($customer)
            ->sequence($order->id) // Fatura numarası olarak sipariş ID'si
            ->serialNumberFormat('{SEQUENCE}')
            ->currencySymbol('')
            ->currencyCode('TRY')
            ->dateFormat('d/m/Y')
            ->payUntilDays(7) // Ödeme vadesi
            ->filename("invoice_{$order->id}") // PDF dosya adı
            ->addItems($items);

        // Varsa indirimler
        if ($order->discount > 0) {
            $invoice->totalDiscount($order->discount);
        }

        // Kargo ücreti
        // if ($order->shipping_cost > 0) {
        //     $invoice->shipping($order->shipping_cost);
        // }

        // return $invoice->stream(); // PDF olarak görüntüle
        // Alternatif olarak:
        // return $invoice->download(); // PDF'i indir
        // $invoice->save('storage/invoices'); // Depolama alanına kaydet


        // UTF-8 headers for PDF response
        return $invoice->stream()
            // ->header('Content-Type', 'application/pdf; charset=UTF-8');

            ->header('Content-Type', 'application/pdf')
            ->header('Content-Disposition', 'inline; filename="fatura.pdf"')
            ->header('Accept-Charset', 'UTF-8');

    }
```,
```php

    protected function getHeaderActions(): array
    {
        return [
            Actions\EditAction::make(),
            Actions\Action::make('generate_invoice')
                ->label('Fatura Oluştur')
                ->action(function () {
                    return $this->generateInvoice($this->record);
                })
                ->requiresConfirmation()
                ->color('success')
                ->icon('heroicon-o-document-text'),
        ];
    }

,

config/invoices.php

<?php

return [
    'date' => [

        /*
         * Carbon date format
         */
        'format' => 'Y-m-d',

        /*
         * Due date for payment since invoice's date.
         */
        'pay_until_days' => 7,
    ],

    'serial_number' => [
        'series'   => 'AA',
        'sequence' => 1,

        /*
         * Sequence will be padded accordingly, for ex. 00001
         */
        'sequence_padding' => 5,
        'delimiter'        => '.',

        /*
         * Supported tags {SERIES}, {DELIMITER}, {SEQUENCE}
         * Example: AA.00001
         */
        'format' => '{SERIES}{DELIMITER}{SEQUENCE}',
    ],

    'currency' => [
        'code' => 'TRY',

        /*
         * Usually cents
         * Used when spelling out the amount and if your currency has decimals.
         *
         * Example: Amount in words: Eight hundred fifty thousand sixty-eight EUR and fifteen ct.
         */
        'fraction' => 'krs.',
        'symbol'   => '',

        /*
         * Example: 19.00
         */
        'decimals' => 2,

        /*
         * Example: 1.99
         */
        'decimal_point' => ',',

        /*
         * By default empty.
         * Example: 1,999.00
         */
        'thousands_separator' => '.',

        /*
         * Supported tags {VALUE}, {SYMBOL}, {CODE}
         * Example: 1.99 €
         */
        'format' => '{VALUE} {SYMBOL}',
    ],

    'paper' => [
        // A4 = 210 mm x 297 mm = 595 pt x 842 pt
        'size'        => 'a4',
        'orientation' => 'portrait',
    ],

    'disk' => 'local',

    'seller' => [
        /*
         * Class used in templates via $invoice->seller
         *
         * Must implement LaravelDaily\Invoices\Contracts\PartyContract
         *      or extend LaravelDaily\Invoices\Classes\Party
         */
        'class' => \LaravelDaily\Invoices\Classes\Seller::class,

        /*
         * Default attributes for Seller::class
         */
        'attributes' => [
            'name'          => env('APP_NAME', 'Şirket Adı'),
            'address'       => '89982 Pfeffer Falls Damianstad, CO 66972-8160',
            'code'          => '41-1985581',
            'vat'           => '123456789',
            'phone'         => '760-355-3930',
            'custom_fields' => [
                /*
                 * Custom attributes for Seller::class
                 *
                 * Used to display additional info on Seller section in invoice
                 * attribute => value
                 */
                'SWIFT' => 'BANK101',
                'web' => env('APP_URL', 'Şirket Adı'),
                'email' => '[email protected]',
                'IBAN' => 'TR00 0000 0000 0000'
            ],
        ],
    ],

    'dompdf_options' => [
        'enable_php' => true,
        /**
         * Do not write log.html or make it optional
         *  @see https://github.com/dompdf/dompdf/issues/2810
         */
        'logOutputFile' => '/dev/null',

        
        'defaultFont' => 'DejaVu Sans',
        'isHtml5ParserEnabled' => true,
        'isRemoteEnabled' => true,
        'defaultMediaType' => 'screen',
        'isFontSubsettingEnabled' => true,
        'defaultEncoding' => 'UTF-8',
        'chroot' => public_path(),
    ],
];

i tried this functions:

  • mb_convert_encoding
  • htmlspecialchars
  • mb_strtoupper(mb_substr($variable, 0, 1)

database charset and collation:

MariaDB [xxxxxxxxxxx]> SELECT @@character_set_database, @@collation_database;
+--------------------------+----------------------+
| @@character_set_database | @@collation_database |
+--------------------------+----------------------+
| utf8mb4                  | utf8mb4_unicode_ci   |
+--------------------------+----------------------+

i tried this commands:

  • artisan optimize:clear
  • artisan view:clear
  • artisan config:clear
  • artisan cache:clear
  1. Go to '...'
  2. Click on '....'
  3. Scroll down to '....'
  4. See error

Expected behavior
A clear and concise description of what you expected to happen.

Screenshots
If applicable, add screenshots to help explain your problem.

Image

Desktop (please complete the following information):

  • OS: [e.g. iOS]
  • Browser [e.g. chrome, safari]
  • Version [e.g. 22]

Smartphone (please complete the following information):

  • Device: [e.g. iPhone6]
  • OS: [e.g. iOS8.1]
  • Browser [e.g. stock browser, safari]
  • Version [e.g. 22]

Additional context
Add any other context about the problem here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant