class FillPdfServicePdfBackend in FillPDF 5.0.x
Same name and namespace in other branches
- 8.4 src/Plugin/PdfBackend/FillPdfServicePdfBackend.php \Drupal\fillpdf\Plugin\PdfBackend\FillPdfServicePdfBackend
FillPDF Service PdfBackend plugin.
Plugin annotation
@PdfBackend(
id = "fillpdf_service",
label = @Translation("FillPDF Service"),
description = @Translation(
"No technical prerequisites.
Sign up for <a href=':url'>FillPDF Service</a>.",
arguments = {
":url" = "https://fillpdf.io"
}
),
weight = -10
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\fillpdf\Plugin\PdfBackendBase implements PdfBackendInterface
- class \Drupal\fillpdf\Plugin\PdfBackend\FillPdfServicePdfBackend
- class \Drupal\fillpdf\Plugin\PdfBackendBase implements PdfBackendInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of FillPdfServicePdfBackend
File
- src/
Plugin/ PdfBackend/ FillPdfServicePdfBackend.php, line 29
Namespace
Drupal\fillpdf\Plugin\PdfBackendView source
class FillPdfServicePdfBackend extends PdfBackendBase {
/**
* {@inheritdoc}
*/
public function parseFile(FileInterface $template_file) {
$pdf_content = file_get_contents($template_file
->getFileUri());
return $this
->parseStream($pdf_content);
}
/**
* {@inheritdoc}
*/
public function parseStream($pdf_content) {
$result = $this
->xmlRpcRequest('parse_pdf_fields', base64_encode($pdf_content));
if ($result->error == TRUE) {
// @todo: Throw an exception, log a message etc.
return [];
}
// after setting error message
$fields = $result->data;
return $fields;
}
/**
* Make an XML-RPC request.
*
* @param string $method
* The method to call. Additional arguments are the paramters to the
* xmlrpc() call.
*
* @return object
* Object with properties 'error' and 'data' representing the result of the
* request.
*/
protected function xmlRpcRequest($method) {
$url = $this->configuration['remote_protocol'] . '://' . $this->configuration['remote_endpoint'];
$args = func_get_args();
// Fix up the array for Drupal 7 xmlrpc() function style.
$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;
}
/**
* {@inheritdoc}
*/
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);
}
/**
* {@inheritdoc}
*/
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) {
// Anonymize image data from the fields array; we should not send the
// real filename to FillPDF Service. We do this in the specific backend
// because other plugin types may need the filename on the local system.
$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;
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
public | function | 2 | |
DependencySerializationTrait:: |
public | function | 2 | |
FillPdfServicePdfBackend:: |
public | function |
Populate a PDF file with field data. Overrides PdfBackendInterface:: |
|
FillPdfServicePdfBackend:: |
public | function |
Populate a PDF file with field data. Overrides PdfBackendInterface:: |
|
FillPdfServicePdfBackend:: |
public | function |
Parse a PDF and return a list of its fields. Overrides PdfBackendInterface:: |
|
FillPdfServicePdfBackend:: |
public | function |
Parse a PDF and return a list of its fields. Overrides PdfBackendInterface:: |
|
FillPdfServicePdfBackend:: |
protected | function | Make an XML-RPC request. | |
MessengerTrait:: |
protected | property | The messenger. | 27 |
MessengerTrait:: |
public | function | Gets the messenger. | 27 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
2 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
PluginBase:: |
public | function | Constructs a \Drupal\Component\Plugin\PluginBase object. | 98 |
StringTranslationTrait:: |
protected | property | The string translation service. | 4 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |