vendredi 26 avril 2019

How to fix unicode utf-8 when export content to PDF on laravel 7 with dompdf?

I created a function to export download PDF files with Laravel dompdf. I have a bug with UTF-8 Unicode for the Japanese language. The pdf file displays incorrect the Japanese language. When I copy the text in the PDF to the word file it can display correctly.

I created a function in controller and view to generate the pdf and download as below.

The controller:

public function exportPdf(Request $request)
    {
        $project_id = $request->project_id;

        // Fetch all element from database
        $data = DB::table('element_designs')
            ->join('elements', function($join){
                $join->on('element_designs.id', '=', 'elements.element_design_id')
                    ->where('elements.deleted_at', null);
            })

            ->leftJoin('tag_mappings', function($join){
                $join->on('elements.id' , '=', 'tag_mappings.element_id')
                    ->where('tag_mappings.deleted_at', null);
            })
            ->leftJoin('rfid_tags', function($join){
                $join->on('tag_mappings.tag_id', '=', 'rfid_tags.id')
                    ->where('rfid_tags.disable_flg', false);
            })
            ->leftJoin('districts', function($join){
                $join->on('element_designs.district_id', '=', 'districts.id')
                    ->where('districts.disable_flg', false);
            })
            ->leftJoin('statuses', function($join){
                $join->on('elements.status_id', '=', 'statuses.id')
                    ->where('statuses.disable_flg', false);
            })
            ->leftJoin('locations', function($join){
                $join->on('elements.location_id', '=', 'locations.id')
                    ->where('locations.disable_flg', false);
            })
            ->leftJoin('areas', function($join){
                $join->on('elements.area_id', '=', 'areas.id')
                    ->where('areas.disable_flg', false);
            })
            ->leftJoin('delivery_date_mappings', function($join){
                $join->on('elements.id', '=', 'delivery_date_mappings.element_id')
                    ->where('delivery_date_mappings.deleted_at', null);
            })
            ->leftJoin('delivery_dates', function($join){
                $join->on('delivery_date_mappings.delivery_date_id', '=', 'delivery_dates.id')
                    ->where('delivery_dates.disable_flg', false);
            })
            ->where('element_designs.disable_flg', false)
            ->where('element_designs.project_id', $project_id)
            ->orderBy('element_designs.id')
            ->select('element_designs.id as element_design_id', 'element_designs.doc_no', 'elements.id as element_id', 'element_designs.project_id as project_id',
                'tag_mappings.created_at as create_time', 'tag_mappings.id as tag_mappings_id', 'element_designs.element_name', 'elements.element_seq_id',
                'districts.district_name', 'element_designs.material_name', 'element_designs.element_size', 'element_designs.element_length', 'element_designs.single_weight', 'element_designs.remarks',
                'element_designs.total_weight', 'rfid_tags.device_id', 'statuses.status_name', 'locations.location_name', 'areas.area_name',
                'elements.latitude', 'elements.longitude', 'delivery_date_mappings.delivery_date_id', 'delivery_dates.delivery_date',
                'elements.updated_at', 'statuses.status_name', 'statuses.id as statuses_id')
            ->paginate(100);

        // Send data to the view using loadView function of PDF facade
        $view = view('element.pdf', ['items' => $data]);
        $html = mb_convert_encoding($view, 'HTML-ENTITIES', 'UTF-8');
        $html_decode = html_entity_decode($html);
        $pdf = \PDF::loadHTML($view)
            ->setPaper('a4', 'landscape')
            ->setWarnings(false)
            ->setOptions(['isFontSubsettingEnabled' => true]);

        // Store pdf file in the server
        return $pdf->download('element_list.pdf');
        //return view('element.pdf', ['items' => $data]);
    }

The view:

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>Export element list to PDF</title>
    <style type="text/css">
        /*@import url(https://fonts.googleapis.com/css?family=Kosugi);*/
        /*Nunito font*/
        /*@import url(//fonts.googleapis.com/earlyaccess/notosansjapanese.css);
        @font-face {
            !*font-family: "Noto Sans", "Noto Sans CJK JP", sans-serif;*!
            font-family: 'Noto Sans Japanese', sans-serif;
            src: url() format("otf");
            font-weight: normal;
            font-style: normal;
            font-size: 8px;
        }*/
        /*Nunito font
        * @import url(https://fonts.googleapis.com/css?family=Nunito);
        */
        /* vietnamese */
        @font-face {
            font-family: 'Nunito';
            font-style: normal;
            font-weight: 400;
            src: local('Nunito Regular'), local('Nunito-Regular'), url() format('woff2');
            unicode-range: U+0102-0103, U+0110-0111, U+1EA0-1EF9, U+20AB;
        }
        /* latin-ext */
        @font-face {
            font-family: 'Nunito';
            font-style: normal;
            font-weight: 400;
            src: local('Nunito Regular'), local('Nunito-Regular'), url() format('woff2');
            unicode-range: U+0100-024F, U+0259, U+1E00-1EFF, U+2020, U+20A0-20AB, U+20AD-20CF, U+2113, U+2C60-2C7F, U+A720-A7FF;
        }
        /* latin */
        @font-face {
            font-family: 'Nunito';
            font-style: normal;
            font-weight: 400;
            src: local('Nunito Regular'), local('Nunito-Regular'), url() format('woff2');
            unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
        }
        /*End nunito fonts*/

        *{
            font-family: 'Nunito', sans-serif;

        }
        body {
            font-family: 'Nunito', sans-serif;
            font-size: 8px;
        }
        .table {
            border-collapse: collapse;
            width: 100%;
        }

        .table td, .table th {
            border: 1px solid #000000;
            padding: 8px;
        }

        .table tr:nth-child(even){}/*background-color: #f2f2f2;*/

        .table tr:hover {}/*background-color: #ddd;*/

        .table th {
            padding-top: 12px;
            padding-bottom: 12px;
            text-align: left;
            background-color: #fff6a1;
            font-weight: normal;
        }
    </style>
</head>
<body>
    <table class="table table-striped">
        <thead>
            <tr>
                <th>ID</th>
                <th>図面No</th>
                <th>製品名</th>
                <th>工区</th>
                <th>鋼材</th>
                <th>部材</th>
                <th>長さ</th>
                <th>単重</th>
                <th>鉄板その他</th>
                <th>重量</th>
                <th>タグID</th>
                <th>ステータス</th>
                <th>ロケーション</th>
                <th>エリア</th>
            </tr>
        </thead>

        <tbody>
        @foreach ($items as $item)
            <tr>
                <td></td>ID
                <td></td>図面No
                <td></td>製品名
                <td></td>
                <td></td>鋼材
                <td></td>部材
                <td></td>長さ
                <td></td>単重
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
            </tr>
        @endforeach
        </tbody>
</table>

</body>
</html>




I am using Laravel 5.7 and PHP 7.

Please see the code above and help me. Thank you so much, BienHV



from Newest questions tagged laravel-5 - Stack Overflow http://bit.ly/2GzOwP3
via IFTTT

Aucun commentaire:

Enregistrer un commentaire