FillPdfServicePdfBackend.php in FillPDF 8.4
File
src/Plugin/PdfBackend/FillPdfServicePdfBackend.php
View source
<?php
namespace Drupal\fillpdf\Plugin\PdfBackend;
use Drupal\file\Entity\File;
use Drupal\file\FileInterface;
use Drupal\fillpdf\FieldMapping\ImageFieldMapping;
use Drupal\fillpdf\FieldMapping\TextFieldMapping;
use Drupal\fillpdf\FillPdfBackendPluginInterface;
use Drupal\fillpdf\FillPdfFormInterface;
use Drupal\fillpdf\Plugin\PdfBackendBase;
class FillPdfServicePdfBackend extends PdfBackendBase implements FillPdfBackendPluginInterface {
public function parse(FillPdfFormInterface $fillpdf_form) {
$template_file = File::load($fillpdf_form->file->target_id);
return $this
->parseFile($template_file);
}
public function parseFile(FileInterface $template_file) {
$pdf_content = file_get_contents($template_file
->getFileUri());
return $this
->parseStream($pdf_content);
}
public function parseStream($pdf_content) {
$result = $this
->xmlRpcRequest('parse_pdf_fields', base64_encode($pdf_content));
if ($result->error == TRUE) {
return [];
}
$fields = $result->data;
return $fields;
}
protected function xmlRpcRequest($method) {
$url = $this->configuration['remote_protocol'] . '://' . $this->configuration['remote_endpoint'];
$args = func_get_args();
$args = [
$args[0] => array_slice($args, 1),
];
$result = xmlrpc($url, $args);
$ret = new \stdClass();
if (isset($result['error'])) {
$this
->messenger()
->addError($result['error']);
$ret->error = TRUE;
}
elseif ($result == FALSE || xmlrpc_error()) {
$error = xmlrpc_error();
$ret->error = TRUE;
$this
->messenger()
->addError($this
->t('There was a problem contacting the FillPDF service.
It may be down, or you may not have internet access. [ERROR @code: @message]', [
'@code' => $error->code,
'@message' => $error->message,
]));
}
else {
$ret->data = $result['data'];
$ret->error = FALSE;
}
return $ret;
}
public function populateWithFieldData(FillPdfFormInterface $fillpdf_form, array $field_mapping, array $context) {
$template_file = File::load($fillpdf_form->file->target_id);
return $this
->mergeFile($template_file, $field_mapping, $context);
}
public function mergeFile(FileInterface $template_file, array $field_mappings, array $context) {
$pdf_content = file_get_contents($template_file
->getFileUri());
return $this
->mergeStream($pdf_content, $field_mappings, $context);
}
public function mergeStream($pdf_content, array $field_mappings, array $context) {
$api_key = $this->configuration['fillpdf_service_api_key'];
$fields = $images = [];
foreach ($field_mappings as $pdf_key => $mapping) {
if ($mapping instanceof TextFieldMapping) {
$fields[$pdf_key] = (string) $mapping;
}
elseif ($mapping instanceof ImageFieldMapping) {
$field_path_info = pathinfo($mapping
->getUri());
$fields[$pdf_key] = '{image}' . md5($field_path_info['filename']) . '.' . $field_path_info['extension'];
$images[$pdf_key] = [
'data' => base64_encode($mapping
->getData()),
'filenamehash' => md5($field_path_info['filename']) . '.' . $field_path_info['extension'],
];
}
}
$result = $this
->xmlRpcRequest('merge_pdf_v3', base64_encode($pdf_content), $fields, $api_key, $context['flatten'], $images);
if ($result->error === FALSE && $result->data) {
$populated_pdf = base64_decode($result->data);
return $populated_pdf;
}
}
}