0

我的代码不保存 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");
       
    }

}

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

4

1 回答 1

0

我相信您可以使用 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->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-02-12T11:43:43.240 回答