Hello,
As using Yii's mpdf() extenstion for generating pdf file.
code is as below:

$mPDF1 = Yii::app()->ePdf->mpdf();
$stylesheet = file_get_contents(Yii::getPathOfAlias('webroot.css') . '/screen.css');
$mPDF1->WriteHTML($stylesheet, 1);
$Arraylist= array('employee_gslab_id'=>$employee_gslab_id,'other_details'=>$employee_details);
$mPDF1->WriteHTML($this->renderPartial('address_proof',$Arraylist,true));
$mPDF1->Output($name, EYiiPdf::OUTPUT_TO_DOWNLOAD);

everything is working as expected except when this file is downloaded/saved then it is not showing extesion to file.
how can I add (.pdf) extension to it?

Dani AI

Generated

Short summary: the missing extension was caused by the filename you passed when sending the PDF. As discovered, the simplest fix is to make sure the filename actually ends with ".pdf". Beyond that, there are a few practical hardening steps and checks to make the behavior consistent across browsers and operating systems.

Ensure a safe, explicit filename before sending

  • Make sure the name variable is non-empty, append ".pdf" when the extension is missing, and remove unsafe characters. Example sanitation (does not call the PDF library directly):
$base = trim($name);
if ($base === '') $base = 'document';
if (strtolower(pathinfo($base, PATHINFO_EXTENSION)) !== 'pdf') {
    $base .= '.pdf';
}
$base = preg_replace('/[^\w\-.]/', '_', $base);

Headers, character sets and edge cases

  • If you stream the PDF yourself (or generate it to a string), ensure the response headers match the filename. For non-ASCII names include an RFC5987-style filename* for better browser support. If you let the library set headers, verify it included the filename you expect.

Troubleshooting tips

  • If a saved file still appears to have no extension, check the OS: Windows often hides known extensions (Explorer option "Hide extensions for known file types"). Also confirm the file is actually a PDF — the file should start with the bytes “%PDF-”. These checks will rule out library or browser quirks and help you choose whether to adjust headers, sanitize the name, or force a download filename.

Hello,
So stupid I was,
I just changed line
$mPDF1->Output($name, EYiiPdf::OUTPUT_TO_DOWNLOAD);
to
$mPDF1->Output($name.".pdf", EYiiPdf::OUTPUT_TO_DOWNLOAD);

it get solved!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.