2017-07-11 11 views
1

現在、このコードを使用して、特定の変数を設定する必要があるテンプレートからpdfを生成しています。すべてうまく動作します。しかし、pdfも.pyファイルを実行しているフォルダに保存されます。Pythonフラスコアップロードpdfkitを使用してPDFファイルを生成し、AWS S3に保存しないでください。

実際に保存する必要はありません。私はアップロード直後にそれを削除することができましたが、それは回避策になります。私はもっ​​と良い方法があるように感じる。 the_invoice_filenameFalseのを変更することによって解決

path_wkthmltopdf = r'C:\Programme\wkhtmltopdf\bin\wkhtmltopdf.exe' 
config = pdfkit.configuration(wkhtmltopdf=path_wkthmltopdf) 

pdfkit.from_string(render_template('invoice_template.html', invoice_id=the_id, invioce_date_start=the_start_date, 
         invioce_date_end=the_end_date, invioce_company_name=the_invoice_company, invioce_user_vorename=the_invoice_user_forename, 
         invioce_user_surname=the_invoice_user_surname, invioce_user_email=the_invoice_user_email), the_invoice_filename, configuration=config) 

new_invoice = Rechnung(name=the_invoice_filename, date_created=the_start_date, mandatnummer=1, rechnungsnummer=1, users_id=current_user.id) 
db_session.add(new_invoice) 

s3 = boto.connect_s3(app.config['MY_AWS_ID'], app.config['MY_AWS_SECRET'], host='s3.eu-central-1.amazonaws.com') 
# Get a handle to the S3 bucket 
bucket_name = 'my-bucket' 
bucket = s3.get_bucket(bucket_name) 
k = Key(bucket) 

k.key = "invoices/" + the_invoice_filename 
k.set_contents_from_filename(the_invoice_filename) 
+0

は、あなたがこれを行う方法を理解しました:

今、AWSではなく、ここで

完全な例があるk.set_contents_from_filename(YOUR_CONTENTS)k.set_contents_from_string(YOUR_CONTENTS)を使用する必要がありますか? – atwalsh

答えて

0

号。この引数がFalseの場合、ファイルは保存されません。

def create_invoice(the_invioce_rechnungsnummer):   

    config = pdfkit.configuration(wkhtmltopdf=app.config['WKHTMLTOPDF_CMD']) 

    the_pdf = pdfkit.from_string(render_template('invoice_template.html', invioce_rechnungsnummer=the_invioce_rechnungsnummer), False, configuration=config) 

    s3 = boto.connect_s3(app.config['MY_AWS_ID'], app.config['MY_AWS_SECRET'], host='s3.eu-central-1.amazonaws.com') 
    bucket_name = 'YOUR_BUCKET_NAME' 
    bucket = s3.get_bucket(bucket_name) 
    k = Key(bucket)    

    PDF_FILE_NAME = "YOUR_PDF_FILE_NAME" 
    k.key = "BUCKET/" + PDF_FILE_NAME 
    k.set_contents_from_string(the_pdf) 
関連する問題