如何在 Laravel 中下载运单 pdf 而不是在浏览器中打开
How to download a waybill pdf instead open in browser in Laravel
下面是我的 Laravel 函数,用于从快递提供商处生成运单。目前,代码运行良好,但运单 pdf 直接在浏览器中打开。我如何强制它下载 pdf 文件?
public function awb(Request $request)
{
function e($str) {print'<pre>';print_r($str);print'</pre>';}
$url = 'http://web.com/print/print.action';
$key = $request->key;
$billCode = $key;
$data = array(
'billcode' => $billCode,
'account' => 'xxx',
'password' => 'xxx',
'customercode' => 'xxx',
);
$t = array('logistics_interface' => json_encode($data), 'msg_type' =>
'1', 'data_digest' => md5($billCode));
$s = curl_init();
curl_setopt($s,CURLOPT_URL,$url);
curl_setopt($s,CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($s,CURLOPT_RETURNTRANSFER,true);
curl_setopt($s,CURLOPT_POSTFIELDS,http_build_query($t));
header('Content-type: application/pdf');
$r = curl_exec($s);
curl_close($s);
e($r);
}
您需要将请求保存到文件并return下载
public function awb(Request $request)
{
function e($str) {print'<pre>';print_r($str);print'</pre>';}
$url = 'http://web.com/print/print.action';
$key = $request->key;
$billCode = $key;
$data = array(
'billcode' => $billCode,
'account' => 'xxx',
'password' => 'xxx',
'customercode' => 'xxx',
);
$t = array('logistics_interface' => json_encode($data), 'msg_type' =>
'1', 'data_digest' => md5($billCode));
$s = curl_init();
curl_setopt($s,CURLOPT_URL,$url);
curl_setopt($s,CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($s,CURLOPT_RETURNTRANSFER,true);
curl_setopt($s,CURLOPT_POSTFIELDS,http_build_query($t));
//header('Content-type: application/pdf');
$r = curl_exec($s);
curl_close($s);
$f = 'pdf/download.pdf';
Storage::disk('local')->put($f, $r);
return Response::download(Storage::disk('local')->getDriver()->getAdapter()->applyPathPrefix($f));
}
下面是我的 Laravel 函数,用于从快递提供商处生成运单。目前,代码运行良好,但运单 pdf 直接在浏览器中打开。我如何强制它下载 pdf 文件?
public function awb(Request $request)
{
function e($str) {print'<pre>';print_r($str);print'</pre>';}
$url = 'http://web.com/print/print.action';
$key = $request->key;
$billCode = $key;
$data = array(
'billcode' => $billCode,
'account' => 'xxx',
'password' => 'xxx',
'customercode' => 'xxx',
);
$t = array('logistics_interface' => json_encode($data), 'msg_type' =>
'1', 'data_digest' => md5($billCode));
$s = curl_init();
curl_setopt($s,CURLOPT_URL,$url);
curl_setopt($s,CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($s,CURLOPT_RETURNTRANSFER,true);
curl_setopt($s,CURLOPT_POSTFIELDS,http_build_query($t));
header('Content-type: application/pdf');
$r = curl_exec($s);
curl_close($s);
e($r);
}
您需要将请求保存到文件并return下载
public function awb(Request $request)
{
function e($str) {print'<pre>';print_r($str);print'</pre>';}
$url = 'http://web.com/print/print.action';
$key = $request->key;
$billCode = $key;
$data = array(
'billcode' => $billCode,
'account' => 'xxx',
'password' => 'xxx',
'customercode' => 'xxx',
);
$t = array('logistics_interface' => json_encode($data), 'msg_type' =>
'1', 'data_digest' => md5($billCode));
$s = curl_init();
curl_setopt($s,CURLOPT_URL,$url);
curl_setopt($s,CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($s,CURLOPT_RETURNTRANSFER,true);
curl_setopt($s,CURLOPT_POSTFIELDS,http_build_query($t));
//header('Content-type: application/pdf');
$r = curl_exec($s);
curl_close($s);
$f = 'pdf/download.pdf';
Storage::disk('local')->put($f, $r);
return Response::download(Storage::disk('local')->getDriver()->getAdapter()->applyPathPrefix($f));
}