2017-07-07 132 views
0

我使用reportlab創建pdf。我正在使用Reportlab段落。問題是我每次下載它時,都會產生一個空的txt。 我測試了它沒有Django,它沒有問題。如果我使用畫布,但它不適合我需要的畫布。Django Reportlab生成空的pdf

views.py

from django.http import HttpResponse 
from django.shortcuts import render 
from reportlab.lib.enums import TA_JUSTIFY 
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer 
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle 
from reportlab.lib.pagesizes import letter 


def genereaza_pdf(request): 
    if request.method == 'POST': 
     response = HttpResponse(content_type='application/pdf') 
     response['Content-Disposition'] = 'attachment; filename="example.pdf"' 
     doc = SimpleDocTemplate("example.pdf", pagesize=letter, rightMargin=70, leftMargin=70, topMargin=70, 
           bottomMargin=60) 
     report = [] 

     styles = getSampleStyleSheet() 
     styles.add(ParagraphStyle(name="Times", fontName='Times-Roman', fontSize=15, alignment=TA_JUSTIFY)) 

     p_text = "<u>ANEXA 1</u>" 
     report.append(Paragraph(p_text, styles["Times"])) 
     report.append(Spacer(1, 5)) 
     doc.build(report) 
     return response 
    return render(request, 'pdf_test.html') 

pdf_test.html

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset="UTF-8"> 
    <title>Download pdf</title> 
</head> 
<body> 
    <form method="POST"> 
     {% csrf_token %} 
     <button type="submit">Download</button> 
    </form> 
</body> 
</html> 

出了什麼問題?

回答

0

功能是生成兩個文件,一個在下載爲默認,但它是一個空的TXT和一個項目的目錄。我做了更改。

def genereaza_pdf(request): 
    if request.method == 'POST': 
     response = HttpResponse(content_type='application/pdf') 
     response['Content-Disposition'] = 'attachment; filename="example.pdf"' 
     doc = SimpleDocTemplate(response, pagesize=letter, rightMargin=70, leftMargin=70, topMargin=70, 
           bottomMargin=60) 
     report = [] 

     styles = getSampleStyleSheet() 
     styles.add(ParagraphStyle(name="Times", fontName='Times-Roman', fontSize=15, alignment=TA_JUSTIFY)) 

     p_text = "<u>ANEXA 1</u>" 
     report.append(Paragraph(p_text, styles["Times"])) 
     report.append(Spacer(1, 5)) 
     doc.build(report) 
     return response 
    return render(request, 'pdf_test.html') 
1

您必須將文件寫入流中。試試這個:

from io import BytesIO 
def genereaza_pdf(request): 
    if request.method == 'POST': 
     response = HttpResponse(content_type='application/pdf') 
     response['Content-Disposition'] = 'attachment; filename="example.pdf"' 
     buff = BytesIO() 
     doc = SimpleDocTemplate(buff, pagesize=letter, rightMargin=70, leftMargin=70, topMargin=70, 
           bottomMargin=60) 
     report = [] 

     styles = getSampleStyleSheet() 
     styles.add(ParagraphStyle(name="Times", fontName='Times-Roman', fontSize=15, alignment=TA_JUSTIFY)) 

     p_text = "<u>ANEXA 1</u>" 
     report.append(Paragraph(p_text, styles["Times"])) 
     report.append(Spacer(1, 5)) 
     doc.build(report) 
     response.write(buff.getvalue()) 
     buff.close() 
     return response 
    return render(request, 'pdf_test.html')