-->

Dompdf package other languages support in laravel

2019-08-20 10:40发布

问题:

i have created my pdf using dompdf package in laravel project. But when i want to render other language (bangla) instead of english

???????????????????????????? 

are shown.

<body style="font-family: 'AdorshoLipi', Arial, sans-serif !important;">

    <div style="border-style: solid; border-width: 2px; margin: 50px;  padding: 10px;">
        <div align="center">
            <h2>
                 Anti-corruption commission hotline-106
            </h2>
        </div>
        <div>
            <p> Reference No : {{ $complain->call_ref_id }}</p>
            {{-- <br> --}}
            <p> Date : {{ $complain->created_at }}</p>
            <p> Name : {{ $complain->name }}</p>
            <p> Address : {{ $complain->address }}</p>
            <p> Phone Number : {{ $complain->phone_number }}</p>
            <p> Against : {{ $complain->against }}</p>
            <p> Sector : {{ $complain->department }}</p>
            <p style="font-family: 'AdorshoLipi', Arial, sans-serif !important;"> Type : {{ $complain->type }}</p>
            <p> Complain Description : {{ $complain->complain_description }}</p>
            <p> Amount : {{ $complain->amount }}</p>
            <p> Status : {{ $complain->status }}</p>


        </div>

        <div style=" margin-left: 450px; width: 100px; ">
            <hr>
            <label>Signature</label>
        </div>

        <div class="panel-primary">

        </div>

    </div>


</body>

回答1:

You can add support with your TrueType font into laravel Dompdf package. Just follow below steps to load TrueType font into dompdf from the command line.

There are basic two methods to use TrueType font family with this package:
1) Using load_font.php script
2) Using @font-face css rules to load a font at run-time

1) Using load_font.php script
load_font.php is a script which run using command line so you need to run the command from the command line. You can easily get this script from Dompdf utils

Once this utility added, run command below command into your command line interface to install the font from your dompdf installation directory.

php load_font.php font_family /pathto/fontfile.ttf

Note: dompdf currently supports only TrueType fonts. So when looking for a font make sure it comes in TrueType format.

2) Using @font-face css rules to load a font at run-time
You just need to add below style script into your page for use TrueType of font.

@font-face {
  font-family: 'font_family';
  font-style: normal;
  font-weight: normal;
  src: url(http://example.com/fonts/fontfile.ttf) format('truetype');
}

Once above font family loaded, you can use it like: <div style="font-family: font_family, sans-serif;">yourtextinotherlanguage</div>

Make sure that your document specify that using UTF-8 like <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>.

More info can be found here too



回答2:

You need to add the following code within the styles tag in the view file

  @font-face {
            font-family: 'iskpota';
            src: url('{{public_path()}}/fonts/your-font.ttf') format('truetype') 
        }
        body{
            font-family: 'your-font', sans-serif; 
        }

Then you need to have the font file included in the storage directory. For that, create a folder named "fonts" right inside the storage folder and include the "your-font.ttf" file within the fonts folder.

PS. These fonts you add in the storage location should also be available in the public/fonts folder.



回答3:

For a better to use different languages like bangla, you can try to used mpf. This one is good for utf8.



回答4:

Try to decode your output with utf8:

utf8_decode($var)