首页 文章

在mPDF生成的文档中使用字体

提问于
浏览
6

我希望你能帮助我解决这个问题,我第一次使用mPDF,我觉得这很棒,但是我希望用与我的网站相同类型的字体显示报告,在其文档中解释如何要做到这一点,但它似乎仍然不适合我 . 我到现在所做的是:

  • 添加网址从谷歌打开我的文档,然后生成它, <link href='http://fonts.googleapis.com/css?family=Open+Sans' rel='stylesheet' type='text/css'> .

  • 然后我添加一个样式表文件,其中包含使用字体 <link rel="stylesheet" href="<?php echo base_url(); ?>assets/publicidad/css/reporte.css" type="text/css"> Inside reporte.css,我've already added, I have a definition to use ' Open Sans'字体

body {font-family:'Open Sans',Sans-Serif; }按预期显示字体 .

  • 我用mPDF生成pdf,但是当我想要的时候效果很好
    在文档中显示Open Sans,它从http://www.fontsquirrel.com/fonts/open-sans打开Sans',并在其文档中说明添加到mPDF目录的ttfonts文件夹中 .

在它之后我按照这个明确的文档http://mpdf1.com/manual/index.php?tid=453,但到现在为止我直到现在它的部分是"4. To use the font with specific languages, you need also to edit the configuration file (config_cp.php); let us imagine that Frutiger contains a full set of characters needed for the Thai language:",但我不知道这个问题因为我使用了我放下的默认配置 .

function pdf_create($html, $filename, $stream = TRUE) {
            require_once(APPPATH . 'helpers/mpdf/mpdf.php');
            $mpdf = new mPDF();
            $mpdf->SetAutoFont();
            $mpdf->WriteHTML($html);
            if ($stream)
            {
                $mpdf->Output($filename . '.pdf', 'D');
            }
            else
            {
                $mpdf->Output('./uploads/temp/' . $filename . '.pdf', 'F');

                return './uploads/temp/' . $filename . '.pdf';
            }
        }

在我的控制器中,我这样做 .

$html = $this->load->view('publicidad/reporte_x_curso', $data, true);
$this->load->helper('mpdf');
pdf_create($html, 'Reporte_por_cursos', true);

(以上内容未被stackverflow编辑器识别)

最后我到目前为止应该按照文档执行的操作是:

$this->fontdata = array(
    "'Open Sans'" => array(
    'R' => 'OpenSans-Regular.ttf'
    )

PD:我把单引号,因为我在我的html文档中添加了这种方式,但我也试过没有它们,没有成功 . 我希望你能提前帮助我 .

3 回答

  • 12

    注意:

    首先,不要使用 mpdf1.com 网站,它已关闭 . 使用 https://github.com/mpdf/mpdf . Version 6 and 7 )有不同的代码

    示例(v7):

    source
    1)将 MyFont.ttf 上传到 /mpdf/ttfonts
    2)在 /mpdf/config_fonts.php ,在 $this->fontdata 数组内,添加你的:

    "my_custom_fonttt" => array( 
                        'R' => 'MyFont.ttf',            //Regular- REQUIRED
                        'I' => "MyFont-Oblique.ttf",    //Italic - OPTIONAL
                        'B' => "MyFont-Bold.ttf",       //Bold   - OPTIONAL
                        ),
    

    3)然后,无论您在何处执行自定义脚本,请在css中使用 my_custom_fonttt

    <?php
    $mpdf=new mPDF();
    $texttt= '
        <html><head><style>
        body {font-family: "my_custom_fonttt";}
        </style></head>
        <body>My SAMPLE TEXTTTTT </body>
        </html>';
    $mpdf->WriteHTML("$texttt");
    $mpdf->Output(dirname(__FILE__).'/new_created_file.pdf','F');
    ?>
    
  • 14

    我已经解决了我的问题,字体的定义中应该没有空格,所以我在我的css font-face声明中用“opensans”替换了

    $this->fontdata = array(
        "opensans" => array(
        'R' => 'OpenSans-Regular.ttf'
        )
    

    请注意字体类型('OpenSans-Regular.ttf')应该在mpdf文件夹的ttfonts文件夹中

  • 1

    我遇到了同样的问题:确保在定义字体名称时没有空格,保持小写 .

    它对我有用

相关问题