Using Custom Fonts

Hello:

How do you use a custom font in Yii2?

I’ve downloaded a font that displays the text in barcode.

I followed the answer in this question , but I don’t know how to use the installed font.

What I’m trying to do is to show the product code as barcode in the view (DetailView in this case) page of the product.

Regards.

if you mean showing font in html view is just matter of html and css

Lest say your code is saved in $model->barcode

in your view just




echo "<span style='font-family: font-barcode;'>".  $model->barcode . "</span>";



where font-barcode is your font name, the above assume that you imported the font in you css (in case you need more info search for css web font)

After answering your question I give you a suggestion: do not use font for barcode in my personal experience they are highly unreliable.

Much better to use a barcode generator that give you an image and that support multiple barcode format like qr barcode

For yii2 there is this extension that handle this:

I agree that a barcode generator is the best route. I prefer to use a barcode service. There are a number of free and paid services that work great. It’s then as simple as:


Html::img('https://samplebarcodeapi.com/?format=39&data=12345&output=png')

Just Google "barcode rest api"

http://www.barcodes4.me is one of of free ones I have used for several years without any issues.

Ryan

Thank you both!,

I didn’t know that using a font was unreliable (I couldn’t make it work anyway)

Both recommendations seems pretty simple to use, I’ll take a look at them.

Thanks!