Display CJK character on laravel-invoices

Dec 9, 2021
GitHub - LaravelDaily/laravel-invoices: Laravel package to generate PDF invoices from various customizable parameters
Laravel package to generate PDF invoices from various customizable parameters - GitHub - LaravelDaily/laravel-invoices: Laravel package to generate PDF invoices from various customizable parameters

Laravel invoice is a great package for generating PDF invoices. However, it doesn't came with the support of CJK characters. This post described how to configure CJK character support.

The package internally uses brexit/laravel-dompdf for PDF generation. Which is a wrapper library of dompdf.

Publish config

Since this package uses laravel-dompdf, you could publish its configure file to gain more controls.

Downloading font files

The dompdf package does not support otf fonts, but only support ttf fonts, with no collection support. So it is required to download fonts in different weight separately. The font can be download here.

noto-cjk/Sans/Mono at main · googlefonts/noto-cjk
Noto CJK fonts. Contribute to googlefonts/noto-cjk development by creating an account on GitHub.

After the font was downloaded, you need to convert the otf font to ttf font, which can be achieved using otf2ttf.

otf2ttf
command-line tool for converting OTF to TTF

After converted, place the fonts under storage/fonts and add the following lines into the invoice view template.

@font-face {
	font-family: 'NotoSansCJKhk';
	font-style: normal;
	font-weight: normal;
	src: url({{ storage_path('fonts/NotoSansCJKhk-Regular.ttf') }}) format('truetype')
}

 @font-face {
 	font-family: 'NotoSansCJKhk';
 	font-style: normal;
	font-weight: bold;
	src: url({{ storage_path('fonts/NotoSansCJKhk-Bold.ttf') }}) format('truetype')
 }

When all is completed, you may notice that it is still not working. Let me describe the problem.

What's the problem

The setOptions was called by laravel-invoice, which replaces the whole options of dompdf instead of merging them. Dispite of the comments in laravel-dompdf suggests the method is "Set/Change an option in DomPdf".

Workaround of this problem

laravel-invoices set the option enable_php as true, which are used to generate page number. The easiest solution of this is to remove this line if you are not going to use page numbers.

Great! You've successfully subscribed.
Great! Next, complete checkout for full access.
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.