adding font to mPDF

后端 未结 6 1070
失恋的感觉
失恋的感觉 2020-12-14 02:32

I\'m getting the following error when I try and generate a PDF using the mPDF class:

TTF file \"C:/wamp/www/inc/mpdf/ttfonts/verdana.ttf\": invalid checksum          


        
6条回答
  •  天命终不由人
    2020-12-14 03:23

    There is another "dirty" way to add fonts dynamically in the run-time aside to the lib files. This was my solution because I wasn't able to modify config_fonts.pdf file since it was in vendor/ files and would be overwritten on library update.

    function add_custom_fonts_to_mpdf($mpdf, $fonts_list) {
        // Logic from line 1146 mpdf.pdf - $this->available_unifonts = array()...       
        foreach ($fonts_list as $f => $fs) {
            // add to fontdata array
            $mpdf->fontdata[$f] = $fs;
    
            // add to available fonts array
            if (isset($fs['R']) && $fs['R']) { $mpdf->available_unifonts[] = $f; }
            if (isset($fs['B']) && $fs['B']) { $mpdf->available_unifonts[] = $f.'B'; }
            if (isset($fs['I']) && $fs['I']) { $mpdf->available_unifonts[] = $f.'I'; }
            if (isset($fs['BI']) && $fs['BI']) { $mpdf->available_unifonts[] = $f.'BI'; }
        }
        $mpdf->default_available_fonts = $mpdf->available_unifonts;
    }
    

    Make sure to provide font paths relative to to mpdf's ttfonts/ dir

    IMPORTANT: CSS font-family will be transformed to lowercase + nospaces so "Source Sans Pro-Regular" will become sourcesanspro-regular

    For example here I'm adding 2 fonts and 3 font files because other font has regular and bold version:

    $mpdf = new mPDF('utf-8', 'A4', '', '', 20, 15, 50, 25, 10, 10);
    $custom_fontdata = array(
        'sourcesanspro-regular' => array(
            'R' => "../../../../wms/hr_frontend/job/internet/fonts/SourceSansPro-Regular/SourceSansPro-Regular.ttf" 
            // use 'R' to support CSS font-weight: normal
            // use 'B', 'I', 'BI' and etc. to support CSS font-weight: bold, font-style: italic, and both...
        ),
        'someotherfont' => array(
            'R' => "../../../../wms/hr_frontend/job/internet/fonts/someotherfont.ttf", // In CSS font-weight: normal
            'B' => "../../../../wms/hr_frontend/job/internet/fonts/someotherfont-bold.ttf" // In CSS font-weight: bold
        )
    );
    add_custom_font_to_mpdf($mpdf, $custom_fontdata);
    $mpdf->WriteHTML($html);
    

    This was for mpdf 5.x but hopefully it works for 6.x as well. Did anyone try?

提交回复
热议问题