TCPDF for PHP在使用特殊(斯洛文尼亚语)字符đ时创建空PDF;使用

TCPDF for PHP在使用特殊(斯洛文尼亚语)字符đ时创建空PDF;使用,php,pdf,special-characters,tcpdf,Php,Pdf,Special Characters,Tcpdf,我正在使用TCPDF生成PDF(显然)。一切都很好,但现在我正试图制作一个带有字符“đ”(显然是一个斯洛伐克字符)的PDF 数据是用户生成的,所以我不知道他们是否打算使用这些类型的字符-这就是为什么我认为我应该使用UTF-8。但显然这是不对的 PDF已创建,但它完全为空/白色 这是我的相关代码: $pdf = new XTCPDF('P', PDF_UNIT, 'Letter', true, 'UTF-8', false); $pdf->SetPrintHeader(false); $pd

我正在使用TCPDF生成PDF(显然)。一切都很好,但现在我正试图制作一个带有字符“đ”(显然是一个斯洛伐克字符)的PDF

数据是用户生成的,所以我不知道他们是否打算使用这些类型的字符-这就是为什么我认为我应该使用UTF-8。但显然这是不对的

PDF已创建,但它完全为空/白色

这是我的相关代码:

$pdf = new XTCPDF('P', PDF_UNIT, 'Letter', true, 'UTF-8', false);
$pdf->SetPrintHeader(false);
$pdf->SetPrintFooter(false);
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
$pdf->SetMargins($pageMargin, $pageMargin, $pageMargin); //PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT
$pdf->SetAutoPageBreak(TRUE, $pageMargin/2);
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
$pdf->SetFont('helvetica', '', 10, '', true);

$pdf->AddPage();

$pdf->setJPEGQuality(70);
我发现,但改成这样也不行:

$pdf = new XTCPDF('P', PDF_UNIT, 'Letter', false, 'ISO-8859-1', false);
我的数据块(用户名、地址等)当前的包装方式如下:

trim(mb_convert_encoding($myHtml, "HTML-ENTITIES", "UTF-8"))

问题:

