32

I am newbie for mpdf so don't mind if you feel this question is stupid one:),

I generated the pdf document usinf mPDF class , the issue is that after pdf generated the browser opens it in tab . But i want it not to open but auto download , My code is like follwing..

include("../mpdf.php");
$html="my HTML code here !";

$mpdf=new mPDF('c','A4','','',32,25,27,25,16,13); 
$mpdf->SetDisplayMode('fullpage');
$mpdf->list_indent_first_level = 0; 
$stylesheet = file_get_contents('mpdfstyletables.css');
$mpdf->WriteHTML($stylesheet,1);
$mpdf->WriteHTML($html);
$mpdf->Output('mpdf.pdf');

I am expecting that there will be a function to download in the mpdf class like $mpdf->download instead $mpdf->Output('mpdf.pdf').I searched alot for this type solution but in vain i could't find any .

2 Answers 2

89

Add 'D' parameter for download

$mpdf->Output('MyPDF.pdf', 'D');
6
  • 1
    Edited, it is an download parameter, use D Aug 27, 2012 at 9:33
  • 19
    In their DOCS and Source, I is for browser output, F is for local save, S is to return as string and D is for download. Aug 27, 2012 at 9:36
  • Mihai ,how can we do print on button click ? is there any parameter for it or if you can tell another method? Aug 27, 2012 at 12:32
  • as we generate it i want that it directly go for print as well. Aug 27, 2012 at 12:54
  • That is not HTML, you have 2 possible options, let the browser display it, or force-download. No direct print .. Aug 27, 2012 at 12:55
13

for downloading use this

$filename = "mpdf.pdf";
if (file_exists($filename)) {
   header('Content-type: application/force-download');
   header('Content-Disposition: attachment; filename='.$filename);
   readfile($filename);
}
2
  • 4
    well i think you shud accept Mihai's answer as his ans is appropriate according to your ques
    – Uttara
    Aug 27, 2012 at 9:37
  • yes Milhai's second answer is best for my question , i accepted that , Aug 27, 2012 at 9:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.