解決larave-dompdf中文字體顯示問題


0、使用MPDF

dompdf個人感覺沒有那么好用,最終的生產環境使用的是MPDF,github上有文檔說明。
如果你堅持使用,下面是解決辦法。可以明確的說,中文亂碼是可以解決的。

1、安裝laravel-dompdf依賴。

Packagist:https://packagist.org/packages/barryvdh/laravel-dompdf

composer require barryvdh/laravel-dompdf

2、配置config/app.php

// add the ServiceProvider to the providers array in config/app.php
// 添加到providers數組下
Barryvdh\DomPDF\ServiceProvider::class,

// Add this to your facades
// 添加到aliases數組下
'PDF' => Barryvdh\DomPDF\Facade::class,

3、創建view

  下載中文字體,這里我使用了msyh.ttf(微軟雅黑)。

      在public目錄下創建fonts目錄並把下載好的字體復制到該目錄下。

      

      創建view:

<!DOCTYPE html>
<html>
    <head>
        <title>測試pdf</title>

        <style>
            @font-face {
                font-family: 'msyh';
                font-style: normal;
                font-weight: normal;
                src: url(http://www.testpdf.com/fonts/msyh.ttf) format('truetype');
            }
            html, body {  height: 100%;  }
            body {  margin: 0;  padding: 0;  width: 100%;
                /*display: table;  */
                font-weight: 100;  font-family: 'msyh';  }
            .container {  text-align: center;
                /*display: table-cell; */
                vertical-align: middle;  }
            .content {  text-align: center;  display: inline-block;  }
            .title {  font-size: 96px;  }
        </style>
    </head>
    <body>
        <div class="container">
            <div class="content">
                <div class="title">xiao{{$name}}</div>
            </div>
        </div>
    </body>
</html>

注意:引入字體,使用@font-face,並通過【font-family: 'msyh';】應用到這個body上。

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

 

 4、創建controller

<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use PDF;

class PdfController extends Controller
{
    /**
     * 測試下載pdf,使用loadView()方法
     * @return mixed
     */
    public function testDownloadPdf()
    {
        $data = array('name'=>'寶bao');
        $pdf = PDF::loadView('invoice', $data);
        return $pdf->download('invoice.pdf');
    }

    /**
     * 測試web頁面展示pdf,使用loadHTML()方法加載
     * @return mixed
     */
    public function testStreamPdf()
    {
        $html = '<html><head><title>Laravel</title><meta http-equiv=\'Content-Type\' content=\'text/html; charset=utf-8\'/><style>body{  font-family: \'msyh\';  }  @font-face {  font-family: \'msyh\';  font-style: normal;  font-weight: normal;  src: url(http://www.testenv.com/fonts/msyh.ttf) format(\'truetype\');  }</style></head><body><div class=\'container\'><div class=\'content\'><p style=\'font-family: msyh, DejaVu Sans,sans-serif;\'>獻給母親的愛</p><div style=\'font-family: msyh, DejaVu Sans,sans-serif;\' class=\'title\'>Laravel 5中文測試</div><div  class=\'title\'>測試三</div></div></div></body></html>';
        $pdf = PDF::loadHTML($html);
         return $pdf->stream();
    }
    
}

 

問題1:

ErrorException in AdobeFontMetrics.php line 45:
fopen(D:\workspace\php\testpdf\storage\fonts/\b0a260823b52b90d6a99ba3a53e67e9f.ufm): failed to open stream: No such file or directory

解決方法:在storage下創建fonts文件夾

 

問題2:

FatalThrowableError in TableCell.php line 31:
Call to a member function get_cellmap() on null

解決方法:

更多解釋請查看:https://github.com/barryvdh/laravel-dompdf/issues/137

// 注釋掉view中的這一行代碼
display: table-cell;

 

參考資料:https://github.com/dompdf/dompdf/wiki/UnicodeHowTo

 

有問題可加QQ群詢問【php/laravel技術交流群】:576269252

--------------------------------------

聲明: 原創文章,未經允許,禁止轉載!

--------------------------------------


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM