/**
  * Execute the job.
  *
  * @return void
  */
 public function handle()
 {
     foreach ($this->responses as $response) {
         $path_to_blank = $this->getBlankPath($response->title);
         $renderer = $this->getViewName($response->title);
         if (file_exists($path_to_blank)) {
             if (view()->exists($renderer)) {
                 $pdf = new Pdf($path_to_blank);
                 $existing_fields = $pdf->getDataFields();
                 $pdf = new Pdf($path_to_blank);
                 $pdf->setFields($existing_fields);
                 $pdf->setId($response->formid);
                 $rendered = view($renderer)->with('pdf', $pdf)->with('responses', new Response($response->responses))->with('student', $this->student)->with('event', $this)->render();
                 $rendered = json_decode($rendered);
                 $path_to_filled = $this->getFilledPath($response->title);
                 if (!empty($rendered)) {
                     foreach ($rendered as $index => $pdfFile) {
                         if ($index == 0) {
                             $pdf = new $pdf($pdfFile);
                         } else {
                             $pdf->addFile($pdfFile);
                         }
                     }
                     $pdf->saveAs($path_to_filled);
                 } else {
                     $pdf->fillForm($pdf->fields())->flatten()->needAppearances()->saveAs($path_to_filled);
                     if (empty($pdf->getError())) {
                         if ($this->watermarkOption == 'draft') {
                             $pdf = new Pdf($path_to_filled);
                             $pdf->stamp(config('iep.draft_watermark'))->saveAs($path_to_filled);
                         } else {
                             if ($this->watermarkOption == 'copy') {
                                 $pdf = new Pdf($path_to_filled);
                                 $pdf->stamp(config('iep.copy_watermark'))->saveAs($path_to_filled);
                             }
                         }
                     }
                 }
                 if (empty($pdf->getError())) {
                     $files[] = $path_to_filled;
                 } else {
                     $error[$pdf->getId()] = $pdf->getError();
                 }
             } else {
                 $error[$response->formid] = 'There is no renderer for this form.';
             }
         } else {
             $error[$response->formid] = 'There is no pdf file for this form.';
         }
     }
     $downloadFile = '';
     if (isset($files)) {
         $this->concatName .= '-' . count($files) . '-forms';
         $downloadFile = event(new PdfWasFilled($files, $this->concatName, $this->fileOption))[0];
     }
     return ['file' => $downloadFile, 'error' => isset($error) ? $error : []];
 }
 /**
  * concat all pdfs into one
  *
  * @param array $files
  */
 protected function concatPdfs($files)
 {
     foreach ($files as $index => $file) {
         if ($index == 0) {
             $pdf = new Pdf($file);
         } else {
             $pdf->addFile($file);
         }
     }
     $pdf->saveAs($this->outFile);
 }