0

I have this endpoint, which outputs (render in the browser) a PDF file:

    $pdf = PDF::loadView('print.property', $data, []); // DOMPDF

    $filename = 'test.pdf';
    if($show) {
        return $pdf->stream( $filename );
    }
    return $pdf->download($filename);

, where $show means render, but not download the PDF file.

For desktop everything works fine and the PDF file is rendered, but when I set with Chrome Dev Tools, the mobile simulator, the server doesn't response, but just stays in load mode.

I have tried with exit, without return with headers:

header("Content-Type: application/octet-stream");
header("Content-Disposition: inline; filename=\"$filename\"");

With Content-Disposition: attachment, it downloads the correct generated file. The problem is somewhere in the headers I guess. * I am using LiteServer.

This are some of the generated from the library response headers:

content-disposition: inline; filename="test.pdf"
content-type: application/pdf

I have tried and with this headers before: die ($pdf->stream( $filename));

header('Content-Length: 101840');
header('Content-Type: application/octet-stream');

or:

header('Content-Type: application/pdf');

or: header('Content-Disposition: inline; filename="'.$filename.'"'); or: header('Content-Disposition: attachment; filename="'.$filename.'"');

Nothing works. The closest that I ca get is downloading or render it as a string in the browser in Chrome (mobile).

gdfgdfg
  • 3,181
  • 7
  • 37
  • 83
  • 1
    I don't think you're supposed to modify the headers yourself. Do you get any errors in Laravel or your browser console? – Bram Verstraten Mar 02 '20 at 12:19
  • Nope, but I can set them if I use `die()`. – gdfgdfg Mar 02 '20 at 12:28
  • When you say "the server doesn't response, but just stays in load mode", do you mean that you don't receive a response, or that you get a blank page? – Bram Verstraten Mar 02 '20 at 12:44
  • The page doesn't render and the loader in the tab is there, but I have response headers, which is really strange. Also, if I click on Cancel to load the page (left to the URL) the loader within the tab is still there, like the page is still in load . – gdfgdfg Mar 02 '20 at 12:54

2 Answers2

1

personally I had problems with also. Now, I use "https://github.com/barryvdh/laravel-snappy" I do not know if on the v6 laravel it works.

Snoxik
  • 390
  • 4
  • 15
0

I also added this answer to another question Preview pdf instead of download using Barryvdh\DomPDF\Facade.

because it works for me, I tried more than 6 times to figure it out and finary I came up with this solution

use Illuminate\Support\Facades\Response;
use Illuminate\Support\Facades\Storage;
use Barryvdh\DomPDF\Facade\Pdf;  

public function PDFDownload($view, $data = [], $fileName = 'File.pdf',  $paperSize = 'A4', $orientation = 'potrait') {
  $pdf =  PDF::loadView($view, $data)->setPaper($paperSize, $orientation)->setWarnings(false);
  $filePath = storage_path('app/temp/'.$fileName);

  // Save the PDF to a temporary file on the server
  Storage::put('temp/'.$fileName, $pdf->output());

  $headers = [
    'Content-Type' => 'application/pdf',
    'Content-Disposition' => 'attachment; filename="'.$fileName.'"',
  ];

  // Get the PDF content as a string
  $content = file_get_contents($filePath);

  // Delete the temporary file
  Storage::delete('temp/'.$fileName);

  // Return the response with the PDF content and headers
  return response($content, 200, $headers);
}

Mobile devices should also be able to use this code. However, there are situations when mobile devices have security settings that forbid downloading files from the browser.

Tyler2P
  • 2,324
  • 26
  • 22
  • 31