skip to Main Content

Hello i want to convert html to pdf using dompdf, my HTML has Chinese text. In HTML view the Chinese text show very well, but when I convert to pdf the Chinese text only show square.

This is my code

$html = $this->load->view('view_view', $data, TRUE);
//echo $html;

require("assets/dompdf1/autoload.inc.php"); 
define("DOMPDF_UNICODE_ENABLED", true);
$options = new Options();
$options->set('defaultFont', 'DejaVu Sans');
$dompdf = new Dompdf($options);
$html = mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8');
$dompdf->loadHtml($html);


// (Optional) Setup the paper size and orientation
$dompdf->setPaper('A4', 'landscape');

// Render the HTML as PDF
$dompdf->render();

        // Output the generated PDF to Browser

$dompdf->stream("View.pdf", array("Attachment" => false));


exit(0);

The error

I already set the font face in the HTML

   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<style>
    @page {
        margin-top: 0.3cm;
        margin-bottom: 0cm;
    }
    @font-face {
        font-family: 'NotoSansCJK';
        src: url('assets/dompdf1/lib/fonts/NotoSansCJKtc-Regular.ttf') format('truetype');
    }
     body {
       font-family: 'NotoSansCJK', DejaVu Sans, sans-serif;
    }
    </style>

Can someone help me, I already searching anywhere but still don’t find the solution

I use dompdf ver 0.8.0

EDIT: I follow chams by adding the line

$options = new Options();
$options->set('defaultFont', 'DejaVu Sans');
$dompdf = new Dompdf($options);

and the square is gone, I don’t know what is wrong
Update

EDIT: I just update the font, I’m using kai bold font, the square is coming back the error

EDIT: I just update the font again, I’m using NotoSansCJKtc-Regular, and still got the square.

font folder

2

Answers


  1. Chosen as BEST ANSWER

    I try many solution but still the chinese text is not shown anyone can help me?


  2. Make sure to a font that support Chinese characters which Dompdf by default don’t support.

    EDIT:

    require 'vendor/autoload.php';
    define('DOMPDF_FONT_DIR', __DIR__ . 'path-to-fonts-dir');
    
    $options = new Options();
    $options->set('isPhpEnabled', true); // Enable PHP evaluation
    $options->set('fontDir', DOMPDF_FONT_DIR);
    $dompdf = new Dompdf($options);
    
    $html = $this->load->view('view_view', $data, TRUE);
    $html = mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8');
    
    $options->set('defaultFont', 'NotoSansCJKtc-Regular');
    
    $dompdf->loadHtml($html);
    $dompdf->setPaper('A4', 'landscape');
    $dompdf->render();
    $dompdf->stream("View.pdf", array("Attachment" => false));
    
    exit(0);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search