How to encode UTF-8 in TCPDF

How to encode UTF-8 in TCPDF



I battling with UTF-8 encoding in TCPDF. After much search online I have tried everything including changing the font to freeserif.


freeserif



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.


<?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. rnrnPlease find attached list as requested.rn".$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
//============================================================+



This is my first attempt to send and email containing a pdf attachment






Have you tested saving the PDF to file and opening to verify the file is corrupt?

– mulquin
Sep 12 '18 at 3:14






@mulquin Yes I have

– Charles Tester
Sep 12 '18 at 3:15






OK, I would go through and comment out all $pdf-> calls apart from the bare essentials, then uncomment one at a time until failure occurs. I also don't think the header() function is required for this file.

– mulquin
Sep 12 '18 at 3:18



$pdf->


header()






@mulquin. Thanks that helped. I am battling with UTF-8 encoding on the script above. I need to be able to write a letter ë. Any suggestions?

– Charles Tester
Sep 14 '18 at 2:32





0



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)