python and matplotlib server side to generate .pdf documents
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/pngn"
# print "Content-Type: image/PDFn"
# 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
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
# plt.savefig(sys.stdout, format='png')
# plt.savefig(sys.stdout, format='PDF')
plt.savefig( "test.pdf", format='pdf' )
and it works just fine for me. I'm using python 2.6.smth and matplolib 0.99
我只是在这里猜测,但正确的MIME类型是application / pdf,并且在该注释行中,您不在打印语句中包含必要的额外换行符。
链接地址: http://www.djcxy.com/p/8180.html