A simple PDF utils for Django Rest Framework
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
docs Merge pull request #19 from drgarcia1986/response-by-filename May 6, 2015
drf_pdf
examples
tests
.bumpversion.cfg
.coveragerc
.editorconfig
.gitignore
.landscape.yaml
.travis.yml
LICENSE
MANIFEST.in
Makefile
README.md
conftest.py
mkdocs.yml
requirements-dev.txt
setup.py
tox.ini

README.md

DRF-PDF

Documentation Status Build Status Coverage Status Code Health

A simple PDF utils for Django Rest Framework

Install

pip install drf-pdf

Example

# coding: utf - 8
from rest_framework import status
from rest_framework.response import Response
from rest_framework.views import APIView

from drf_pdf.renderer import PDFRenderer

from my_pdf_package import PDFGenerator


class PDFHandler(APIView):

    renderer_classes = (PDFRenderer, )

    def get(self, request):
        pdf = PDFGenerator('foo')
        headers = {
            'Content-Disposition': 'filename="foo.pdf"',
            'Content-Length': len(pdf),
        }

        return Response(
            pdf,
            headers=headers,
            status=status.HTTP_200_OK
        )

With two or more renderer_classes

# coding: utf - 8
from rest_framework import status
from rest_framework.response import Response
from rest_framework.renderers import JSONRenderer
from rest_framework.views import APIView

from drf_pdf.response import PDFResponse
from drf_pdf.renderer import PDFRenderer

from my_pdf_package import get_pdf


class PDFHandler(APIView):

    renderer_classes = (PDFRenderer, JSONRenderer)

    def get(self, request, pdf_id):
        pdf = get_pdf(pdf_id)
		if not pdf:
			return Response(
				{'error': 'Not found'},
				status=status.HTTP_404_NOT_FOUND
			)

        return PDFResponse(
            pdf=pdf,
			file_name=pdf_id,
            status=status.HTTP_200_OK
        )