I have a bunch of pdf reports I want to download all at once, right now there are as many as 150, I have a method for downloading a single pdf and that works ok, it's really slow to load. I want to know if there is a way I can download all of the reports at once.
This is the single download pdf function, this pdf report is for a survey results, so all the questions and a % of answered options.
public function pdf($id)
{
$survey = Survey::with('surveyQuestions.responseType',
'surveyQuestions.surveyQuestionOption', 'surveyQuestions.answer')
->where('id', $id)->first();
$Eid = Evaluation::where('survey_id', $id)->pluck('id')->first();
$evaluators = EvaluationEvaluator::where('evaluation_id', $Eid)->with('group.users')->get();
if ($evaluators[0]->user_id == null) {
$tot = $evaluators[0]['group']['users']->count();
} else {
$tot = $evaluators->count();
}
$totalAnswered = SurveyAnswer::where('survey_id', $survey->id)->selectRaw('COUNT(DISTINCT user_id) as count')
->value('count');
$result = [];
foreach ($survey['surveyQuestions'] as $question) {
$options = [];
if ($question->response_type_id === 1) {
foreach ($survey['answer'] as $answer) {
if ($question['pivot']->survey_id === $survey->id) {
if ($answer->survey_question_id === $question->id) {
$options[$answer->id] = [
'title' => $answer->answer,
'vote_count' => null
];
}
}
}
}
if ($question->response_type_id === 2) {
$mul_total = SurveyAnswer::where('survey_id', $survey->id)->where('survey_question_id', $question->id)
->selectRaw('COUNT(DISTINCT user_id) as count')
->value('count');
foreach ($question['surveyQuestionOption'] as $option) {
if ($question['pivot']->survey_id === $survey->id) {
$mul_count = 0;
foreach ($survey['answer'] as $answer) {
if ($answer->survey_question_id === $option->survey_question_id
&& (int)$answer->answer === $option->id) {
$mul_count++;
}
$options[$option->id] = [
'title' => $option->option,
'vote_count' => $mul_count,
'total' => $mul_total
];
}
}
}
}
if ($question->response_type_id === 3) {
if ($question->rank === 3) {
$question->opciones = [
['id' => 1, 'option' => 1],
['id' => 2, 'option' => 2],
['id' => 3, 'option' => 3]
];
} else if ($question->rank === 4) {
$question->opciones = [
['id' => 1, 'option' => 1],
['id' => 2, 'option' => 2],
['id' => 3, 'option' => 3],
['id' => 4, 'option' => 4]
];
} else if ($question->rank === 5) {
$question->opciones = [
['id' => 1, 'option' => 1],
['id' => 2, 'option' => 2],
['id' => 3, 'option' => 3],
['id' => 4, 'option' => 4],
['id' => 5, 'option' => 5]
];
} else {
$question->opciones = [
['id' => 1, 'option' => 1],
['id' => 2, 'option' => 2],
['id' => 3, 'option' => 3],
['id' => 4, 'option' => 4],
['id' => 5, 'option' => 5],
['id' => 6, 'option' => 6]
];
}
$rk_total = SurveyAnswer::where('survey_id', $survey->id)->where('survey_question_id', $question->id)
->selectRaw('COUNT(DISTINCT user_id) as count')
->value('count');
foreach ($question['opciones'] as $opt) {
$rk_count = 0;
foreach ($question['answer'] as $answer) {
if ($answer->survey_id === $survey->id) {
if ($answer->survey_question_id === $question->id && (int)$answer->answer === $opt['option']) {
$rk_count++;
}
$options[$opt['id']] = [
'title' => $opt['option'],
'vote_count' => $rk_count,
'total' => $rk_total
];
}
}
}
}
if ($question->response_type_id === 4) {
$question->opciones = [
['id' => 1, 'option' => 'Si'],
['id' => 2, 'option' => 'No']
];
$total = SurveyAnswer::where('survey_id', $survey->id)->where('survey_question_id', $question->id)
->selectRaw('COUNT(DISTINCT user_id) as count')
->value('count');
foreach ($question['opciones'] as $opt) {
$count = 0;
foreach ($question['answer'] as $answer) {
if ($answer->survey_id === $survey->id) {
if ($answer->survey_question_id === $question->id && $answer->answer === $opt['option']) {
$count++;
}
$options[$opt['id']] = [
'title' => $opt['option'],
'vote_count' => $count,
'total' => $total
];
}
}
}
}
$result[] = [
'id' => $question->id,
'type' => $question->response_type_id,
'rank' => $question->rank,
'num' => $question->num,
'title' => $question->question,
'options' => $options
];
}
$data = Survey::with('surveyQuestions.responseType')->where('id', $id)->first();
$data['result'] = $result;
$data['totalSent'] = $tot;
$data['totalAnswered'] = $totalAnswered;
$pdf = App::make('dompdf.wrapper');
$pdf->loadView('pdf.report', ['survey' => $data]);
return $pdf->stream();
}
This is what I tried for the downloadAll function, however understandably this give me a timeout error, a single pdf takes about 15-20 seconds to load which is incredibly slow.
public function downloadAllReports()
{
$now = Carbon::now();
$report = Evaluation::with('survey')
->whereDate('report_date', '<=', $now)
->whereDate('report_end', '>=', $now)
->where('report', true)
->whereHas('survey', function ($query) {
$query->where('user_id', auth()->user()->id);
})
->orderBy('updated_at', 'desc')
->get();
foreach ($report as $item) {
$this->pdf($item->id);
}
}
What is my best option here to download all the reports, also if there is a way to make the pdf load faster that would be great.