【问题标题】:view pdf stored on public/storage on laravel?查看存储在laravel公共/存储中的pdf?
【发布时间】:2021-02-12 11:31:37
【问题描述】:

我的代码没有保存 pdf,我需要显示它。如果可以的话也下载吧。

<td><a name="Manual" href="{{asset('storage/app').'/'.$machineData->getManual()}}">
                Manual de {{$machineData->getMachineName()}}</a></td>

在控制器上我有这个

protected function downloadFile($src)
{
    if(is_file($src))
    {
        $finfo=finfo_open(FILEINFO_MIME_TYPE);
        $content_type=finfo_file($finfo,$src);
        finfo_open($finfo);
        $file_name=basename($src).PHP_EOL;
        $size=filesize($src);
        header("Content-Type: $content_type");
        header("Content-Disposition: attachment; filename=$file_name");
        header("Content-Transfer-Encoding: binary");
        header("Content-Length: $size");
       
        return true;
    }
    else
    {
        return false;
    }
}
public function download(Request $request)
{
    $note=$request->file('manual');
    if(!$this->downloadFile(app_path(). $note))
    {
        return redirect("/machineIndex");
       
    }

}

请帮忙,我只是想显示文件,谢谢!!

【问题讨论】:

    标签: php laravel pdf download


    【解决方案1】:

    我相信您可以使用 TCPDF 实现这一目标

            $pdf = new TCPDF('P', 'in', [$sizeX, $sizeY], true, 'UTF-8', false);
    
            $pdf->SetPrintHeader(false);
            $pdf->SetPrintFooter(false);
            $pdf->AddPage();
            $pdf->SetMargins(0, 0, 0, true);
            $pdf->SetAutoPageBreak(false, 0);
    
            $pdf->Output($outputPath, 'I');
    

    此代码将创建一个新的 pdf 文档,然后查看 https://tcpdf.org/ 以进行必要的更改,最后的 $pdf-&gt;Output($outputPath, 'I'); 将打开 pdf 而不将其保存到存储中(几乎就像 dd)。


    编辑:

    我写这篇文章的假设是您不想将文件保存到存储中,如果文件已经保存在 public/storage 中,您可以向它提供一个直接 url。 您需要带有php artisan storage:link 的符号链接,然后引导它在目录中的位置。例如'laravel.test/public/pdfs/pdf1.pdf'

    更多信息https://laravel.com/docs/8.x/filesystem

    【讨论】:

      猜你喜欢
      • 2021-09-08
      • 1970-01-01
      • 2017-10-03
      • 2018-08-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多