如何在浏览器中避免PDF预览,并使用DOMPDF将其保存在文件夹中



我想避免在浏览器中预览PDF并将PDF保存在文件夹中,并且我正在使用DOMPDF工具,该如何实现。基本上,我将从API调用此功能,因此我想避免在浏览器上预览。

这是我的代码:

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());
}
}

您需要在php代码中添加标头才能告知浏览器以强制下载提供的内容。

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

$ fileName:文件将在客户端上下载此名称

相关内容

最新更新