23

I'm currently using mPDF to generate a pdf from HTML (which was generated by PHP).

All works as expected but I'd like to be able to change the default filename. Currently, I have:

$payStub=new mPDF();
$payStub->SetTitle('My title');
$payStub->WriteHTML($pcTableRows);
$payStub->Output();

When I save the pdf that opened in my browser it defaults to mpdf.pdf.
Is it possible to change mpdf.pdf to something of my choosing?

I tried

$payStub->Output('myFileName.pdf');

and

$payStub->Output('myFileName.pdf', 'F');

but those want to save it to the server, I'm trying to have it for when the user saves it locally.

Jason
  • 15,017
  • 23
  • 85
  • 116

3 Answers3

52

Try the I flag in the Output function, which will output the PDF to the browser, and use the filename from the first argument:

$payStub=new mPDF();
$payStub->SetTitle('My title');
$payStub->WriteHTML($pcTableRows);
$payStub->Output('yourFileName.pdf', 'I');
Trolley
  • 2,328
  • 2
  • 23
  • 28
22

You can try as:

$file_name = 'yourFileName.pdf';
$mpdf->Output($file_name, 'D');

Help:

  1. 'D': download the PDF file
  2. 'I': serves in-line to the browser
  3. 'S': returns the PDF document as a string
  4. 'F': save as file $file_out
Shiv Singh
  • 6,939
  • 3
  • 40
  • 50
  • 1
    Also mPDF supports 4 constants for them in Destination class. \Mpdf\Output\Destination::FILE ('F'), \Mpdf\Output\Destination::DOWNLOAD ('D'), \Mpdf\Output\Destination::STRING_RETURN ('S'), \Mpdf\Output\Destination::INLINE ('I') – OzgurG Mar 15 '20 at 19:12
-6

Modify mdpdf.php

form.setAttribute("action", "'._MPDF_URI.'includes/out.php/'.$name.'");

for downloading with other name

Arpit
  • 6,212
  • 8
  • 38
  • 69
Julian
  • 1