Как кодировать UTF-8 в TCPDF - PullRequest
       21

Как кодировать UTF-8 в TCPDF

0 голосов
/ 12 сентября 2018

Я борюсь с кодировкой UTF-8 в TCPDF.После долгих поисков в Интернете я перепробовал все, включая изменение шрифта на freeserif.

. Другая проблема заключается в том, что при получении письма, созданного с помощью приведенного ниже кода, я получаю сообщение об ошибке, что Adobe не может открыть PDF, потому чтолибо не поддерживается тип файла (я убедился, что это PDF), либо был отправлен как вложение и неправильно закодирован.

<?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->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('ME');
$pdf->SetTitle('My title');
$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.' ', PDF_HEADER_STRING);

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

// ---------------------------------------------------------

/*
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' => 'name,
'Location' => 'address',
'Reason' => 'reason',
'ContactInfo' => 'https://www.xxx.xxx',
);

// set document signature
$pdf->setSignature($certificate, $certificate, 'tcpdfdemo', '', 2, $info);

// set font
$pdf->SetFont('freeserif', '', 7);

// add a page
$pdf->AddPage();

// print a line of text
$text = "<html><head></head><body>
<b>Profile</b>
<br>
 <br>
This is test text  
<br /><br />

</body></html>
";

$pdf->writeHTML($text, true, 0, true, 0);

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// *** set signature appearance ***

// create content for signature (image and/or text)

// define active area for signature appearance
$pdf->setSignatureAppearance(180, 60, 15, 15);

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

// *** set an empty signature appearance ***
$pdf->addEmptySignatureAppearance(180, 80, 15, 15);

// ---------------------------------------------------------



//define the receiver of the email
$to = $email;
$subject = $subject;
$repEmail = 'info@xxx.xxx';

$fileName = 'My.pdf';
$fileatt = $pdf->Output($fileName, 'E');
$attachment = chunk_split($fileatt);
$eol = PHP_EOL;
$separator = md5(time());

$headers = 'From: Identykidz <'.$repEmail.'>'.$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
//============================================================+

Это моя первая попытка отправить и отправить электронное письмо с вложением в формате PDF

...