【发布时间】:2011-04-20 01:10:36
【问题描述】:
我想编写一个生成 .pdf 文档的服务器端 python 脚本。
目前我在服务器端安装了 Python 2.7 并且 matplolib 也安装了服务器端。
创建简单绘图并生成 .png 图片的简单脚本 有效。
这是我使用的脚本:
# to access standard output :
import sys
# select a non-GUI backend :
import matplotlib
matplotlib.use('Agg')
#matplotlib.use("cairo.pdf")
#matplotlib.use('PDF')
# import plotting module :
import matplotlib.pyplot as plt
# generate the plot :
plt.plot([1,2,3,2,3,4])
# print the content type (what's the data type)
# the new line is embedded, using '\n' notation :
print "Content-Type: image/png\n"
# print "Content-Type: image/PDF\n"
# print "Content-type: application/pdf"
# output directly to webserver, as a png file:
plt.savefig(sys.stdout, format='png')
# plt.savefig(sys.stdout, format='PDF')
# plt.savefig( "test.pdf", format='pdf' )
我想知道如何做同样的事情,但发送一个 pdf 文件而不是 一张png图片。 (# 或粗体字符代表我尝试并发表评论的所有内容)
有人知道吗?
谢谢。
让克劳德
【问题讨论】:
-
您能描述一下您尝试时pdf输出失败的原因吗?
-
当我使用这个时:plt.savefig(sys.stdout,format='pdf') 当我使用这个时,我得到:“文件已损坏,无法恢复”:plt.savefig (sys.stdout.write('test6.pdf'),format='pdf') 我得到:文件没有以 '%PDF-' 开始
-
尝试使用编辑器打开文件。我猜第一行是“Content-Type: image/png”(不带引号),然后它才会变成 %PDF-etc --- 这就是 pdf 文件的开头。
-
现在可以使用了!谢谢大家。
标签: python cgi matplotlib