我目前正在使用此代码从模板生成pdf,其中需要设置某些变量。一切正常。但pdf也会保存到执行.py文件的文件夹中。
我实际上不需要它被保存。我可以在上传后立即删除它,但这将是一个工作场所。我觉得可能有更好的方法。
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 :(得分:1)
通过将the_invoice_filename
更改为False
来解决问题。如果此参数为False
,则不保存该文件。
现在,AWS必须使用k.set_contents_from_string(YOUR_CONTENTS)
代替k.set_contents_from_filename(YOUR_CONTENTS)
以下是一个完整的例子:
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)