2017-05-16 131 views
1

試圖使用谷歌字體與Laravel DomPDF 0.7。*,但我很難讓他們展示。事實上,我無法獲得任何自定義字體,而不僅僅是Google字體。Laravel DomPDF谷歌字體未加載

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
     <link href="https://fonts.googleapis.com/css?family=Caveat" rel="stylesheet"> 
     <style type="text/css"> 

      .signature { 
       font-family: 'Caveat', cursive !important; 
       font-size: 30px; 
      } 
     </style> 
    </head> 

    <body> 
     <p class="signature">Name</p> 
    </body> 
</html> 

這是什麼顯示,它應該是手寫字體。

enter image description here

dompdf.php配置文件,字體目錄和字體緩存都是可寫的。

"DOMPDF_FONT_DIR" => storage_path('app/tmp/'), 
"DOMPDF_FONT_CACHE" => storage_path('app/tmp/'), 

這裏是控制器代碼:

public function download(Request $request, string $contractKey) 
{ 
    $item = Item::findOrFail($contractKey); 

    $body = @$item->contract; 

    $pdf = PDF::loadView('pdf.contract', [ 
     'body' => $this->cleanContract($body), 
     'docId' => md5($item->id), 
     'item' => $item, 
     'ip' => $ip = @$request->ips()[1] ?: $request->ip(), 
    ]); 

    $pdf->output(); 
    $dom_pdf = $pdf->getDomPDF(); 

    $canvas = $dom_pdf->get_canvas(); 
    $canvas->page_text($canvas->get_width() - 70, $canvas->get_height() - 30, "Page {PAGE_NUM} of {PAGE_COUNT}", null, 10, [0, 0, 0]); 


    return $pdf->stream(); 
} 
+0

請試試這個'字母間距:1px的;''在CSS .signature' @ –

+0

它QuỳnhNguyễn不是正確的字體,字母間距不是問題 – Wasim

+0

你可以嘗試更新到最新版本,看看是否修復它。當前版本是'0.8.0' – Sandeesh

回答

0

字體位置應答

我只能夠得到這個通過讓我的服務器上的字體工作。

您需要告訴DomPDF字體的位置。

public function download(Request $request, string $contractKey) 
{ 
    $item = Item::findOrFail($contractKey); 

    $body = @$item->contract; 

    $pdf = PDF::loadView('pdf.contract', [ 
     'body' => $this->cleanContract($body), 
     'docId' => md5($item->id), 
     'item' => $item, 
     'ip' => $ip = @$request->ips()[1] ?: $request->ip(), 
    ]); 

    $pdf->output(); 
    $dom_pdf = $pdf->getDomPDF(); 

    // set font dir here 
    $dom_pdf->set_option('font_dir', storage_path('fonts/')); 

    $canvas = $dom_pdf->get_canvas(); 
    $canvas->page_text(
     $canvas->get_width() - 70, 
     $canvas->get_height() - 30, 
     "Page {PAGE_NUM} of {PAGE_COUNT}", null, 10, [0, 0, 0]);  
    return $pdf->stream(); 
} 

下載字體到storage/fonts

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
    "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
     <style type="text/css"> 

      .signature { 
       font-family: 'Caveat', cursive; 
       font-size: 30px; 
      } 
     </style> 
    </head> 

    <body> 
     <p class="signature">Name</p> 
    </body> 
</html> 

我用DOMPDF Wrapper for Laravel 5將它與Laravel集成。這很容易。

錯字回答

您需要刪除!important'Caveat', cursive; !important;看到jsfiddle。還有一個額外的分號。

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
    http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
     <link href="https://fonts.googleapis.com/css?family=Caveat" rel="stylesheet"> 
     <style type="text/css"> 

      .signature { 
       font-family: 'Caveat', cursive; 
       font-size: 30px; 
      } 
     </style> 
    </head> 

    <body> 
     <p class="signature">Name</p> 
    </body> 
</html> 
+0

不知道爲什麼會出現語法錯誤,但這也不是問題。同樣的問題。 – Wasim