0
html 파일에서 이미지를 pdf로 내보내는 데 문제가 발생했습니다. 비슷한 해결책이 존재합니다. here.django에서 이미지와 텍스트를 html로 내보내기 PDF로
html이 서버에서 제대로 렌더링되고 있습니다. URL에 교차 확인하여 확인했습니다.
하지만 다운로드하는 동안 내가 PDF를 얻을 수 있지만, 비어있는, 또한 여기에
views.py
의 다운로드 기능의 세 번째 줄에 오류가 내가 뭘하려 말한다 **/PDF를 렌더링 :HTML 파일 :
<html>
<head>
<link href="{{ STATIC_URL }}css/certificate.css" rel="stylesheet"
type="text/css" />
</head>
<body>
<div class="certificate_container">
<div class="statictext">
<p>{{ name }}</p>
</div>
</div>
</body>
<html>
CSS 파일 :
body{margin:0px; padding:0px;}
.certificate_container{ width:792px; height:612px; background:url("../images/certificate.gif") no-repeat;}
.statictext{width:400px; margin:0px auto; padding-top:240px; height:30px; text-align:center; font:bold 14px Arial, Helvetica, sans-serif; color:#333;}
views.py :
#relevant imports
from reportlab.pdfgen import canvas
import xhtml2pdf.pisa as pisa
import cStringIO as StringIO
def download(request):
html = render_to_string("certificate.html", { 'pagesize' : 'A4', }, context_instance=RequestContext(request))
result = StringIO.StringIO()
pdf = pisa.pisaDocument(StringIO.StringIO(), dest=result, link_callback=fetch_resources)
if not pdf.err:
return HttpResponse(result.getvalue(), mimetype='application/pdf')
return HttpResponse('Gremlins ate your pdf! %s' % cgi.escape(html))
def fetch_resources(uri, rel):
path = os.path.join(settings.STATIC_ROOT, uri.replace(settings.STATIC_URL, ""))
return path
def home(request):
return render(request, 'certificate.html', {'name':'user1'})
URL이 올바르게 처리되었습니다.
이 정보가 도움이 되셨습니까? http://stackoverflow.com/questions/4678723/django-pdf-question-with-pisa – jperelli