我可以更改什么以使其:

  • 我可以创建一个带有“đ”这样字符的PDF
  • 我可以进行设置,这样无论用户使用何种角色,它都可以创建

  • 在将字符串插入pdf之前,请尝试此操作

    $string = htmlentities( (string) $your_slovic_string, ENT_QUOTES, 'utf-8', FALSE);
    
    您还可以使用:

    我在用户输入的内容上遇到了类似的问题,这为我解决了问题。

    大卫·范·德里斯切(David van Driessche)已经;正在使用的字体不支持
    đ
    字符

    基本测试(请注意,我将此PHP脚本保存为UTF8文件):

    这为我提供了以下PDF输出(在Firefox中呈现):

    看一看,你可以看到以下非常有趣的一行:

    $pdf->SetFont('freeserif', '', 12);
    
    奇怪的是,
    freeserif
    没有被列为其中一种字体,而是在与TCPDF一起分发的
    font
    文件夹中(查看此文件夹,我也找到了
    freeserans
    ,所以我在上面的示例中使用了它)。我只能假设文档不是最新的

    您可以选择使用
    freeans
    或通过该方法加载第三方字体


    编辑:我用稍旧的TCPDF:6.0.099版本测试了上述内容。

    在6.2.8版中,我通过添加字符đ修改了第一个示例,一切似乎都正常。代码如下:

    // Include the main TCPDF library (search for installation path).
    require_once('tcpdf_include.php');
    
    // create new PDF document
    $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
    
    // set document information
    $pdf->SetCreator(PDF_CREATOR);
    $pdf->SetAuthor('Nicola Asuni');
    $pdf->SetTitle('TCPDF Example 001');
    $pdf->SetSubject('TCPDF Tutorial');
    $pdf->SetKeywords('TCPDF, PDF, example, test, guide');
    
    // set default header data
    $pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' 001', PDF_HEADER_STRING, array(0,64,255), array(0,64,128));
    $pdf->setFooterData(array(0,64,0), array(0,64,128));
    
    // set header and footer fonts
    $pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
    $pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
    
    // set default monospaced font
    $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
    
    // set margins
    $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
    $pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
    $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
    
    // set auto page breaks
    $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
    
    // set image scale factor
    $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
    
    // set some language-dependent strings (optional)
    if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
        require_once(dirname(__FILE__).'/lang/eng.php');
        $pdf->setLanguageArray($l);
    }
    
    // ---------------------------------------------------------
    
    // set default font subsetting mode
    $pdf->setFontSubsetting(true);
    
    // Set font
    // dejavusans is a UTF-8 Unicode font, if you only need to
    // print standard ASCII chars, you can use core fonts like
    // helvetica or times to reduce file size.
    $pdf->SetFont('dejavusans', '', 14, '', true);
    
    // Add a page
    // This method has several options, check the source code documentation for more information.
    $pdf->AddPage();
    
    // Set some content to print
    $html = <<<EOD
    <h1>Welcome tćžžćččšđ <a href="http://www.tcpdf.org" style="text-decoration:none;background-color:#CC0000;color:black;">
    &nbsp;<span style="color:black;">TC</span><span style="color:white;">PDF</span>&nbsp;</a>!</h1>
    <i>This is the first example of TCPDF library.</i>
    <p>This text is printed using the <i>writeHTMLCell()</i> method but you can also use: <i>Multicell(), writeHTML(), Write(), Cell() and Text()</i>.</p>
    <p>Please check the source code documentation and other examples for further information.</p>
    <p style="color:#CC0000;">TO IMPROVE AND EXPAND TCPDF I NEED YOUR SUPPORT, PLEASE <a href="http://sourceforge.net/donate/index.php?group_id=128076">MAKE A DONATION!</a></p>
    EOD;
    
    // Print text using writeHTMLCell()
    $pdf->writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, '', true);
    
    // ---------------------------------------------------------
    
    // Close and output PDF document
    // This method has several options, check the source code documentation for more information.
    $pdf->Output('example_001.pdf', 'I');
    
    //============================================================+
    // END OF FILE
    //============================================================+
    
    //包括主TCPDF库(搜索安装路径)。
    require_once('tcpdf_include.php');
    //创建新的PDF文档
    $pdf=新的TCPDF(pdf页面方向,pdf单元,pdf页面格式,真,'UTF-8',假);
    //设置文档信息
    $pdf->SetCreator(pdf\u CREATOR);
    $pdf->SetAuthor('Nicola Asuni');
    $pdf->SetTitle('TCPDF示例001');
    $pdf->SetSubject(“TCPDF教程”);
    $pdf->SetKeywords('TCPDF,pdf,示例,测试,指南');
    //设置默认标题数据
    $pdf->SetHeaderData(pdf_头_徽标,pdf_头_徽标_宽度,pdf_头_标题'001',pdf_头_字符串,数组(0,64255),数组(0,64128));
    $pdf->setFooterData(数组(0,64,0),数组(0,64128));
    //设置页眉和页脚字体
    $pdf->setHeaderFont(数组(pdf\u FONT\u NAME\u MAIN',pdf\u FONT\u SIZE\u MAIN));
    $pdf->setFooterFont(数组(pdf字体名称数据),pdf字体大小数据);
    //设置默认的单间距字体
    $pdf->SetDefaultMonospacedFont(pdf\u字体\u等距);
    //设置边距
    $pdf->SetMargins(pdf\u MARGIN\u左、pdf\u MARGIN\u上、pdf\u MARGIN\u右);
    $pdf->SetHeaderMargin(pdf\u MARGIN\u HEADER);
    $pdf->SetFooterMargin(pdf\U MARGIN\U FOOTER);
    //设置自动分页符
    $pdf->SetAutoPageBreak(TRUE,pdf\u MARGIN\u BOTTOM);
    //设置图像比例因子
    $pdf->setImageScale(pdf图像比例);
    //设置一些语言相关字符串(可选)
    如果(@file_存在(dirname(_文件)。'/lang/eng.php')){
    require_once(dirname(__文件)'/lang/eng.php');
    $pdf->setLanguageArray($l);
    }
    // ---------------------------------------------------------
    //设置默认字体子集模式
    $pdf->setFontSubsetting(真);
    //设置字体
    //dejavusans是一种UTF-8 Unicode字体,如果您只需要
    //打印标准ASCII字符,可以使用核心字体,如
    //helvetica或减少文件大小的时间。
    $pdf->SetFont('dejavusans','',14',,true);
    //添加页面
    //此方法有多个选项,有关详细信息,请查看源代码文档。
    $pdf->AddPage();
    //设置一些要打印的内容
    
    $html=尝试另一个字符集。搜索斯洛文尼亚字符集。@RobinR-使用斯洛文尼亚字符集会使我无法使用其他非斯拉夫字符,对吗?你能发布一个示例(空)PDF文件吗?看看它是否仍然包含任何内容可能会很有趣,如果是,那是什么。另外,您是否尝试过设置标准PDF字体以外的其他字体?您使用的是Helvetica,文档说明默认字体使用“cp1252”编码,据我所知,该编码不支持您所说的字符。了解该字符集中的其他特殊字符是否起作用是很有趣的。我更新了我的答案,说明了我是如何包装我的数据的——我相信这是一样的(基本上),但当我尝试你的答案时,我会看到所有的HTML字符都显示出类似的名称(并且所讨论的字符是一个实际的问号)你想在你的PDF文件中输入斯洛文尼亚语字符吗?还是不?因为否则你可以设置标题。。。试着多解释一下。谢谢!关于如何保持低文件大小有什么想法吗?从helvetica更改为freesans(或dejavusans)几乎是文件大小的四倍(15k到60k)。有什么可以做的吗?我试着打开子集设置,但速度较慢,只降低到59k。@Dave唯一想到的是
    setfontsubseting(true)
    ,但正如你所说,你已经尝试过了,我不知道还有什么建议。相对而言,60K仍然很小;除非你的应用程序被大量使用,否则我认为折衷(文件大小与字符支持)是值得的。好的,谢谢。60k“还可以”,但是-仍然是四倍大,我希望它尽可能小。也就是说,我认为拥有更大的字体集必然会创建更大的文件。只是没想到它会大四万五千(300%)。
    // Include the main TCPDF library (search for installation path).
    require_once('tcpdf_include.php');
    
    // create new PDF document
    $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
    
    // set document information
    $pdf->SetCreator(PDF_CREATOR);
    $pdf->SetAuthor('Nicola Asuni');
    $pdf->SetTitle('TCPDF Example 001');
    $pdf->SetSubject('TCPDF Tutorial');
    $pdf->SetKeywords('TCPDF, PDF, example, test, guide');
    
    // set default header data
    $pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' 001', PDF_HEADER_STRING, array(0,64,255), array(0,64,128));
    $pdf->setFooterData(array(0,64,0), array(0,64,128));
    
    // set header and footer fonts
    $pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
    $pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
    
    // set default monospaced font
    $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
    
    // set margins
    $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
    $pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
    $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
    
    // set auto page breaks
    $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
    
    // set image scale factor
    $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
    
    // set some language-dependent strings (optional)
    if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
        require_once(dirname(__FILE__).'/lang/eng.php');
        $pdf->setLanguageArray($l);
    }
    
    // ---------------------------------------------------------
    
    // set default font subsetting mode
    $pdf->setFontSubsetting(true);
    
    // Set font
    // dejavusans is a UTF-8 Unicode font, if you only need to
    // print standard ASCII chars, you can use core fonts like
    // helvetica or times to reduce file size.
    $pdf->SetFont('dejavusans', '', 14, '', true);
    
    // Add a page
    // This method has several options, check the source code documentation for more information.
    $pdf->AddPage();
    
    // Set some content to print
    $html = <<<EOD
    <h1>Welcome tćžžćččšđ <a href="http://www.tcpdf.org" style="text-decoration:none;background-color:#CC0000;color:black;">
    &nbsp;<span style="color:black;">TC</span><span style="color:white;">PDF</span>&nbsp;</a>!</h1>
    <i>This is the first example of TCPDF library.</i>
    <p>This text is printed using the <i>writeHTMLCell()</i> method but you can also use: <i>Multicell(), writeHTML(), Write(), Cell() and Text()</i>.</p>
    <p>Please check the source code documentation and other examples for further information.</p>
    <p style="color:#CC0000;">TO IMPROVE AND EXPAND TCPDF I NEED YOUR SUPPORT, PLEASE <a href="http://sourceforge.net/donate/index.php?group_id=128076">MAKE A DONATION!</a></p>
    EOD;
    
    // Print text using writeHTMLCell()
    $pdf->writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, '', true);
    
    // ---------------------------------------------------------
    
    // Close and output PDF document
    // This method has several options, check the source code documentation for more information.
    $pdf->Output('example_001.pdf', 'I');
    
    //============================================================+
    // END OF FILE
    //============================================================+