4

I am using Django 2.x and DRF.

From my APIView, I want to return pdf or eps binary data file.

class DownloadFile(APIView):
    serializer_class = DownloadFileSerializer
    renderer_classes = (BinaryFileRenderer,)

    def post(self, request):
        serializer = self.serializer_class(data=request.data)
        serializer.is_valid(raise_exception=True)

        name = serializer.validated_data.get('name')
        data = serializer.validated_data.get('imgdata')

        mimetype = None

        data, mimetype = convert_file_to_pdf_or_eps(data)

        if data and mimetype:

            response = Response(data=data, content_type=mimetype)
            ascii_name = get_ascii_name(name, "QRCode")

            response['Content-Disposition'] = 'attachment; filename=%s' % "-".join(filename.split())

            return response
        else:
            return Response(status=status.HTTP_400_BAD_REQUEST, data='Either data or mime type was missing')

and the BinaryFileRenderer

from rest_framework.renderers import BaseRenderer


class BinaryFileRenderer(BaseRenderer):
    media_type = 'application/octet-stream'
    format = None
    charset = None
    render_style = 'binary'

    def render(self, data, media_type=None, renderer_context=None):
        return data

This works fine for pdf data, but with EPS data, it gives error

renderer returned unicode, and did not specify a charset value
Anuj TBE
  • 9,198
  • 27
  • 136
  • 285

1 Answers1

0

You need to change from charset = None to charset = 'utf-8'

Florent
  • 1,791
  • 22
  • 40