Search code examples
phpcodeigniterdompdf

How to avoid pdf preview in browser and save it in a folder using dompdf


I want to avoid previewing PDF in the browser and save the pdf in a folder and i am using dompdf tool how can i achieve it . basically i will call this function from API so i want to avoid preview on the browser .

This is my code :

public function load_view($view, $data = array(),$id,$type)
{
   $dompdf = new Dompdf();
   $html = $this->ci()->load->view($view, $data, TRUE);
   $dompdf->loadHtml($html);
   $time = time();
   $dompdf->setPaper('portrait');
   $dompdf->render();
   file_put_contents('uploads'.'/'.'pdf'.'/'.$id.'_'.$type.".pdf", $dompdf->output());
}
}

Solution

  • You need to add header in your php code to inform browser to forcefully download the provided content.

    header('Content-Type: application/octet-stream');
    header("Content-Transfer-Encoding: Binary"); 
    header("Content-disposition: attachment; filename=\"".$filename."\""); 
    

    $filename : File will be downloaded with this name on client side