简体   繁体   中英

HTML2PDF - Download and display pdf file to page

I am using HTML2PDF with Laravel 5.1. I have a problem with showing the pdf file on the page and downloading it to the server.

When I use this code, it shows the pdf file without problems:

$pdf = $html2pdf->Output('', 'S'); 
return response($pdf)
    ->header('Content-Type', 'application/pdf')
    ->header('Content-Length', strlen($pdf))
    ->header('Content-Disposition', 'inline; filename="sample.pdf"');

However, the above code does not save the file to the server. So I tried this:

$filename = '\Report-' . $project->id . '.pdf';
$output_path = base_path() . '\public\reports' . $filename;
$pdf = $html2pdf->Output($output_path, 'F'); 
return response($pdf)
    ->header('Content-Type', 'application/pdf')
    ->header('Content-Length', strlen($pdf))
    ->header('Content-Disposition', 'inline; filename="'.$output_path.'"');

I've tried this in Chrome and in Firefox but it does not display the document, it just downloads the file to the server. What am I doing wrong?

You're probably actually wanting to do this:

$filename = '\Report-' . $project->id . '.pdf';
$output_path = base_path() . '\public\reports' . $filename;
$pdf = $html2pdf->Output($output_path, 'F'); 
return response(file_get_contents($output_path))
                ->header('Content-Type', 'application/pdf')
                ->header('Content-Length', strlen($pdf))
                ->header('Content-Disposition', 'inline; filename="'.$output_path.'"');

Or possibly:

$filename = '\Report-' . $project->id . '.pdf';
$output_path = base_path() . '\public\reports' . $filename;
$pdf = $html2pdf->Output($output_path, 'F'); 
return response($html2pdf->Output($output_path, 'S'))
                ->header('Content-Type', 'application/pdf')
                ->header('Content-Length', strlen($pdf))
                ->header('Content-Disposition', 'inline; filename="'.$filename.'"');

I can't tell from the documentation, but I don't believe Output with the 'F' option returns the file contents where 'S' does. So you'll just need to load the contents and return those instead.

Not at all familiar with laravel, but consider simply launching the outputted pdf as any URL link as modern day browsers render them as pages. Below assumes pdf is saved to server and is intended as the response object:

$filename = '\Report-' . $project->id . '.pdf';
$output_path = base_path() . '\public\reports' . $filename;
$pdf = $html2pdf->Output($output_path, 'F'); 
return response($output_path)
    ->header("Location: $output_path ");

I don't know if this is the best solution, but this works:

$filename = 'Report-' . $project->id . '.pdf';
$output_path = base_path() . '\public\reports\\' . $filename;
$pdf = $html2pdf->Output('', 'S');
$html2pdf->Output($output_path, 'F');
return response($pdf)
   ->header('Content-Type', 'application/pdf')
   ->header('Content-Length', strlen($pdf))
   ->header('Content-Disposition', 'inline; filename="'.$filename.'"');

I noticed that when $pdf = $html2pdf->Output('', 'S'); , the browser displays the file but does not download the file. However, if $pdf = $html2pdf->Output($output_path, 'F'); , the browser does not display the file, but downloads it nevertheless. So I realized that since I'm doing response($pdf) , I assigned $html2pdf->Output('', 'S'); to $pdf . And since I need to download the file, I just did $html2pdf->Output($output_path, 'F'); , without assigning this to $pdf .

Hope I explained this properly. I don't know if this has some loopholes or this is not good practice, but I'm going to stick to this for a while since I have not yet found another way for this to work.

Thank you for all those who answered.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM