Display CJK characters 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 come with the support of CJK characters. This post described how to configure CJK character support.

The package is internally usedbrexit/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 weights separately. The font can be downloaded 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. Despite 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 numbers. The easiest solution to this is to remove this line if you are not going to use page numbers.