尝试在laravel中的/public/assets/others/brochure/jan-2016.pdf
路由中查看存储在brochure/jan-2016.pdf', 'BrochureController@show');
文件中的pdf。
但我收到的错误为:Whoops, looks like something went wrong
路线代码:
Route::get('/brochure/{file}', 'BrochureController@show');
在控制器中:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class BrochureController extends Controller
{
public function show($filename)
{
$filename = 'jan-2016.pdf';
$filepath = 'assets/others/brochure/';
$path = $filepath.$filename;
return Response::make(file_get_contents($path), 200, [
'Content-Type' => 'application/pdf',
'Content-Disposition' => 'inline; '.$filename,
]);
}
}
当我尝试仅打印文件内容时,它会捕获文件并使用file_get_contents($path)
以某种奇怪的方式显示
输出: %PDF-1.4 %���� 1 0 obj <>stream PScript5.dll Version 5.2.2 GVTP January 2016 Dummy content endstream endobj 2 0 obj <> endobj 4 0 obj <> endobj 3 0 obj <> endobj 5 0 obj <>/Rotate 90/Parent 3 0 R/MediaBox[0 0 595 842]/Contents 11 0 R/Type/Page>> endobj 11 0 obj <>stream x��]Ys�u.;����8Ξ��d���z_�'�Z���Iت�� �$����_��oN�L�9=sw@��-...
这意味着它从确切的路径中选择文件。我认为有一位读者可以将其作为第三方服务来处理,例如:Adobe pdf阅读器。
我做错了什么?
答案 0 :(得分:0)
这是代码
$filename = 'assets/others/129778.pdf';
$path = storage_path().DIRECTORY_SEPARATOR.$filename;
return Response::make($path, 200, [
'Content-Type' => 'application/pdf',
'Content-Disposition' => 'inline; '.$filename,
]);
看看它是否有效并且知道了吗?
答案 1 :(得分:0)
在BrochureController.php中
public function show($filename)
{
$file = base_path(). "/public/assets/others/brochure/". $filename;
$name = "Human Name For File.pdf";
$headers = ['Content-Type: application/pdf'];
return Response::download($file, $name, $headers);
}
现在在您的应用中访问http://example.com/brochure/jan-2016.pdf
并下载jan-2016.pdf文件。
如果您不希望用户能够猜到他们也可以访问http://example.com/assets/others/jan-2016.pdf
那么您可以将PDF移动到另一个目录(例如/resources/brochures
)可公开访问,然后您可以将方法的第一行更改为:
$file = base_path(). "/resources/brochures/". $filename;
这意味着每个人都必须转到http://example.com/brochure/jan-2016.pdf
才能访问该文件。
答案 2 :(得分:0)
你可以使用这个包[barryvdh / laravel-dompdf] [1]。并使用如下: 这只是示例snippset
class BrochureController extends Controller
{
public function show($filename)
{
$html = 'Your content in PDF';
$pdf = PDF::loadHTML($html);
//set the name of the pdf document
$file_name = 'jan-2016.pdf';
//check if the path exist or not
if(!File::exists(storage_path() . '/pdf')) {
$result = File::makeDirectory(storage_path() . '/pdf');
}
$pdf->save(storage_path() . '/pdf/' . $file_name);
//return $file_name;
return $pdf->download($file_name);
}
}
答案 3 :(得分:0)
考虑报价合同示例
控制器:
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Response;
class OfferController extends Controller
{
public function index()
{
$file = public_path().DIRECTORY_SEPARATOR.'media'.DIRECTORY_SEPARATOR.'offer.pdf';
$file = File::get($file);
$response = Response::make($file,200);
$response->header('Content-Type', 'application/pdf');
return $response;
}
}
路线:
Route::get('/offer','OfferController@index')->name('offer');
优点:
https://name.site/offer