core icon indicating copy to clipboard operation
core copied to clipboard

PDF custom format is not supported

Open abderahmaneBasil opened this issue 3 years ago • 0 comments

Hello everyone, I have difficulties implementing a custom format for PDF content. I am always receiving this error despite following api-platform doc : Serialization for the format \"pdf\" is not supported.

What I have at the moment is a DataOuputTransformer which transform my entity to a PDF so I get a Response with Content-type : application/pdf (using WeasyPrint).

I set a specific custom endpoint with an output_formats. The DTO just contain a string for the Url of the PDF.

'receipt' => [
            "method" => "GET",
            "status" => 200,
            "input" => false,
            "output" => DeliveryNote::class,
            "output_formats" => [
                "pdf" => ["application/pdf"]
            ],
            "path" => "/livraisons/{id}/receipt",
            "openapi_context" => [
                "summary" => "Export in PDF a delivery note.",
                "description" => "Export in PDF a delivery note."
            ]
        ]

I created a Serializer from doc example but it seems to not be used :/

# api/config/services.yaml
services:
    App\Serializer\DeliveryNoteNormalizer:
        arguments: [ '@api_platform.serializer.normalizer.item' ]
<?php
// api/src/Serializer/DeliveryNoteNormalizer.php

namespace App\Serializer;

use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

final class DeliveryNoteNormalizer implements NormalizerInterface, DenormalizerInterface
{
    private $normalizer;

    public function __construct(NormalizerInterface $normalizer)
    {
        if (!$normalizer instanceof DenormalizerInterface) {
            throw new \InvalidArgumentException('The normalizer must implement the DenormalizerInterface');
        }

        $this->normalizer = $normalizer;
    }

    public function denormalize($data, $class, $format = null, array $context = [])
    {
        return $this->normalizer->denormalize($data, $class, $format, $context);
    }

    public function supportsDenormalization($data, $type, $format = null)
    {
        return $this->normalizer->supportsDenormalization($data, $type, $format);
    }

    public function normalize($object, $format = null, array $context = [])
    {
        return $this->normalizer->normalize($object, $format, $context);
    }

    public function supportsNormalization($data, $format = null)
    {
        return $this->normalizer->supportsNormalization($data, $format);
    }
}

When the pdf format is registered globally in api_platform.yaml, swagger is giving the same error while loading : Serialization for the format \"pdf\" is not supported.

Do you have some idea what could be the problem and how to make it works ? Thank you !

abderahmaneBasil avatar Jul 12 '22 10:07 abderahmaneBasil