Laravel DomPdf 添加自定义字体
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43636379/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel DomPdf Add Custom Font
提问by maxasela
I am trying to use OLD English font in Dompdf with Laravel.,
I have inserted the font in laravel view. But it seems when generating the pdf It is not working.I tried editing dompdf >vendor >.../dompdf_font_family_cache.dist.php
File.But no luck,
我正在尝试在带有 Laravel 的 Dompdf 中使用 OLD 英文字体。我已经在 Laravel 视图中插入了字体。但似乎在生成 pdf 时它不起作用。我尝试编辑dompdf >vendor >.../dompdf_font_family_cache.dist.php
文件。但没有运气,
Can anyone Suggest a Solution?
任何人都可以提出解决方案吗?
Thanks In Advance.
提前致谢。
回答by Melvin Tehubijuluw
- Make a fonts directory in the storage folder of your Laravel project. ( storage/fonts )
- Put your .otf or .ttf files in the storage/fonts directory.
In your view/html add @font-face rules by using the storage_path method Laravel provides.
@font-face { font-family: 'Your custom font name'; src: url({{ storage_path('fonts\your-custom-font.ttf') }}) format("truetype"); font-weight: 400; // use the matching font-weight here ( 100, 200, 300, 400, etc). font-style: normal; // use the matching font-style here }
Add font-family rules to your css as seen below
body { font-family: "Your custom font name"; }
- 在 Laravel 项目的存储文件夹中创建一个 fonts 目录。(存储/字体)
- 将 .otf 或 .ttf 文件放在 storage/fonts 目录中。
在你的 view/html 中,使用 Laravel 提供的 storage_path 方法添加 @font-face 规则。
@font-face { font-family: 'Your custom font name'; src: url({{ storage_path('fonts\your-custom-font.ttf') }}) format("truetype"); font-weight: 400; // use the matching font-weight here ( 100, 200, 300, 400, etc). font-style: normal; // use the matching font-style here }
将字体系列规则添加到您的 css 中,如下所示
body { font-family: "Your custom font name"; }
Hope this helps.
希望这可以帮助。
回答by Sebastien
I was trying to include custom fonts too. I included the font in the view (using @font-face declaration), but when I tried to generate the pdf, it gave me an ErrorException in AdobeFontMetrics.php. I resolved by creating a fonts folder in the storage folder of your laravel project. So now I have:
我也试图包含自定义字体。我在视图中包含了字体(使用@font-face 声明),但是当我尝试生成 pdf 时,它在 AdobeFontMetrics.php 中给了我一个 ErrorException。我通过在 Laravel 项目的存储文件夹中创建一个 fonts 文件夹来解决。所以现在我有:
> storage
> app
> fonts
> framework
> logs
I hope it will help
我希望它会有所帮助
回答by Shailesh Ladumor
Set font into html page which is load in Dompdf
<html>
<head>
<style>
@font-face {
font-family: 'Helvetica';
font-weight: normal;
font-style: normal;
font-variant: normal;
src: url("font url");
}
body {
font-family: Helvetica, sans-serif;
}
</style>
</head>
<body>
<p>hello world</p>
</body>
</html>