gpt4 book ai didi

python - 如何在 Django 中将 PDFTemplateResponse 转换为类似字节的对象?

转载 作者:行者123 更新时间:2023-12-01 09:32:23 26 4
gpt4 key购买 nike

我正在尝试使用 wkhtmltopdf 生成 PDF 并通过电子邮件将其作为附件发送。这是我的观点:

class MYPDFView(View):
template = 'pdftemplate.html'

def get(self, request):

data = {}

response = PDFTemplateResponse(
request=request,
template=self.template,
filename="hello.pdf",
context= data,
show_content_in_browser=True,
cmd_options={'margin-top': 10,
"zoom":1,
"viewport-size" :"1366 x 513",
'javascript-delay':1000,
'footer-center' :'[page]/[topage]',
"no-stop-slow-scripts":True},
)

email = EmailMessage(
'Hello',
'Body goes here',
'from@example.com',
['to1@example.com', 'to2@example.com'],
['bcc@example.com'],
reply_to=['another@example.com'],
headers={'Message-ID': 'foo'},
attachments=[('demo.pdf', response, 'application/pdf')]
)

email.send()

return response

我收到的错误是一个TypeError,它表示预期的类似字节的对象,而不是PDFTemplateResponse

我假设我返回以查看 PDF 的 response 变量不是我应该在 attachments 属性中提供的类型。

我的问题是,如何在附件三元组中提供 PDFTemplateResponse 之前将其转换为类似字节的对象?

最佳答案

return_file = "tmp/hello.pdf"    
temp_file = response.render_to_temporary_file("hello.html")
wkhtmltopdf(pages=[temp_file.name], output=return_file)
email.attach_file(return_file)

Source

关于python - 如何在 Django 中将 PDFTemplateResponse 转换为类似字节的对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49851702/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com