title: How to encode UTF-8 in TCPDF
body: <p>I battling with UTF-8 encoding in TCPDF. After much search online I have tried everything including changing the font to <code>freeserif</code>.</p>
  
  <p>Another issue is that when I receive the email created by the code below, I get an error that Adobe cannot open the PDF because it is either not  supported file type (I made sure it is PDF) or was sent as an attachment and not correctly coded. </p>
  
  <pre><code>&lt;?php
  //This file create a pd contract agreement based on the client's detail. 
  // Include the main TCPDF library (search for installation path).
  require_once('tcpdf_include.php');
  
  require_once('../../connect.php');
  
  //Connect and get information
  
  // create new PDF document
  $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
  
  
  header('Content-Type: text/html; charset=utf-8');
  
  // set document information
  $pdf-&gt;SetCreator(PDF_CREATOR);
  $pdf-&gt;SetAuthor('ME');
  $pdf-&gt;SetTitle('My title');
  $pdf-&gt;SetSubject('TCPDF Tutorial');
  $pdf-&gt;SetKeywords('TCPDF, PDF, example, test, guide');
  
  // set default header data
  $pdf-&gt;SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' ', PDF_HEADER_STRING);
  
  // set header and footer fonts
  $pdf-&gt;setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
  $pdf-&gt;setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
  
  // set default monospaced font
  $pdf-&gt;SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
  
  // set margins
  $pdf-&gt;SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
  $pdf-&gt;SetHeaderMargin(PDF_MARGIN_HEADER);
  $pdf-&gt;SetFooterMargin(PDF_MARGIN_FOOTER);
  
  // set auto page breaks
  $pdf-&gt;SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
  
  // set image scale factor
  $pdf-&gt;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-&gt;setLanguageArray($l);
  }
  
  // ---------------------------------------------------------
  
  /*
  NOTES:
  - To create self-signed signature: openssl req -x509 -nodes -days 365000      -newkey rsa:1024 -keyout tcpdf.crt -out tcpdf.crt
  - To export crt to p12: openssl pkcs12 -export -in tcpdf.crt -out tcpdf.p12
  - To convert pfx certificate to pem: openssl pkcs12 -in tcpdf.pfx -out tcpdf.crt -nodes
  */
  
  // set certificate file
  $certificate = 'file://data/cert/tcpdf.crt';
  
  // set additional information
  $info = array(
  'Name' =&gt; 'name,
  'Location' =&gt; 'address',
  'Reason' =&gt; 'reason',
  'ContactInfo' =&gt; 'https://www.xxx.xxx',
  );
  
  // set document signature
  $pdf-&gt;setSignature($certificate, $certificate, 'tcpdfdemo', '', 2, $info);
  
  // set font
  $pdf-&gt;SetFont('freeserif', '', 7);
  
  // add a page
  $pdf-&gt;AddPage();
  
  // print a line of text
  $text = "&lt;html&gt;&lt;head&gt;&lt;/head&gt;&lt;body&gt;
  &lt;b&gt;Profile&lt;/b&gt;
  &lt;br&gt;
   &lt;br&gt;
  This is test text  
  &lt;br /&gt;&lt;br /&gt;
  
  &lt;/body&gt;&lt;/html&gt;
  ";
  
  $pdf-&gt;writeHTML($text, true, 0, true, 0);
  
  // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  // *** set signature appearance ***
  
  // create content for signature (image and/or text)
  
  // define active area for signature appearance
  $pdf-&gt;setSignatureAppearance(180, 60, 15, 15);
  
  // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  
  // *** set an empty signature appearance ***
  $pdf-&gt;addEmptySignatureAppearance(180, 80, 15, 15);
  
  // ---------------------------------------------------------
  
  
  
  //define the receiver of the email
  $to = $email;
  $subject = $subject;
  $repEmail = 'info@xxx.xxx';
  
  $fileName = 'My.pdf';
  $fileatt = $pdf-&gt;Output($fileName, 'E');
  $attachment = chunk_split($fileatt);
  $eol = PHP_EOL;
  $separator = md5(time());
  
  $headers = 'From: Identykidz &lt;'.$repEmail.'&gt;'.$eol;
  $headers .= 'MIME-Version: 1.0' .$eol;
  $headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"";
  
  $message = "--".$separator.$eol;
  $message .= "Content-Transfer-Encoding: 7bit".$eol.$eol;
  $message .= "Dear parent.  \r\n\r\nPlease find attached list as requested.\r\n".$eol;
  
  $message .= "--".$separator.$eol;
  $message .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
  $message .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
  
  $message .= "--".$separator.$eol;
  $message .= "Content-Type: application/pdf; name=\"".$fileName."\"".$eol; 
  $message .= "Content-Transfer-Encoding: base64".$eol;
  $message .= "Content-Disposition: attachment".$eol.$eol;
  $message .= $attachment.$eol;
  $message .= "--".$separator."--";
  
  
  // Send the email
  if(mail($to, $subject, $message, $headers)) {
    echo "Email sent";
  }
  else {
  
  echo "There was an error sending the mail.";
  }
  //============================================================+
  // END OF FILE
  //============================================================+
  </code></pre>
  
  <p>This is my first attempt to send and email containing a pdf attachment</p>