简体   繁体   中英

Generate accents and ç in PDF with PDFlib + PHP

I made the following code to pass a string and it generates my lines in 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;
}

Because when I pass something like "é" or "ç" in PDF appears strange characters "é".

How do I fix this?

EXEMPLE

Something will be to generate a PDF with accents?

$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("");

was to appear in pdf something like "A data é: 03/23/2016", but when I put the bla stretch it hangs in php and does not return error, or generate PDF, ne do anything. I do not know if lack install something, or is some configuration of thing, or you must enable something.

REFERENCE

The example "Example 10-6. Font demonstration" this site do what I want, but I can not do in my .php, first because I have not pdflib.upr file it passes the pdf_set_parameter .

Second because I did not understand what he is doing on the line:

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

as this line would be, if I move to fuction fit_textline

UPDATE

I tried on another page php similar code 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);
}

But still it does not generate my pdf, and also just from a 500 error on the server does not show the outputs of the error as it should because I have done the treatment with try catch.

Someone who has worked with the PDFlib know how to do this? Set a external source and put the type of text to utf8, ie it works with accents and ç?

You can use this:

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);
}

first of all: why not using textflow (the multi line functionality of PDFlib) to create multiline output?

and now to your wrong output:

  • I guess you have "é" (which is the utf-8 two byte representation of "é") in your input strings, but you have not set the textformat in PDFlib to utf-8. The textformat should match to the input strings you apply.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM