python和matplotlib服务器端生成.pdf文档
我想编写一个服务器端 python 脚本来生成 .pdf 文档。
目前我在服务器端安装了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 图片。 (# 或粗体字符代表我尝试并发表评论的所有内容)
有人知道吗?
谢谢。
让-克洛德
i would like to write a server side python script that generate .pdf documents.
for the moment i have Python 2.7 installed server side
and matplolib installed server side too.
A simple script that create a simple plot and generate a .png picture
works.
this is the script i use :
# 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' )
I am wondering how to do the same thing but with sending a pdf file instead of
a png picture. (the # or bold character are for all the things i tried and put in comment)
Does someone know ?
thanks.
jean-claude
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,在您的代码中,您将 print 语句中的单词和图形本身发送到 stdout。
我刚刚尝试过你的脚本,像这样更改评论
,它对我来说效果很好。我正在使用 python 2.6.smth 和 matplolib 0.99
First of all, in your code you send to stdout both the words from the print statement and the figure itself.
I've just tried your script, changing the comments like this
and it works just fine for me. I'm using python 2.6.smth and matplolib 0.99
我只是在这里猜测,但是 正确的 MIME 类型 是 application/pdf ,并且在该注释行上,您不会在打印语句中包含必要的额外换行符。
I'm just guessing here, but the correct MIME type is application/pdf, and on that comment line you don't include the necessary extra newline in the print statement.