public function JavaBridgeFillPdfBackend::populateWithFieldData in FillPDF 8.4
Populate a FillPDF form with field data.
Formerly known as merging. Accept an array of PDF field keys and field values and populate the PDF using them.
Parameters
\Drupal\fillpdf\FillPdfFormInterface $fillpdf_form: The FillPdfForm referencing the file whose field values are going to be populated.
array $field_mapping: An array of fields mapping PDF field keys to the values with which they should be replaced. Example array:
[
'values' => [
'Field 1' => 'value',
'Checkbox Field' => 'On',
],
'images' => [
'Image Field 1' => [
'data' => base64_encode($file_data),
'filenamehash' => md5($image_path_info['filename']) . '.' . $image_path_info['extension'],
],
],
];
array $context: The request context as returned by FillPdfLinkManipulatorInterface::parseLink().
Return value
string|null The raw file contents of the new PDF, or NULL if populating failed. The caller has to handle saving or serving the file accordingly.
Overrides FillPdfBackendPluginInterface::populateWithFieldData
File
- modules/
fillpdf_legacy/ src/ Plugin/ FillPdfBackend/ JavaBridgeFillPdfBackend.php, line 87
Class
- JavaBridgeFillPdfBackend
- Legacy JavaBridge FillPdfBackend plugin.
Namespace
Drupal\fillpdf_legacy\Plugin\FillPdfBackendCode
public function populateWithFieldData(FillPdfFormInterface $fillpdf_form, array $field_mapping, array $context) {
/** @var \Drupal\file\FileInterface $original_file */
$original_file = File::load($fillpdf_form->file->target_id);
$pdf_data = file_get_contents($original_file
->getFileUri());
$fields = $field_mapping['fields'];
$require = drupal_get_path('module', 'fillpdf') . '/lib/JavaBridge/java/Java.inc';
require_once DRUPAL_ROOT . '/' . $require;
try {
$fillpdf = new \java('com.ocdevel.FillpdfService', base64_encode($pdf_data), 'bytes');
foreach ($fields as $key => $field) {
if (substr($field, 0, 7) == '{image}') {
// Remove {image} marker.
$image_filepath = substr($field, 7);
$image_realpath = $this->fileSystem
->realpath($image_filepath);
$fillpdf
->image($key, $image_realpath, 'file');
}
else {
$fillpdf
->text($key, $field);
}
}
} catch (\JavaException $e) {
$this
->messenger()
->addError(java_truncate((string) $e));
return NULL;
}
try {
if ($context['flatten']) {
$populated_pdf = java_values(base64_decode($fillpdf
->toByteArray()));
}
else {
$populated_pdf = java_values(base64_decode($fillpdf
->toByteArrayUnflattened()));
}
} catch (\JavaException $e) {
$this
->messenger()
->addError(java_truncate((string) $e));
return NULL;
}
return $populated_pdf;
}