pdftk
을 사용하여 장고는 사용자에게 제공되는 동적 임시 pdf 파일을 생성합니다.모바일 장치에서 Django의 동적 PDF를 제공하는 방법
데스크톱에서는 잘 작동합니다. pdf 파일이 열리면서 모든 Android 브라우저에서 내 브라우저에 저장됩니다 (iOS에서는 동일하지만 iOS에서는 테스트 할 수 없음). , pdf가 성공적으로 다운로드되지 않습니다. 다운로드가 시작되지만 항상 실패하고 이유를 파악할 수 없습니다. 왜 이런 일이 될 수도에 관한
def get_pdf():
fdf = {...}
t1 = tempfile.NamedTemporaryFile(delete=False)
t2 = tempfile.NamedTemporaryFile(delete=False)
t1.file.write(fdf)
# close temp files for pdftk to work properly
t1.close()
t2.close()
p = Popen('pdftk %s fill_form %s output %s flatten' %
('original.pdf', t1.name, t2.name), shell=True)
p.wait()
with open(t2.name, 'rb') as fid:
data = fid.read()
# delete t1 and t2 since they are temp files
# at this point the data is the binary of the pdf
return data
def get_pdf(request):
pdf = get_pdf()
response = HttpResponse(pdf, mimetype='application/pdf')
response['Content-Disposition'] = 'filename=foofile.pdf'
return response
어떤 아이디어 :
다음은 뷰의 미리보기와 PDF 바이너리 데이터를 생성하는 기능입니다 다음?
큰 차이는 있지만 응답 [ 'Content-Disposition'] = 'attachment'로 응답 [ 'Content-Disposition'] = 'filename = foofile.pdf'을 바꾸려고합니다. filename = foofile.pdf ' 그러면 PDF 다운로드가 즉시 시작됩니다. 모바일 장치에서 더 잘 작동 할 수 있습니다. – Pascal
생성 된 PDF 파일의 크기에 Content-Length 헤더를 추가하면 도움이 될 수도 있습니다. – Pascal
도움 주셔서 감사합니다. 어떻게 해결했는지에 대한 답변을 참조하십시오. – miki725