我使用rst2pdf在我的Django应用程序中生成一个格式化的pdf文件。这是我用来做这个的代码:
temp_file = tempfile.NamedTemporaryFile(mode='w+t', dir=PDF_PATH, delete=False)
temp_pdf = tempfile.NamedTemporaryFile(dir=PDF_PATH, delete=False)
temp_file.write(plaintext)
subprocess.call('rst2pdf ' + temp_file.name +' -s '+ STYLESHEET_PATH +' -o ' + temp_pdf.name, shell=True)
pdf = open(temp_pdf.name, 'rb')
response = HttpResponse(pdf.read(), mimetype='application/force-download')
response['Content-Disposition'] = 'attachment; filename=output.pdf'
response['Content-Length'] = os.stat(temp_pdf.name).st_size
return response由于某些原因,被用作响应的pdf在最后一页被截断。文件系统上的pdf被截断。我可以看到写在pdf上的明文,就在那里。我甚至使用所有相同的参数在命令行上运行相同的rst2pdf调用,并且生成的pdf很好。但出于某种原因,当我在Django应用程序中调用rst2pdf时,文件会被截断。对于导致这种情况的原因,或者我如何调试子进程调用,有什么想法吗?
发布于 2013-10-31 00:55:05
看起来我并不完全理解python是如何处理可写文件的。显然,在调用temp_file.write之后,缓冲区中仍然存在数据,除非我调用了temp_file.close()或temp_file.flush(),否则这些数据不会被写入。
https://stackoverflow.com/questions/19460066
复制相似问题