0

I am trying to download multiple pdf using dompdf in laravel, but below solution just concatenate into one pdf file

  $weeks = ['test','test2'];

  $html = '';
  foreach($weeks as $hours)
  {
      $view = view('contract');
      $html .= $view->render();
  }

  $pdf = PDF::loadHTML($html);            
  $sheet = $pdf->setPaper('a4', 'landscape');
  return $sheet->download('download.pdf');

2 Answers 2

1

the server can only return a response, if you want several pdf files you should modify your method so that it accepts some parameter and for each parameter generate a pdf

Sign up to request clarification or add additional context in comments.

1 Comment

return $sheet->download('download.pdf'); this download me pdf but i want download in each to download file
1

It is not possible to download more than one file in a single HTTP request.

What you could do is pack the files into o zip and download that. You can try https://github.com/zanysoft/laravel-zip or any other available solution.

Another option would be to save the files and return only paths for the saved files back to the client. Then make a new request and download the file for each of the returned filepaths.

Also if you want to save to separate PDF files you need to move PDF creation into your foreach loop (create a PDF for each parameter).

  $weeks = ['test','test2'];
  $files = [];

  foreach($weeks as $hours)
  {
      $view = view('contract');
      $html = $view->render();
      $pdf = PDF::loadHTML($html);            
      $pdf->setPaper('a4', 'landscape');
      $pdf->save('documents/'.$hours.'.pdf'); 
      $files[] = 'documents/'.$hours.'.pdf';
  }

  return response()->json(['files' => $files]);

In this case the foreach doesn't really do anything other than produce two identical PDF files. If you want to actually apply some kind of changes to your view based on values in $weeks you need to pass that to your view.

$view = view('contract', ['data' => $hours]);

That makes $data available in your view and you can change the resulting PDF in that way (show your contract.blade.php if you need more help regarding that).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.