I'm using DOMPDF ver. 0.7.0 to make a pdf file from HTML, but I have problem when I need to use custom font (arial) with bold style. the bold font won't work after rendering.
here's the CSS style I'm using now:
#font-face {
font-family: 'font';
font-style: normal;
font-weight: normal;
src: url(dompdf/font/arial.ttf);
}
#font-face {
font-family: 'font';
font-style: normal;
font-weight: bold;
src: url(dompdf/font/arialbd.ttf);
}
body{
font-family: 'font';
}
and here's dompdf - php code:
require_once 'dompdf/autoload.inc.php';
use Dompdf\Dompdf;
$dompdf = new Dompdf();
$dompdf->load_html($html);
$dompdf->setPaper('A4', 'portrait');
$dompdf->render();
$pdf = $dompdf->output();
file_put_contents('pdf/sptjm.pdf', $pdf)
here's the result in HTML:
and here's the result in PDF:
the pdf didn't show the bold type, please help. thanks in advance.
i have done it simply with using different font name:
#font-face {
font-family: 'font';
font-style: normal;
font-weight: normal;
src: url(dompdf/font/arial.ttf);
}
#font-face {
font-family: 'font2';
font-style: normal;
font-weight: bold;
src: url(dompdf/font/arialbd.ttf);
}
body{
font-family: 'font';
}
so when i need the bold font type, just simply call the other font name 'cuz basically it is different font source.
Related
Seems this question has been posted a few times, but posting here again because I couldn't find anything useful for my issue :(
I am using wkhtmltopdf to generate pdf files from HTML views. I have added custom fonts in the site. The rendered HTML view looks great. But the CSS styles aren't applied in the PDF properly.
My CSS
#font-face {
font-family: 'MyFont';
src: url('fonts/MyFont-Light.otf') format('opentype'),
url('fonts/MyFont-Light.ttf') format('truetype');
font-weight: 300;
font-style: normal;
}
#font-face {
font-family: 'MyFont';
src: url('fonts/MyFont-Regular.otf') format('opentype'),
url('fonts/MyFont-Regular.ttf') format('truetype');
font-weight: 400;
font-style: normal;
}
body {
font-family: 'MyFont', Helvetica, Arial, sans-serif;
font-weight: 300;
}
h1 {
font-weight: 400;
}
HTML View
PDF View
Can someone please help me resolve this issue? Thanks in advance.
In my pdf template I use the following style:
#font-face {
font-family: 'Open Sans', sans-serif;
font-style: normal;
font-weight: normal;
src: url('https://fonts.googleapis.com/css2?family=Open+Sans:wght#300;400;600;700;800&display=swap');
}
table thead tr td{
font-family: 'Open Sans', sans-serif;
}
table tbody tr td{
font-family: 'Open Sans', sans-serif;
}
This works fine. I see the correct font in my pdf. But when I want to change the font into source sans Pro font, it is not working. The font is set to Arial not to source sans pro:
#font-face {
font-family: 'Source Sans Pro', sans-serif;
font-style: normal;
font-weight: normal;
src: url('https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght#0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&display=swap');
}
table thead tr td{
font-family: 'Source Sans Pro', sans-serif;
}
table tbody tr td{
font-family: 'Source Sans Pro', sans-serif;
}
What could be a reason for this?
The other problem, all content that was loaded with bold is now set to normal, even if I remove the font-weight.
one of the solution is to include the css from the link into a local file. Then provide the right path to load the external css font file from your app.
$dompdf = new DOMPDF();
$dompdf->setBasePath(realpath(APPLICATION_PATH . '/path/to/googleapis/fonts.css'));
$dompdf->loadHtml($html);
$dompdf->render();
Also check the css compatibility of different css tags from https://github.com/dompdf/dompdf/wiki/CSSCompatibility
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,
Can anyone Suggest a Solution?
Thanks In Advance.
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";
}
Hope this helps.
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:
> storage
> app
> fonts
> framework
> logs
I hope it will help
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>
#font-face {
font-family: 'Journal';
src: url('yourwebsite.com/journal.ttf")}}') format('truetype');
}
.typed {
font-family: 'Journal';
}
<p class='typed'>Signature</p>
Make sure to put your font name like this font: Journal; It will work.
Plus add fonts folder under your storage folder.
Place the font in the storage/fonts folder.
In my example OpenSans is used, you can download here https://files.fm/f/sr3wah265 . Unzip and put it in storage/fonts/
Open sans can also be downloaded from GoogleFonts.
then add the following style tag to your html.
<style>
#font-face {
font-family: 'Open Sans';
src: url({{ storage_path("fonts/static/OpenSans/OpenSans-Bold.ttf") }}) format("truetype");
font-weight: 700;
font-style: normal;
}
#font-face {
font-family: 'Open Sans';
src: url({{ storage_path("fonts/static/OpenSans/OpenSans-BoldItalic.ttf") }}) format("truetype");
font-weight: 700;
font-style: italic;
}
#font-face {
font-family: 'Open Sans';
src: url({{ storage_path("fonts/static/OpenSans/OpenSans-ExtraBold.ttf") }}) format("truetype");
font-weight: 800;
font-style: normal;
}
#font-face {
font-family: 'Open Sans';
src: url({{ storage_path("fonts/static/OpenSans/OpenSans-ExtraBoldItalic.ttf") }}) format("truetype");
font-weight: 800;
font-style: italic;
}
#font-face {
font-family: 'Open Sans';
src: url({{ storage_path("fonts/static/OpenSans/OpenSans-Light.ttf") }}) format("truetype");
font-weight: 300;
font-style: normal;
}
#font-face {
font-family: 'Open Sans';
src: url({{ storage_path("fonts/static/OpenSans/OpenSans-LightItalic.ttf") }}) format("truetype");
font-weight: 300;
font-style: italic;
}
#font-face {
font-family: 'Open Sans';
src: url({{ storage_path("fonts/static/OpenSans/OpenSans-Medium.ttf") }}) format("truetype");
font-weight: 500;
font-style: normal;
}
#font-face {
font-family: 'Open Sans';
src: url({{ storage_path("fonts/static/OpenSans/OpenSans-MediumItalic.ttf") }}) format("truetype");
font-weight: 500;
font-style: italic;
}
#font-face {
font-family: 'Open Sans';
src: url({{ storage_path("fonts/static/OpenSans/OpenSans-Regular.ttf") }}) format("truetype");
font-weight: 400;
font-style: normal;
}
#font-face {
font-family: 'Open Sans';
src: url({{ storage_path("fonts/static/OpenSans/OpenSans-SemiBold.ttf") }}) format("truetype");
font-weight: 600;
font-style: normal;
}
#font-face {
font-family: 'Open Sans';
src: url({{ storage_path("fonts/static/OpenSans/OpenSans-SemiBoldItalic.ttf") }}) format("truetype");
font-weight: 600;
font-style: italic;
}
#font-face {
font-family: 'Open Sans';
src: url({{ storage_path("fonts/static/OpenSans/OpenSans-Italic.ttf") }}) format("truetype");
font-weight: 400;
font-style: italic;
}
body {
font-family: 'Open Sans', sans-serif;
}
</style>
I have already asked and played around with this question on Laravel discussions here (https://laracasts.com/discuss/channels/code-review/linking-to-custom-font-file-from-a-stylesheet-inside-a-blade-template). moving the file back to the stylesheet and removing the asset in the link the did not. I am trying to import custom fonts into my website, but because of Laravel's router, I can't directly link to the font files in my CSS. how can I get my custom fonts to display in the browser? So far, this dosent work:
#font-face { font-family: NexaBold; src: url('{!! asset('build/fonts/NexaBold.otf') !!}'); }
#font-face { font-family: NexaLight; src: url('{!! asset('build/fonts/NexaLight.otf') !!}'); }
#font-face { font-family: OpenSans; src: url('{!! asset('build/fontsOpenSans-Regular.ttf') !!}'); }
I tried replacing asset with public_path but that didn't work either. How do I get my fonts to display using the Laravel blade engine?
You can put your fonts in this directory /storage/app/public/fonts, so they can be accessible from the frontend:
This is how you can import them:
#font-face {
font-family:'NexaBold';
src: url('/fonts/NexaBold.otf') format('otf');
font-style: normal;
font-weight: normal;
}
#font-face {
font-family:'NexaLight';
src: url('/fonts/NexaLight.otf') format('otf');
font-style: normal;
font-weight: normal;
}
#font-face {
font-family:'OpenSans';
src: url('/fonts/OpenSans-Regular.ttf') format('ttf');
font-style: normal;
font-weight: normal;
}
Than to style some elements:
body{
font-family: NexaLight,Arial,Helvetica,sans-serif;
}
h1, h2{
font-family: NexaBold,Arial,Helvetica,sans-serif;
}
I want to added custom font in my app, but it does not work.
Here it is path when font located
#font-face { font-family: PF Din Text Cond Pro;
src: url('/fonts/JtNLnKEa.ttf'); }
font located in system/fonts/JtNLnKEa
UPD
#font-face {
font-family: PF Din Text Cond Pro;
src: url('http://contrast-energo.ru/fonts/JtNLnKEa.ttf');
}
body,html {
font-family: PF Din Text Cond Pro;
height: 100%;
min-height: 100%;
margin-top: 0px;
}
How fix?
Sorry for my bad English
Add your font a directory accessible to your CodeIgniter.
#font-face {
font-family: 'YourFontName'; /*a name to be used later*/
src: url('http://domain.com/fonts/JtNLnKEa.ttf'); /*URL to font*/
}
And use your font name like:
.classname {
font-family: 'YourFontName';
}
EDIT:
body,html {
font-family: "PF Din Text Cond Pro"; /* Add double quotes around your custom font name */
height: 100%;
min-height: 100%;
margin-top: 0px;
}
#font-face {
font-family:'Lucida Grande';
font-style: normal;
font-weight: 100;
src: url(../fonts/6216.ttf) format('ttf');
}
#font-face {
font-family:'Myriad Set Pro';
font-style: normal;
font-weight: 100;
src: url(../fonts/myriad.woff) format('woff');
}
#font-face {
font-family:'Helvetica Neue';
font-style: normal;
font-weight: 100;
src: url(../fonts/helvetica-neue-medium.ttf) format('ttf');
}
#font-face {
font-family:'Roboto';
font-style: normal;
font-weight: 100;
src: url(../fonts/helvetica-neue-medium.ttf) format('ttf');
}
body
{
font-family:"Myriad Set Pro","Lucida Grande","Helvetica Neue","Helvetica","Arial","Verdana","sans-serif";
overflow-x:hidden !important;
}
in root folder assets/css/fonts
your appliation structuer should be like this
- application
- system
- assets
- css
- js
- fonts
etc..
then your access url should be like http://contrast-energo.ru/assets/fonts/JtNLnKEa.ttf
Your problem isn't related to CodeIgniter. You have a problem in the CSS/HTML.
You need to use the following CSS code to add properly your fonts to a HTML document:
#font-face {
font-family: 'PF Din Text Cond Pro';
src: url('fonts/PF-Din-Text-Cond-Pro.eot');
src: url('fonts/PF-Din-Text-Cond-Pro.woff') format('woff'),
url('fonts/PF-Din-Text-Cond-Pro.ttf') format('truetype'),
url('fonts/PF-Din-Text-Cond-Pro.svg') format('svg');
font-weight: normal;
font-style: normal;
}
The custom font will be displayed on all the browsers.
In case you want to assign the font to the body element, you can use the following:
body {font-family:'PF Din Text Cond Pro', Arial, sans-serif;}
You need to add the font name between single quotes or double quotes, because it's a custom font.
In case you have only a ".otf" or ".ttf" font file, then you need to convert this file to the other formats.
Online Font Converter is the best free font converter out there.
Try this:
Font name has case sensitive. Use font file name with correct case sensitive.
My font file name is Prata-Regular_0.ttf
So i type this:
#font-face {
font-family: 'Prata-Regular_0';
src: url("http:// YOUR DOMAIN .com/assets/fonts/Prata-Regular_0.ttf");
}
Not like this:
#font-face {
font-family: 'Prata-Regular_0';
src: url("http:// YOUR DOMAIN .com/assets/fonts/prata-regular_0.ttf");
}
It works guys...