8

I am using mPDF to save form input data to PDF. For English, it is working fine. Anyone can use this code to save HTML Form data to PDF.

Issue: In order to fulfill my project requirement I need to use the Chinese Language. My current code is not working for that.

Form.html

<form action='processPDF.php' method='post'>
    <label for="name">Name</label>
    <input name="name" type="text" id="name">
    <input type='submit' name='submit' value='Download PDF'>
</form>

processPDF.php

<?php
header('Content-Type: text/html; charset=UTF-8');
if (isset($_POST['submit'])) {
    if (isset($_POST['name'])) {
        $name = $_POST['name'];
    } else {
        $Larmtid = '';
    }
    if (!isset($error)) {
        ob_start();
?>        
<div style="padding:20px;">
            <p>Name: <?php
        echo $name;
?></p>
        </div>
        <?php
        $body = ob_get_clean();
        $body = iconv('UTF-8', 'UTF-8//IGNORE', $body);
        $body = iconv('UTF-8', 'UTF-8//TRANSLIT', $body);
        include("mpdf/mpdf.php");
        $mpdf = new \mPDF('c', 'A4', '', '', 0, 0, 0, 0, 0, 0);
        $mpdf->SetAutoFont();
        $mpdf->autoScriptToLang = true;
        $mpdf->autoLangToFont   = true;
        $mpdf->WriteHTML($body);
        $mpdf->Output('SavePDF.pdf', 'D');
    }

}
?>

The problem I am having is: In the input field, I typed 怎么用中文说话 and it prints ��������.

If you want to download the source code here is the link to the code

STF
  • 1,485
  • 3
  • 19
  • 36
ZiaUllahZia
  • 1,072
  • 2
  • 16
  • 30
  • Can you define "not working" by telling us what results you're presently gettting? This could also be a file encoding issue. The question lacks detail. – Funk Forty Niner Oct 31 '17 at 13:47
  • In the input field, I typed 怎么用中文说话 and it prints ???????? on pdf – ZiaUllahZia Oct 31 '17 at 13:53
  • Did you need more information. I am doing my research will keep you update. – ZiaUllahZia Oct 31 '17 at 15:39
  • I am not sure about mPDF but I have used TCPDF for unicode documents and it worked. I did need to change the default font, to one that supported unicode characters, perhaps that is your problem with mPDF – bumperbox Oct 31 '17 at 21:18
  • I will try that. If you have any resource please let me know. Thank – ZiaUllahZia Oct 31 '17 at 21:42
  • Your script works (I see Chinese characters) on my system with mPdf 7.1.8, I removed `$mpdf->SetAutoFont();` and created `$mpdf` using `$mpdf = new \Mpdf\Mpdf();`. I think the error was: **(1)** How your system was encoding the text (your browser was sending the data to your script, were you using a Chinese OS or a fallback encoding kicking in and scrambling your text? Log the `$_POST` variables into a file to see what actually got sent to your php script) **(2)** Which program were you viewing your resulting PDF in? **(3)** It was a bug in mPdf v. 5 and 6 which got fixed in mPdf 7? – mxl Feb 07 '19 at 19:35

4 Answers4

7

Do not use 'c' as a $mode parameter, that means PDF core fonts only and they do not support chinese characters.

Try '+aCJK' or '-aCJK' instead.

See examplefiles using chinese font.

Finwe
  • 6,372
  • 2
  • 29
  • 44
  • Hi Finwe, I tried this but unfortunately did not work. I tried to download it on the composer, which is a recomented way. Can you please help me. I am using bootstrap with that. I support external css libraries also. The only one issue is that before your answer, the output was ??? for Chinese and now I am getting output brackets but when I past those bracket for example here 怎么用中文说话 it became chines – ZiaUllahZia Nov 01 '17 at 10:17
  • Your code works correctly for me, with `$mpdf=new \mPDF('+aCJK','A4','','' , 0, 0, 0, 0, 0, 0);` – Finwe Nov 01 '17 at 10:24
  • Yes it did work with that. After follow documentation I installed the latest installed the new version. It is possible to pass those parameters in array as it need array. $mpdf = new \Mpdf\Mpdf([ 'mode' => '+aCJK' ]); – ZiaUllahZia Nov 01 '17 at 11:25
  • Sure. So I guess your problem is solved? In that case please mark the answer as accepted and also consider an upvote, thanks. – Finwe Nov 01 '17 at 11:48
7

My Code is as follow [mpdf v7.0 from composer]

<?php
require_once './vendor/autoload.php';
//report errors
error_reporting(E_ALL);
ini_set("display_errors", 1);

$config = [
    'mode' => '+aCJK', 
    // "allowCJKoverflow" => true, 
    "autoScriptToLang" => true,
    // "allow_charset_conversion" => false,
    "autoLangToFont" => true,
];
$mpdf=new \Mpdf\Mpdf($config);

$mpdf->WriteHTML('Hello World 中文');
$mpdf->Output();

This code works fine, you can try it

Martin Ding
  • 149
  • 1
  • 6
2

// We can pass Language code in mpdf config using mode.

<?php
$config = [
    'mode' => [LANGUAGE_CODE], // Example: zh-Hans, en, fr etc
];
$mpdf = new \Mpdf\Mpdf($config);
0

To display Chinese characters correctly, we need to download a fully Chinese support font and place it into a directory, for instance: /app/fonts/yahei.ttf

We can instance it using ConfigVariables and FontVariables:

use Mpdf\Config\ConfigVariables;
use Mpdf\Config\FontVariables;

// Get the default font dirs and append the custom path to it
$defaultConfig = (new ConfigVariables())->getDefaults();
$fontDirs = $defaultConfig['fontDir'];
$fontDirs[] = '/app/fonts/';
// Get the default font data and add the yahei font
$defaultFontConfig = (new FontVariables())->getDefaults();
$fontData = $defaultFontConfig['fontdata'];
$fontData['yahei'] = [
    'R' => 'yahei.ttf'
];

$mpdf = new \Mpdf\Mpdf([
    'mode' => '+aCJK',
    'setAutoTopMargin' => 'stretch',
    'setAutoBottomMargin' => 'stretch',
    'default_font' => 'yahei',
    "autoScriptToLang" => true,
    "autoLangToFont" => true,
    'fontDir' => $fontDirs,
    'fontdata' => $fontData,
]);

Now you can display Chinese characters

pepiyu
  • 1