使用PDFlib PHP生成PDF格式的重音和ç

我做了以下代码来传递一个字符串,它在pdf中生成我的行:

function text_block($p,$text,$xcrd,$ycrd)
{
    $font_size=24;  //font size, used to space lines on y axis
    $tmplines = explode("\n",$text);
    for($j=0;$j<count($tmplines);$j++){
      $tmptxt = explode(" ",$tmplines[$j]);
      $str="";
      for($i=0;$i<count($tmptxt);$i++){
      if($str=="")
        $str=sprintf("%s",$tmptxt[$i]);
      else
        $str=sprintf("%s %s",$str,$tmptxt[$i]);
      if((strlen($str)*$font_size + strlen($tmptxt[$i+1])*$font_size) > 1512){
        $p->fit_textline($str,$xcrd,$ycrd,"");
        $str="";
        $ycrd-=$font_size;
      }
    }
      $p->fit_textline($str,$xcrd,$ycrd,"");
      $ycrd-=$font_size;
  }
    return $ycrd;
}

因为当我在PDF中传递类似“é”或“ç”的内容时会出现奇怪的字符“Ô.

我该如何解决?

为例

有什么东西可以生成带重音的PDF?

$p = new PDFlib();
$p->set_option("stringformat=utf8");
$name_file = "wp-content/uploads/".$current_user->ID."_".$time."_mysite.pdf";
if ($p->begin_document($name_file, "") == 0) {
  die("Error: " . $p->get_errmsg());
}
$p->set_info("Creator", "mysyte.com.br");
$p->set_info("Author", $current_user->username);
$p->set_info("Title", "title");
$y = 2138;
$x = 1512;
$p->begin_page_ext($x, $y, "");
$y -= 100;
$y = text_block($p, "A data é: ".date("d/m/Y", $time), 50, $y);
$p->end_page_ext("");
$p->end_document("");

是以“数据é:03/23/2016”的形式出现在pdf中,但当我把它拉伸在php中并且不会返回错误或生成PDF时,我会做任何事情.我不知道是否缺少安装东西,或者是某种配置,或者你必须启用一些东西.

参考

示例“示例10-6.字体演示”这个site做我想要的,但我不能在我的.php中做,首先因为我没有pdflib.upr文件它传递了pdf_set_parameter.

第二,因为我不明白他在线上做了什么:

 pdf_continue_text($p,"$f (".chr(128)." Ç à á â ã ç è é ê)");

如果我移动到功能fit_textline,这条线将是

UPDATE

我尝试了另一个页面php类似的代码ai low.

try {
    $p = new PDFlib();
    if ($p->begin_document("wp-content/uploads/hello.pdf", "") == 0) {
            die("Error: " . $p->get_errmsg());
    }
    $p->set_info("Creator","hello.php"); 
    $p->set_info("Author","Rasmus Lerdorf"); 
    $p->set_info("Title","Hello world (PHP)");
    $p->set_parameter("FontOutline", "Ubuntu=/var/www/html/wordpress/wp-includes/fonts/ubuntu-font-family/Ubuntu-C.ttf"); 
    $p->set_parameter("textformat", "utf8"); 
    $y = 2138;
    $x = 1512;
    $p->begin_page_ext($x, $y, "");
    $p->set_text_pos(25,750);
    $font = $p->findfont("Ubuntu", "unicode", "");
    $font_size=24.0;  //font size, used to space lines on y axis
    $p->setfont($font, $font_size);
    $p->continue_text("(".chr(128)." Ç à á â ã ç è é ê)");
    $p->end_page_ext("");
    $p->end_document(""); 
}catch (\PDFlibException $e) {
    die("PDFlib exception occurred in hello sample:\n" .
    "[" . $e->get_errnum() . "] " . $e->get_apiname() . ": " .
    $e->get_errmsg() . "\n");
}catch (\Exception $e) {
    die($e);
}

但它仍然不会生成我的pdf,并且只是从服务器上的500错误中没有显示错误的输出,因为我已经使用try catch完成了处理.

使用过PDFlib的人知道怎么做吗?设置外部源并将文本类型设置为utf8,即它与重音和ç一起使用?

最佳答案 你可以用这个:

try {
    $p = new PDFlib();
    if ($p->begin_document("wp-content/uploads/katleia.pdf", "") == 0) {
            die("Error: " . $p->get_errmsg());
    }
    $p->set_info("Creator","hello.php"); 
    $p->set_info("Author","Rasmus Lerdorf"); 
    $p->set_info("Title","Hello world (PHP)");
    $p->set_parameter("textformat", "utf8"); 
    $y = 2138;
    $x = 1512;
    $p->begin_page_ext($x, $y, "");
    $p->set_text_pos(25,750);
    $font = $p->load_font("Helvetica-Bold", "winansi", "");
    $font_size=24.0;  //font size, used to space lines on y axis
    $p->setfont($font, $font_size);
    $p->continue_text("( Ç à á â ã ç è é ê)");
    $p->end_page_ext("");
    $p->end_document(""); 
}catch (\PDFlibException $e) {
   die("PDFlib exception occurred in hello sample:\n" .
   "[" . $e->get_errnum() . "] " . $e->get_apiname() . ": " .
   $e->get_errmsg() . "\n");
}catch (\Exception $e) {
   die($e);
}
点赞