2010-05-24 1 views
7

브라우저에서 django의 .pdfs를 생성하는 데 피사 (pisa)가 있습니다. 그러나 파일을 디스크에 자동으로 쓰고 싶다면 어떻게해야합니까? 내가하고 싶은 일은 지정된 시점에 .pdf 버전 파일을 생성하고 업로드 디렉토리에 저장하여 브라우저 상호 작용이 없도록하는 것입니다. 이것이 가능한가?피사를 사용하여 디스크에 PDF 파일을 작성하십시오.

답변

12

예 가능합니다. 예를 들어, 선발로 Greg Newman에서 코드를 사용하여 :

from django.template.loader import get_template 
from django.template import Context 
import ho.pisa as pisa 
import cStringIO as StringIO 
import cgi 

def write_pdf(template_src, context_dict, filename): 
    template = get_template(template_src) 
    context = Context(context_dict) 
    html = template.render(context) 
    result = open(filename, 'wb') # Changed from file to filename 
    pdf = pisa.pisaDocument(StringIO.StringIO(
     html.encode("UTF-8")), result) 
    result.close() 

당신은 단지 딕셔너리와 파일 이름에 데이터를 템플릿으로 write_pdf를 호출해야합니다.

+0

감사합니다. 내가 필요한 것. – PhoebeB