I use dompdf
to generate a PDF file, Then I send it using PHPmailer
. But the issue is that the file name of the generated PDF name is not correct.
Here is the code:
//PDF from dompdf
$pdf = $dompdf->output();
//PHPmailer confiurgation
$mail->IsSMTP();
$mail->CharSet = 'UTF-8';
//PDF name
$name = 'محمد احمد علي';
$pdfName = str_replace(" ", "_", $name); //Replace spaces with _
$pdfName .= ".pdf";
//Attach PDF
$mail->AddStringAttachment($pdf, $pdfName);
The PDf name should be محمد_احمد_علي.pdf
in this case. But it's _احمد_علي.pdf
I tried:
$encoding = 'base64';
$type = 'application/pdf';
$mail->AddStringAttachment($pdf, $pdfName, $encoding, $type);
Also:
$mail->Encoding = "base64";
But no success.
I don't see that there is an issue with the string name, As when I run the following code:
$name = 'محمد علي احمد';
$name = str_replace(' ', "_", $name);
echo $name . '.pdf';
I get the expected result.
How to solve this issue?
Update
I just tried:
$dompdf->stream($filename);
To download the PDF without sending it. And it's the same result, So I don't know where the issue is coming from.
New Update In case this happens to anyone in the future.
I found out why this is happening. Dompdf
is using basename()
on the file name. And with my Arabic name that function makes it go wrong like that.
It's really sad how programming languages and libraries are ignoring RTL languages like Arabic.
So running this code:
echo basename('محمد احمد علي');
Would return احمد علي