0

I am able to create pdf file using mpdf. Now i want to download that generated file to USER's system just like IDM downloader. But i am unable to do so.

I tried with mpdf options like D,I,F. Only F is working but it save files to server which is not my requirement.

I also tried with which is commented now in code but it didnt work either.

 header('Content-type: application/force-download');
 header('Content-Disposition: attachment; filename=mpdf.pdf');
 readfile('mpdf.pdf');

Here is the code

<?php
    //db connection
    $id = $_REQUEST['id'];

    if($_REQUEST['action'] == "print") {
        mysql_query('SET character_set_results = utf8');
        $result = mysql_query("SELECT question_id FROM *** WHERE exam_id = '$id' ");
        $exam_details = mysql_fetch_assoc(mysql_query("SELECT name, duration, passing_percent, negative_marking  FROM *** WHERE id = '$id' "));
        $num = mysql_num_rows($result);
        while($row = mysql_fetch_assoc($result))
        {
            $results[] = $row['question_id'];
            $question_id = $row['question_id'];
            $result1 = mysql_query("SELECT question, option1, option2, option3, option4 FROM *** WHERE id = '$question_id' ");
            while($row1 = mysql_fetch_assoc($result1))
            {
                $results1[] = $row1;
            }
        }   
    }

    for($i=1; $i<=count($results1); $i++) {
        $question = $i.'. '.$results1[$i-1]['question'];
        $question = str_replace(array("\\","[","]"),"",$question);
        $question = str_replace("(","<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(",$question);
        $option1 = $results1[$i-1]['option1'];
        $option1 = str_replace(array("\\","[","]"),"",$option1);
        $option2 = $results1[$i-1]['option2'];
        $option2 = str_replace(array("\\","[","]"),"",$option2);
        $option3 = $results1[$i-1]['option3'];
        $option3 = str_replace(array("\\","[","]"),"",$option3);
        $option4 = $results1[$i-1]['option4'];
        $option4 = str_replace(array("\\","[","]"),"",$option4);

        $html .= '<p style="font-size: 12pt; font-family: freeserif "><div style="float: left; width: 100%; margin-bottom:5px; font-family: freeserif">'.$question.'</div>';

    }

    include("../mpdf.php");

    $mpdf=new mPDF('');
    $html = mb_convert_encoding($html, 'UTF-8', 'UTF-8');
    $mpdf->SetWatermarkImage('logo.jpg');
    $mpdf->showWatermarkImage = true;
    $mpdf->WriteHTML($html);

    $mpdf->Output('mpdf.pdf');

    //header('Content-type: application/force-download');
    //header('Content-Disposition: attachment; filename=mpdf.pdf');
    //readfile('mpdf.pdf');
?>

So, Please, tell me how to save generated PDF into user's system.

Mahesh Yadav
  • 87
  • 1
  • 7
  • You cannot "save" it to user's system. You can only offer the download. Depending on the browser and it's options it will either be displayed directly in the browser or be downloaded (with a user prompt). I'm afraid there's no way to force-download it. – Jeff Aug 20 '16 at 13:04
  • @Jeff ok. user prompt will do. But it must prompt. How to do that..? – Mahesh Yadav Aug 20 '16 at 15:54
  • have a look here: http://www.w3schools.com/tags/att_a_download.asp – Jeff Aug 20 '16 at 19:22
  • you will still need content-type headers for pdf (I think it's application/pdf). http://stackoverflow.com/questions/312230/proper-mime-media-type-for-pdf-files – Jeff Aug 20 '16 at 19:27

0 Answers0