Skip to content

Commit

Permalink
add merge endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean-Beru committed Nov 18, 2024
1 parent 0a09396 commit 19a57bd
Show file tree
Hide file tree
Showing 17 changed files with 399 additions and 53 deletions.
21 changes: 21 additions & 0 deletions config/builder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

use Sensiolabs\GotenbergBundle\Builder\AbstractBuilder;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use function Symfony\Component\DependencyInjection\Loader\Configurator\service;

return static function (ContainerConfigurator $container): void {
$services = $container->services();

$services->defaults()
->tag('monolog.logger', ['channel' => 'sensiolabs_gotenberg'])
;

$services->set('.sensiolabs_gotenberg.abstract_builder', AbstractBuilder::class)
->abstract()
->args([
service('sensiolabs_gotenberg.client'),
service('sensiolabs_gotenberg.asset.base_dir_formatter'),
])
;
};
43 changes: 20 additions & 23 deletions config/builder_pdf.php
Original file line number Diff line number Diff line change
@@ -1,42 +1,43 @@
<?php

// use Sensiolabs\GotenbergBundle\Builder\Pdf\ConvertPdfBuilder;
use Sensiolabs\GotenbergBundle\Builder\AbstractBuilder;
use Sensiolabs\GotenbergBundle\Builder\HtmlPdfBuilder;
// use Sensiolabs\GotenbergBundle\Builder\Pdf\LibreOfficePdfBuilder;
// use Sensiolabs\GotenbergBundle\Builder\Pdf\MarkdownPdfBuilder;
// use Sensiolabs\GotenbergBundle\Builder\Pdf\MergePdfBuilder;
// use Sensiolabs\GotenbergBundle\Builder\Pdf\UrlPdfBuilder;
use Sensiolabs\GotenbergBundle\Builder\MergePdfBuilder;
use Sensiolabs\GotenbergBundle\Configurator\HtmlPdfBuilderConfigurator;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use function Symfony\Component\DependencyInjection\Loader\Configurator\abstract_arg;
use function Symfony\Component\DependencyInjection\Loader\Configurator\service;
use function Symfony\Component\DependencyInjection\Loader\Configurator\service_locator;

// use Sensiolabs\GotenbergBundle\Builder\Pdf\LibreOfficePdfBuilder;
// use Sensiolabs\GotenbergBundle\Builder\Pdf\MarkdownPdfBuilder;
// use Sensiolabs\GotenbergBundle\Builder\Pdf\MergePdfBuilder;
// use Sensiolabs\GotenbergBundle\Builder\Pdf\UrlPdfBuilder;

