Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unknown error when embedding adobeCJK custom fonts into pdf #64

Closed
vincent-cm opened this issue Dec 27, 2017 · 1 comment
Closed

Unknown error when embedding adobeCJK custom fonts into pdf #64

vincent-cm opened this issue Dec 27, 2017 · 1 comment

Comments

@vincent-cm
Copy link

vincent-cm commented Dec 27, 2017

Aiming to embed a custom font into the final pdf, working well in 6.0 but something wrong when referencing the 7.0 document to migrate the implementation.

Using: php 7.2.0, CodeIgniter 3.1.6, mpdf 7.0, HTML page main language: zh-CN, zh-HK, zh-TW
Here is what happens:
I was about to render an HTML page which was no lang tag, css font-family is "SF Pro SC" which is widely known as Apple's system default font. In CJK world, the "SF Pro SC" must fall back to "PingFang SC" to support zh language so I have to set default fonts and do some work to support:

public function create_pdf($pdf)
{

    $defaultConfig = (new Mpdf\Config\ConfigVariables())->getDefaults();
    $fontDirs = $defaultConfig['fontDir'];

    $defaultFontConfig = (new Mpdf\Config\FontVariables())->getDefaults();
    $fontData = $defaultFontConfig['fontdata'];


    file_put_contents('php://stderr', print_r(array_merge($fontDirs, [
        base_url() . 'resource/fonts/SF-Pro-SC/v1/',
    ]), TRUE));
    
    $mpdf = new \Mpdf\Mpdf([
        'mode' => '+aCJK',
        'format' => 'A4',
        'mgl' => 15,
        'mgr' => 15,
        'mgt' => 15,
        'mgb' => 16,
        'mgh' => 9,
        'mgf' => 9,
        'orientation' => 'P',
        'watermark_font' => 'sfprosc',
        'fontDir' => array_merge($fontDirs, [
            base_url() . 'resource/fonts/SF-Pro-SC/v1/',
        ]),
        'fontdata' => $fontData + [
                'sfprosc' => [
                    'R' => "PingFangSC-Thin.ttf",
                    'B' => "PingFangSC-Regular.ttf",
                    'I' => "PingFangSC-Light.ttf",
                    'BI' => "PingFangSC-SemiBold.ttf"
                ]
            ],
        'default_font' => 'sfprosc',
        'autoLangToFont' => true,
        'autoScriptToLang' => true,
        'languageToFont' => new CustomLanguageToFontImplementation(),
        'debugfonts' => true
    ]);
    $mpdf->WriteHTML($pdf['html']);
    if (isset($pdf['attach'])) {
        $mpdf->Output('./resource/tmp/' . $pdf['filename'], 'F');
        return base_url() . 'resource/tmp/' . $pdf['filename'];
    } else {
        $mpdf->Output($pdf['filename'], 'D');
        exit;
    }
}

I choose the customise word "sfprosc" to link the font file and set it to be the default font, then I set 'mode' => '+aCJK', 'watermark_font' => 'sfprosc', and both autoLangToFont and autoScriptToLang are TRUE,

Something weird is after I added 'fontdata' config variable, the page is freezing and nothing to display after running this function. Also something I have to change a little bit is the implementation of CustomLanguageToFontImplementation(), I cannot extends the interface like the documents suggested:
class CustomLanguageToFontImplementation implements \Mpdf\Language\LanguageToFontInterface
{

public function getLanguageOptions($llcc, $adobeCJK)
{

    $tags = explode('-', $llcc);
    $lang = strtolower($tags[0]);
    $country = '';
    $script = '';
    if (!empty($tags[1])) {
        if (strlen($tags[1]) === 4) {
            $script = strtolower($tags[1]);
        } else {
            $country = strtolower($tags[1]);
        }
    }
    if (!empty($tags[2])) {
        $country = strtolower($tags[2]);
    }

    $unifont = '';
    $coreSuitable = false;

    if ($lang && ($lang == 'zh' || $lang == 'zho')) {
        $unifont = 'sfprosc';
        if ($adobeCJK) {
            $unifont = 'sfprosc';
            if ($country === 'HK' || $country === 'TW') {
                $unifont = 'sfprosc';
            }
        }
        return [false, $unifont];
    } else {
        /* Undetermined language - script used */
        $unifont = 'sfprosc';
        return [false, $unifont];
    }

    return parent::getLanguageOptions($llcc, $adobeCJK);
}

}

In mpdf6.0, I only force the $unifont = 'sfprosc' in the config_lang2fonts.php before return array($coreSuitable, $unifont); and that's working fine.

@finwe
Copy link
Member

finwe commented Dec 27, 2017

Please use mpdf/mpdf repository to report library bugs.

@finwe finwe closed this as completed Dec 27, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants