当使用特殊(slovic)字符đ时,PHP的TCPDF创建空PDF

时间:2015-05-13 21:11:01

标签: 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);
$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);

我找到了this answer但是改为这个也不起作用:

$pdf = new XTCPDF('P', PDF_UNIT, 'Letter', false, 'ISO-8859-1', false);

我的数据片段(用户名,地址等)目前包含如下:

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

问题:

我可以做些什么来改进:

  1. 我可以创建一个像“đ”
  2. 这样的字符的PDF
  3. 我可以进行设置,无论用户使用哪个角色,都会创建。

3 个答案:

答案 0 :(得分:5)

David van Driessche TCPDF example for including an external UTF-8 text file;使用的字体不支持đ字符。

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

require './tcpdf/tcpdf.php';

function write($pdf)
{
    $pdf->Write(0, 'test đ   ', '', 0, '', false, 0, false, false, 0);
    $pdf->writeHTML(trim(mb_convert_encoding('test2 đ', "HTML-ENTITIES", "UTF-8")), true, false, true, false, '');
}

$pdf = new TCPDF('P', PDF_UNIT, 'Letter', true, 'UTF-8', false);
$pdf->AddPage();

$pdf->SetFont('helvetica', '', 20);
write($pdf);

$pdf->SetFont('freesans', '', 20);
write($pdf);

$pdf->Output();

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

PDF result

查看standard TCPDF fonts,您可以看到以下非常有趣的内容:

$pdf->SetFont('freeserif', '', 12);

奇怪的是,freeserif并未列为AddFont()之一,而是在与TCPDF一起分发的fonts文件夹中(查看此文件夹也导致我freesans,所以我在上面的例子中使用了它。我只能假设文档不是最新的。

您似乎可以选择使用freesans或通过{{3}}方法加载第三方字体。

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

答案 1 :(得分:1)

在将字符串插入pdf之前尝试此操作。

#1. Create dataframe df1:

a1 <- c(1, 1, 1, 1, 2, 2, 2, 2, 1, 1)
b1 <- c(1, 5, 3, 2, 3, 4, 5, 1, 5, 2)
c1 <- c("white", "red", "black", "white", "red", 
        "white", "black", "silver", "red", "green")
df1 <- data.frame(a1, b1, c1)
df1

   a1 b1     c1
1   1  1  white
2   1  5    red
3   1  3  black
4   1  2  white
5   2  3    red
6   2  4  white
7   2  5  black
8   2  1 silver
9   1  5    red
10  1  2  green

#2. Create dataframe df2:

a2 <- c(2, 2, 1, 1, 2, 2, 2, 2, 2, 2)
b2 <- c(3, 1, 3, 2, 1, 3, 4, 5, 3, 5)
c2 <- c("black", "blue", "black", "white", "silver", 
        "green", "green", "red", "blue", "white")
df2 <- data.frame(a2, b2, c2)
df2

   a2 b2     c2
1   2  3  black
2   2  1   blue
3   1  3  black
4   1  2  white
5   2  1 silver
6   2  3  green
7   2  4  green
8   2  5    red
9   2  3   blue
10  2  5  white

#3. Assign unique IDs to each observation in df1:

library(data.table)
df1.2 <- data.table(df1, key="a1,b1,c1") 
df1.2[, id:=.GRP, by=key(df1.2)]
df1.2 <- as.data.frame(df1.2)
df1.2

   a1 b1     c1 id
1   1  1  white  1
2   1  2  green  2
3   1  2  white  3
4   1  3  black  4
5   1  5    red  5
6   1  5    red  5
7   2  1 silver  6
8   2  3    red  7
9   2  4  white  8
10  2  5  black  9

#4. The problematic part!! Assign identical unique IDs to matching observations of df2 as compared to df1.2 
#and assign other unique IDs to all other non-matching obs of df2. 
#Name the resulting dataframe as df2.2 
#My expected result will ideally look as follows:

df2.2

   a2 b2     c2 id
1   2  3  black 10 
2   2  1   blue 11
3   1  3  black  4
4   1  2  white  3
5   2  1 silver  6
6   2  3  green 12
7   2  4  green 13
8   2  5    red 14
9   2  3   blue 15
10  2  5  white 16

您还可以使用:html_entity_decode().

我遇到了与用户输入的内容类似的问题,这为我清理了它。

答案 2 :(得分:1)

在版本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
//============================================================+

对于此版本的下载链接,您可以找到此here

this就是它的外观PDF