파이톤+도장고로 PDF 만들기

6808 단어 Django2.0python3.6

Django를 사용하여 PDF를 내보냅니다.


1. 환경
(1) Python, Django 버전
Python 3.6.5
Django 2.0.4
(2) 가상 환경 및 프로젝트
가상 환경의 생성, 프로젝트의 생성 참조는'Windows에 Python+django 설치'입니다.
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

좋은 웹페이지 즐겨찾기