1

I use PDF::loadHtml for creating pdf in Laravel 5.3

everything works good but i have utf8 problem i cant solv it

My codes :

 $text="<html>
    <head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    </head>
    <body>
    <h2 style="text-align:center">Birinci gün</h2>
    <table style="width:100%;" border="1">
                                    <tbody><tr>
                                        <td></td>
                                        <td>Həftə</td>
                                        <td style="text-align:center">Dərs</td>
                                        <td style="text-align:center">Vaxt</td>
                                        <td style="text-align:center">Müəllim</td>
                                        <td style="text-align:center">Korpus</td>
                                        <td style="text-align:center">Otaq</td>
                                    </tr>
                                    <tr>
                                        <th rowspan="2">1</th>
                                       <td style="padding-left:5px">Üst</td>
                                        <td><input name="ders[]" value=""></td>
                                        <td style="text-align:center"><input name="vaxt[]" value=""></td>
                                        <td style="text-align:center"><input name="teacher[]" value=""></td>
                                        <td style="text-align:center"><input name="korpus[]" value=""></td>
                                        <td style="text-align:center"><input name="otaq[]" value=""></td>
                                    </tr>      </tbody></table>

    </body>
    </html>";
    $pdf = PDF::loadHTML('<?xml encoding="UTF-8">' .$text, 'UTF-8');
            return $pdf->stream();
3
  • Are you using this package github.com/barryvdh/laravel-dompdf ?? Commented Aug 16, 2017 at 10:54
  • @nikhil_gandhi Yes the package is Dompdf Commented Aug 16, 2017 at 10:57
  • You are stacking double quotes, move the view code to a blade file Commented Aug 16, 2017 at 10:57

2 Answers 2

1

Ok, here is what you need to do.

  1. Move your code to blade file. Never write HTML code in your controller.

  2. In your blade file, set the UTF-8 Metatag:

  3. In your controller's method

    $pdf = PDF::loadView('PDF_NAME.pdf', $data);

    return $pdf->download('invoice.pdf');

Sign up to request clarification or add additional context in comments.

1 Comment

This Issue has been solved , The way to solv the matter you need to change the font family with css adding those following lines : *{ font-family:"DeJaVu Sans Mono",monospace; }
1

PDF::loadHtml not support utf-8 characters very well and pure php libraries is too hard for using specially in laravel and many of them has this problem too. I make laravel composer package for do it very very simple by develop and simplify tcpdf.

Just Install it by this command:

composer require vatttan/apdf

Now its available in where ever you like. For example in your view you can use:

use Illuminate\Support\Facades\Route;
use Vatttan\Apdf\Apdf;
Route::get('/', function () {
    $apdf = new Apdf();
    $apdf->print('<p style="text-align: right">Birinci gün</p>');
});

Finished!

3 Comments

Thank you for sharing your experience , This question already archived and solved from several years. The problem solved by changing fonts.
Yes. But in some languages like Persian show characters vise versa and i post answer for other developers that encounter that.
I got it, Thank you very much again, It is nice to me discovering your solution also.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.