You are here

public function InputHelper::attachPdfToForm in FillPDF 5.0.x

Same name and namespace in other branches
  1. 8.4 src/InputHelper.php \Drupal\fillpdf\InputHelper::attachPdfToForm()

Attaches a PDF template file to a FillPdfForm.

Parameters

\Drupal\file\FileInterface $file: The PDF template file to attach.

\Drupal\fillpdf\FillPdfFormInterface $existing_form: The FillPdfForm the PDF template file should be attached to.

Return value

array Associative array with the following keys:

  • 'form': The updated FillPdfForm entity.
  • 'fields': Associative array of the FillPdfForm entity's saved FillPdfFormFields.

Overrides InputHelperInterface::attachPdfToForm

File

src/InputHelper.php, line 49

Class

InputHelper
Class InputHelper.

Namespace

Drupal\fillpdf

Code

public function attachPdfToForm(FileInterface $file, FillPdfFormInterface $existing_form = NULL) {

  // Save the file so we can get an fid.
  $file
    ->setPermanent();
  $file
    ->save();
  if ($existing_form) {
    $fillpdf_form = $existing_form;
    $fillpdf_form->file = $file;
  }
  else {
    $fillpdf_form = FillPdfForm::create([
      'file' => $file,
      'title' => $file->filename,
    ]);
  }

  // Save PDF configuration before parsing.
  $fillpdf_form
    ->save();

  // Parse and save fields.
  $form_fields = $this
    ->parseFields($fillpdf_form);
  foreach ($form_fields as $field) {
    $field
      ->save();
  }
  return [
    'form' => $fillpdf_form,
    'fields' => $form_fields,
  ];
}