JavaBridgeFillPdfBackend.php in FillPDF 8.4
File
modules/fillpdf_legacy/src/Plugin/FillPdfBackend/JavaBridgeFillPdfBackend.php
View source
<?php
namespace Drupal\fillpdf_legacy\Plugin\FillPdfBackend;
use Drupal\Core\File\FileSystem;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Plugin\PluginBase;
use Drupal\file\Entity\File;
use Drupal\fillpdf\FillPdfBackendPluginInterface;
use Drupal\fillpdf\FillPdfFormInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class JavaBridgeFillPdfBackend extends PluginBase implements FillPdfBackendPluginInterface, ContainerFactoryPluginInterface {
protected $fileSystem;
public function __construct(array $configuration, $plugin_id, array $plugin_definition, FileSystem $file_system) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->fileSystem = $file_system;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('file_system'));
}
public function parse(FillPdfFormInterface $fillpdf_form) {
$file = File::load($fillpdf_form->file->target_id);
$content = file_get_contents($file
->getFileUri());
$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($content), 'bytes');
$fields = java_values($fillpdf
->parse());
} catch (\JavaException $e) {
$this
->messenger()
->addError(java_truncate((string) $e));
}
return $fields;
}
public function populateWithFieldData(FillPdfFormInterface $fillpdf_form, array $field_mapping, array $context) {
$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}') {
$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;
}
}