return static function (ContainerConfigurator $container): void {
$services = $container->services();

$services->defaults()
->tag('monolog.logger', ['channel' => 'sensiolabs_gotenberg'])
;

$services->set('.sensiolabs_gotenberg.abstract_builder', AbstractBuilder::class)
->abstract()
// PDF - HTML
$services->set('.sensiolabs_gotenberg.builder_configurator.html', HtmlPdfBuilderConfigurator::class)
->share(false)
->args([
service('sensiolabs_gotenberg.client'),
service('sensiolabs_gotenberg.asset.base_dir_formatter'),
abstract_arg('default body configuration'),
abstract_arg('default headers configuration'),
abstract_arg('default configuration'),
])
;

$services->set('.sensiolabs_gotenberg.pdf_builder.html', HtmlPdfBuilder::class)
->share(false)
->parent('.sensiolabs_gotenberg.abstract_builder')
->arg(4, service_locator([
->arg(2, service_locator([
'logger' => service('logger')->nullOnInvalid(),
'twig' => service('twig')->nullOnInvalid(),
'router' => service('router')->nullOnInvalid(),
]))
->configurator(service('.sensiolabs_gotenberg.builder_configurator.html'))
->tag('sensiolabs_gotenberg.pdf_builder')
;

Expand Down Expand Up @@ -87,17 +88,13 @@
// ->call('setLogger', [service('logger')->nullOnInvalid()])
// ->tag('sensiolabs_gotenberg.pdf_builder')
// ;
//
// $services->set('.sensiolabs_gotenberg.pdf_builder.merge', MergePdfBuilder::class)
// ->share(false)
// ->args([
// service('sensiolabs_gotenberg.client'),
// service('sensiolabs_gotenberg.asset.base_dir_formatter'),
// ])
// ->call('setLogger', [service('logger')->nullOnInvalid()])
// ->tag('sensiolabs_gotenberg.pdf_builder')
// ;
//

$services->set('.sensiolabs_gotenberg.pdf_builder.merge', MergePdfBuilder::class)
->share(false)
->parent('.sensiolabs_gotenberg.abstract_builder')
->tag('sensiolabs_gotenberg.pdf_builder')
;

// $services->set('.sensiolabs_gotenberg.pdf_builder.convert', ConvertPdfBuilder::class)
// ->share(false)
// ->args([
Expand Down
14 changes: 3 additions & 11 deletions src/Builder/AbstractBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Sensiolabs\GotenbergBundle\Builder;

use Psr\Container\ContainerInterface;
use Sensiolabs\GotenbergBundle\Builder\Behaviors\Dependencies\AssetBaseDirFormatterAwareTrait;
use Sensiolabs\GotenbergBundle\Builder\Result\GotenbergAsyncResult;
use Sensiolabs\GotenbergBundle\Builder\Result\GotenbergFileResult;
use Sensiolabs\GotenbergBundle\Client\BodyBag;
Expand All @@ -23,7 +22,6 @@
*/
abstract class AbstractBuilder implements BuilderAsyncInterface, BuilderFileInterface
{

private readonly BodyBag $bodyBag;
private readonly HeadersBag $headersBag;

Expand All @@ -32,19 +30,13 @@ abstract class AbstractBuilder implements BuilderAsyncInterface, BuilderFileInte
/** @var ProcessorInterface<mixed>|null */
private ProcessorInterface|null $processor;

/**
* @param array<string, mixed> $defaultBodyData
*/
public function __construct(
protected readonly GotenbergClientInterface $client,
protected readonly AssetBaseDirFormatter $asset,
array $defaultBodyData = [],
array $defaultHeadersData = [],
protected readonly ContainerInterface $dependencies = new Container(),
)
{
$bodyOptionsResolver = (new OptionsResolver())->setDefaults($defaultBodyData);
$headersOptionsResolver = (new OptionsResolver())->setRequired($defaultHeadersData);
) {
$bodyOptionsResolver = new OptionsResolver();
$headersOptionsResolver = new OptionsResolver();

$this->configure($bodyOptionsResolver, $headersOptionsResolver);

Expand Down
10 changes: 10 additions & 0 deletions src/Builder/Behaviors/Chromium/PagePropertiesTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,14 @@ public function singlePage(bool $singlePage = true): static

return $this;
}

/**
* Sets the paper orientation to landscape.
*/
public function landscape(bool $bool = true): self
{
$this->getBodyBag()->set('landscape', $bool);

return $this;
}
}
4 changes: 4 additions & 0 deletions src/Builder/Behaviors/ChromiumTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@ trait ChromiumTrait
use Chromium\HeaderFooterTrait { Chromium\HeaderFooterTrait::configure as configureHeaderFooter; }
use Chromium\PagePropertiesTrait { Chromium\PagePropertiesTrait::configure as configurePageProperties; }
use Chromium\WaitBeforeRenderingTrait { Chromium\WaitBeforeRenderingTrait::configure as configureWaitBeforeRendering; }
use MetadataTrait { MetadataTrait::configure as configureMetadata; }
use PdfFormatTrait { PdfFormatTrait::configure as configurePdfFormat; }

protected function configure(OptionsResolver $bodyOptionsResolver, OptionsResolver $headersOptionsResolver): void
{
$this->configureHeaderFooter($bodyOptionsResolver, $headersOptionsResolver);
$this->configurePageProperties($bodyOptionsResolver, $headersOptionsResolver);
$this->configureWaitBeforeRendering($bodyOptionsResolver, $headersOptionsResolver);
$this->configureMetadata($bodyOptionsResolver, $headersOptionsResolver);
$this->configurePdfFormat($bodyOptionsResolver, $headersOptionsResolver);
}
}
25 changes: 25 additions & 0 deletions src/Builder/Behaviors/LibreOffice/PagePropertiesTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,31 @@

namespace Sensiolabs\GotenbergBundle\Builder\Behaviors\LibreOffice;

use Sensiolabs\GotenbergBundle\Client\BodyBag;
use Symfony\Component\OptionsResolver\OptionsResolver;

trait PagePropertiesTrait
{
abstract protected function getBodyBag(): BodyBag;

protected function configure(OptionsResolver $bodyOptionsResolver, OptionsResolver $headersOptionsResolver): void
{
$bodyOptionsResolver
->define('landscape')
->info('Set the paper orientation to landscape.')
->allowedTypes('bool')
;

// @TODO
}

/**
* Sets the paper orientation to landscape.
*/
public function landscape(bool $bool = true): self
{
$this->getBodyBag()->set('landscape', $bool);

return $this;
}
}
13 changes: 12 additions & 1 deletion src/Builder/Behaviors/LibreOfficeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,18 @@

namespace Sensiolabs\GotenbergBundle\Builder\Behaviors;

use Symfony\Component\OptionsResolver\OptionsResolver;

trait LibreOfficeTrait
{
use LibreOffice\PagePropertiesTrait;
use LibreOffice\PagePropertiesTrait { LibreOffice\PagePropertiesTrait::configure as configurePageProperties; }
use MetadataTrait { MetadataTrait::configure as configureMetadata; }
use PdfFormatTrait { PdfFormatTrait::configure as configurePdfFormat; }

protected function configure(OptionsResolver $bodyOptionsResolver, OptionsResolver $headersOptionsResolver): void
{
$this->configurePageProperties($bodyOptionsResolver, $headersOptionsResolver);
$this->configureMetadata($bodyOptionsResolver, $headersOptionsResolver);
$this->configurePdfFormat($bodyOptionsResolver, $headersOptionsResolver);
}
}
60 changes: 60 additions & 0 deletions src/Builder/Behaviors/MetadataTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace Sensiolabs\GotenbergBundle\Builder\Behaviors;

use Sensiolabs\GotenbergBundle\Client\BodyBag;
use Sensiolabs\GotenbergBundle\Enumeration\PdfFormat;
use Sensiolabs\GotenbergBundle\Exception\JsonEncodingException;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
* @see https://gotenberg.dev/docs/routes#pdfa-chromium
*/
trait MetadataTrait
{
abstract protected function getBodyBag(): BodyBag;

protected function configure(OptionsResolver $bodyOptionsResolver, OptionsResolver $headersOptionsResolver): void
{
$bodyOptionsResolver
->define('metadata')
->info('Add metadata.')
->normalize(function (Options $options, mixed $value): string {
try {
return json_encode($value, \JSON_THROW_ON_ERROR);
} catch (\JsonException $exception) {
throw new JsonEncodingException(\sprintf('Could not encode property "%s" into JSON', $key), previous: $exception);
}
})
;
}

/**
* Resets the metadata.
*
* @see https://gotenberg.dev/docs/routes#metadata-chromium
* @see https://gotenberg.dev/docs/routes#metadata-libreoffice
* @see https://gotenberg.dev/docs/routes#write-pdf-metadata-route
* @see https://gotenberg.dev/docs/routes#merge-pdfs-route
* @see https://exiftool.org/TagNames/XMP.html#pdf
*
* @param array<string, mixed> $metadata
*/
public function metadata(array $metadata): static
{
$this->getBodyBag()->set('metadata', $metadata);

return $this;
}

/**
* The metadata to write.
*/
public function addMetadata(string $key, string $value): static
{
$this->getBodyBag()->set('metadata', [$key => $value] + $this->getBodyBag()->get('metadata', []));

return $this;
}
}
51 changes: 51 additions & 0 deletions src/Builder/Behaviors/PdfFormatTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Sensiolabs\GotenbergBundle\Builder\Behaviors;

use Sensiolabs\GotenbergBundle\Client\BodyBag;
use Sensiolabs\GotenbergBundle\Enumeration\PdfFormat;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
* @see https://gotenberg.dev/docs/routes#pdfa-chromium
*/
trait PdfFormatTrait
{
abstract protected function getBodyBag(): BodyBag;

protected function configure(OptionsResolver $bodyOptionsResolver, OptionsResolver $headersOptionsResolver): void
{
$bodyOptionsResolver
->define('pdfa')
->info('Convert the resulting PDF into the given PDF/A format.')
->allowedValues(
...array_map(fn (PdfFormat $p): string => $p->value, PdfFormat::cases()),
)
;
$bodyOptionsResolver
->define('pdfua')
->info('Enable PDF for Universal Access for optimal accessibility.')
->allowedTypes('bool')
;
}

/**
* Convert the resulting PDF into the given PDF/A format.
*/
public function pdfFormat(PdfFormat $format): self
{
$this->getBodyBag()->set('pdfa', $format->value);

return $this;
}

/**
* Enable PDF for Universal Access for optimal accessibility.
*/
public function pdfUniversalAccess(bool $bool = true): self
{
$this->getBodyBag()->set('pdfua', $bool);

return $this;
}
}
12 changes: 6 additions & 6 deletions src/Builder/HtmlPdfBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,17 @@ class HtmlPdfBuilder extends AbstractBuilder
use ChromiumTrait { ChromiumTrait::configure as configureChromium; }
use WebhookTrait { WebhookTrait::configure as configureWebhook; }

protected function getEndpoint(): string
{
return '/forms/chromium/convert/html';
}

protected function configure(OptionsResolver $bodyOptionsResolver, OptionsResolver $headersOptionsResolver): void
{
parent::configure($bodyOptionsResolver, $headersOptionsResolver);
$this->configureChromium($bodyOptionsResolver, $headersOptionsResolver);
$this->configureWebhook($bodyOptionsResolver, $headersOptionsResolver);
}
}

/**
* @param string $template #Template
Expand All @@ -35,9 +40,4 @@ public function contentFile(string $path): self
{
return $this->withFilePart(Part::Body, $path);
}

protected function getEndpoint(): string
{
return '/forms/chromium/convert/html';
}
}
Loading

0 comments on commit 19a57bd

Please sign in to comment.