chask's avatar
Level 1

how to generate pdf wtih Japanese font with barryvdh/laravel-dompdf

i need to generate PDF with Japanese font but i don't how to change . i hope anyone can here give me some good advice.thanks in advance.

0 likes
7 replies
chask's avatar
Level 1

thanks for your answer but i got confuse is there any step by step explanation because i am new to laravel framework.

quickliketurtle's avatar
Level 16

@chask This isn't related to Laravel but specific to dompdf.

Is the font you want to use available online? If is was say a google font, then you could use css to style your page.

Using @import

<style>
@import url('https://fonts.googleapis.com/css?family=Roboto');
</style>

And style your page with font-family: 'Roboto', sans-serif;

Or alternatively using @font-face

@font-face {
  font-family: 'Firefly';
  font-style: normal;
  font-weight: normal;
  src: url(http://example.com/fonts/firefly.ttf) format('truetype');
}

If your font is not available from a url. Then your other option would be to load it into dompdf from the command line.

You would need to install the load_font.php script from https://github.com/dompdf/utils More info can be found here also: https://github.com/dompdf/dompdf/wiki/UnicodeHowTo#load-a-font-supporting-your-characters-into-dompdf

chask's avatar
Level 1

Thanks for your answer .it works.

thaihoangtran's avatar

After trying many times, I have come to know how it works best.

(1) Download a font that supports Japanese with the format *.ttf Here I example a lightweight font, NotoSansCJKjp-Regular.ttf You can completely use another font as long as it meets your needs (ex: Ms-Mincho, Hiragino-Sans....).

https://github.com/minoryorg/Noto-Sans-CJK-JP/blob/master/fonts/NotoSansCJKjp-Regular.ttf

(2) Copy the font NotoSansCJKjp-Regular.ttf to the public folder of your source code

blog_laravel/public/assets/fonts/NotoSansCJKjp-Regular.ttf

(3) Create an empty fonts folder in the storage folder. I created a .gitignore file inside. This will help you avoid errors like the one below:

fopen(/application/storage/fonts/notosanscjkjp_normal_16fb3c14c9df86758d3d1069651fd79b.ufm): Failed to open stream: No such file or directory

Directory path:

blog_laravel/storage/fonts/.gitignore

Contents inside the file .gitignore:

*
!.gitignore

(4) The content is in the view pdf file, my example is pdf.blade.php. Note that there is no slash (/) before assets in the url

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <style>
        @font-face {
            font-family: 'NotoSansCJKjp';
            src: url('assets/fonts/NotoSansCJKjp-Regular.ttf') format('truetype');
        }
        body {
            font-family: 'NotoSansCJKjp';
        }
    </style>
</head>
<body>
    <p>Your Japanese characters</p>
</body>
</html>

It will work, you don't need to use load_font.php because it's very complicated.

3 likes
_hiro's avatar

Hello, I used font-family named MS Mincho to show in Japanese. like this body { font-family: 'MS Mincho', Osaka, sans-serif; } .header { text-align: center; font-size: 24px; font-weight: bold; margin-bottom: 20px; } .info, .items { width: 100%; border-collapse: collapse; margin-bottom: 20px; } before the time, it needs to download the msmincho.ttf.

Best Regards

Please or to participate in this conversation.