[PYTHON] Output PDF with Django

Try to output a PDF according to the Django tutorial

Since Django can generate PDF, I tried to output PDF according to the tutorial.

Preparation

(virtualenv) $ sudo pip install reportlab
(virtualenv) $ django-admin startproject pdf_creater
(virtualenv) $ cd pdf_creater
(virtualenv) $ python manage.py startapp myapp

pdf_creater/urls.py


from django.conf.urls import include, url
from django.contrib import admin

urlpatterns = [
    url(r'^myapp/', include('myapp.urls')),
    url(r'^', include('myapp.urls',namespace='myapp')),
    url(r'^admin/', admin.site.urls),
]

myapp/urls.py


from django.conf.urls import url
from . import views

urlpatterns = [
    url(r'^$', views.index, name='index'),
]

That's why I try to start downloading the PDF suddenly with index.

Create views

It's just a tutorial.

myapp/views.py


import sys, codecs
sys.stdout = codecs.getwriter('utf-8')(sys.stdout)

from django.http.response import HttpResponse
from reportlab.pdfgen import canvas

def index(request):
    response = HttpResponse(content_type='application/pdf')
    response['Content-Disposition'] = 'attachment;filename="createdfile.pdf"'

    p = canvas.Canvas(response)
    p.drawString(100,100,'newPDF')

    create = p.showPage()
    p.save()

    return response

migrate and runserver

The PDF download will start with the name createdfile.pdf. It feels like you can receive a text file from a form and create a PDF.

If the customization goes well, it will be about the receipt issuing application.

Recommended Posts

Output PDF with Django
Markdown output with Django
Output PDF with WeasyPrint
Internationalization with django
CRUD with Django
PDF output with Latex extension in Sphinx
Authenticate Google with Django
Django 1.11 started with Python3.6
Upload files with Django
Development digest with Django
Output Django Detail View as PDF (Japanese support)
Use Gentelella with django
Twitter OAuth with Django
Getting Started with Django 1
Send email with Django
File upload with django
View PDF with fbterm
Use LESS with Django
Pooling mechanize with Django
Use MySQL with Django
Start today with Django
Getting Started with Django 2
The story that Japanese output was confused with Django
Do Django with CodeStar (Python3.6.8, Django2.2.9)
Get started with Django! ~ Tutorial ⑤ ~
Minimal website environment with django
Create an API with Django
Do Django with CodeStar (Python3.8, Django2.1.15)
Try Python output with Haxe 3.2
Deploy Django serverless with Lambda
Python3 + Django ~ Mac ~ with Apache
Test standard output with Pytest
Getting Started with Python Django (1)
Create a homepage with django
Output to syslog with Loguru
Output table structure in Django
Get started with Django! ~ Tutorial ④ ~
Getting Started with Python Django (4)
Web application creation with Django
Output large log with discord.py
Getting Started with Python Django (3)
Combine FastAPI with Django ORM
Get started with Django! ~ Tutorial ⑥ ~
Save tweet data with Django
Integrate PDF files with Python
Do AES encryption with DJango
Getting Started with Python Django (6)
Combine two images with Django
Getting Started with Django with PyCharm
Real-time web with Django Channels
Double submit suppression with Django
Django REST framework with Vue.js
Use prefetch_related conveniently with Django
Getting Started with Python Django (5)
Login with django rest framework
Qiita API Oauth with Django
How to output a document in pdf format with Sphinx
Number chapters and sections when PDF output (rst2pdf) with Sphinx
Test Driven Development with Django Part 3
reload in django shell with ipython
Steps to develop Django with VSCode