2016-03-20 20 views
1

私は、Pythonを使ってGoogle App Engineアプリケーション(webapp2を)& Jinja2のを開発していると私はreportlabライブラリを使用してPDFファイルを作成しようとしています。webapp2を - 読み取り専用ファイルシステムエラー

例:私は、私は次のエラーを取得していますサーバーで実行すると

from reportlab.pdfgen import canvas 

class pdf(webapp2.RequestHandler): 
    def get(self): 
    x = 50 
    y = 750 
    c = canvas.Canvas("file.pdf") 
    c.drawString(x*5,y,"Output") 
    c.line(x,y-10,x*11,y-10) 
    c.save() 

raise IOError(errno.EROFS, 'Read-only file system', filename) 
IOError: [Errno 30] Read-only file system: u'file.pdf' 

答えて

1

私はそれがStringIOを使用して動作するようになった:

from reportlab.pdfgen import canvas 

# Import String IO which is a 
# module that reads and writes a string buffer 
# cStringIO is a faster version of StringIO 
from cStringIO import StringIO 

class pdf(webapp2.RequestHandler): 
    def get(self): 
    pdfFile = StringIO() 

    x = 50 
    y = 750 
    c = canvas.Canvas(pdfFile) 
    c.drawString(x*5,y,"Output") 
    c.line(x,y-10,x*11,y-10) 
    c.save() 

    self.response.headers['content-type'] = 'application/pdf' 
    self.response.headers['Content-Disposition'] = 'attachment; filename=file.pdf' 
    self.response.out.write(pdfFile.getvalue()) 
3

をあなたはAppEngineのファイルシステムに書き込むことができない - 結局のところ、あなたが持っているので、複数のマシン(あなたはいつも同じものをguanranteeすることはできません)、あなたはどのマシンのファイルシステムに書きますか?

ただし、reportlab canvasが開いているファイルオブジェクトを受け入れるように見えます。私はこれが動作することを保証することはできませんが、代わりに開いているファイルのようなオブジェクトを渡すことができます。例えばio.BytesIOまたはさらにwebapp2.Response.outである。

import io 
class pdf(webapp2.RequestHandler): 
    def get(self): 
    x = 50 
    y = 750 
    c = canvas.Canvas(self.response.out) 
    c.drawString(x*5,y,"Output") 
    c.line(x,y-10,x*11,y-10) 
    c.save() 
+0

私はエラーになっています: 'はAttributeError:out' – kalpetros

+0

当たり前に...申し訳ありませんが、私は' webapp2.Response'オブジェクトを意味'webapp2.Request'オブジェクトではありません... – mgilson

+0

ファイルシステムに書き込むことができたとしても、これは正しいアプローチです。それ以外の場合、同時に複数のPDF要求があった場合はどうなりますか? –

関連する問題