LoginSignup
5
8

More than 5 years have passed since last update.

Python + Django でPDF作成

Last updated at Posted at 2018-06-05

DjangoでPDFを出力してみます。

1.環境

(1)Python, Djangoのバージョンです。

Python 3.6.5
Django 2.0.4

(2)仮想環境とプロジェクト

仮想環境の作成、プロジェクトの作成は「Python + djangoをWindowsにインストールする」を参照。
https://qiita.com/tiguchi919/items/f9052d259cec7fe54a00

virtualenv:venv
project:mysite

2.ReportLabをインストール

PDFを出力するためのライブラリ「ReportLab」をインストールします

(venv) c:\data\python>pip install reportlab
Collecting reportlab
  Downloading https://files.pythonhosted.org/packages/da/47/fd3afea084d4405be9f49c1916ac4273709780881bffe714414a93b82431/reportlab-3.4.0-cp36-cp36m-win_amd64.whl (2.1MB)
    100% |████████████████████████████████| 2.1MB 1.1MB/s
Requirement already satisfied: setuptools>=2.2 in .\venv\lib\site-packages (from reportlab) (39.1.0)
Collecting pillow>=2.4.0 (from reportlab)
  Downloading https://files.pythonhosted.org/packages/a4/86/283719dac6309cf483452abb09759be9b2c0974435ed608dc67949127e13/Pillow-5.1.0-cp36-cp36m-win_amd64.whl (1.6MB)
    100% |████████████████████████████████| 1.6MB 2.2MB/s
Requirement already satisfied: pip>=1.4.1 in .\venv\lib\site-packages (from reportlab) (10.0.1)
Installing collected packages: pillow, reportlab
Successfully installed pillow-5.1.0 reportlab-3.4.0

3.アプリケーションを作成する

(1)作成コマンド

(venv)python manage.py startapp samplepdf

(2)ファイル構成

C:\data\python\mysite
│  db.sqlite3
│  manage.py
│  
├─mysite
│  │  settings.py
│  │  urls.py
│  │  wsgi.py
│  │  __init__.py
・・・・
│          
├─samplepdf
│  │  admin.py
│  │  apps.py
│  │  models.py
│  │  tests.py
│  │  urls.py
│  │  views.py
│  │  __init__.py
│  │  
・・・・

4.プロジェクト全体の設定

(1)mysite\urls.py

urls.py
urlpatterns = [
    path('samplepdf/', include('samplepdf.urls')),  #追加
    path('admin/', admin.site.urls),
]

(2)mysite\settings.py

settings.py
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'samplepdf', # 追加
]

5.アプリケーションの設定

(1)samplepdf\urls.py

startappコマンドでは、urls.pyは作成されないので、ファイルを作成してください。

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

app_name = 'samplepdf'
urlpatterns = [
    url('', views.index, name='index'),
]

(2)samplepdf\views.py

views.py
from django.http import HttpResponse
from reportlab.pdfgen import canvas

# Create the HttpResponse object with the appropriate PDF headers.
def index(request):
    response = HttpResponse(content_type='application/pdf')
    response['Content-Disposition'] = 'attachment; filename="somefilename.pdf"'
    # Create the PDF object, using the response object as its "file."
    p = canvas.Canvas(response)
    # Draw things on the PDF. Here's where the PDF generation happens.
    # See the ReportLab documentation for the full list of functionality.
    p.drawString(1, 800, "Hello world.")
    # Close the PDF object cleanly, and we're done.
    p.showPage()
    p.save()
    return response

6.確認

(1)サーバを起動する

cmd.prompt
(venv) c:\data\python\mysite>python manage.py runserver

(2)URLにアクセスする

http://localhost:8000/samplepdf

image.png

5
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
8