Since Django can generate PDF, I tried to output PDF according to the tutorial.
(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.
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