[英]Use AWS Chalice to generate PDF
I am looking for creating internal APIs for me that would generate pdfs as API call and use chalice to deploy it as serverless. 我正在寻找为我创建内部API,它将生成pdf作为API调用并使用chalice将其部署为无服务器。 So far I have code that I am stuck with 500 error that I can not debug: 到目前为止,我有代码,我遇到500错误,我无法调试:
from chalice import Chalice, Response
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import letter
from reportlab.lib.pagesizes import landscape
from io import BytesIO
app = Chalice( app_name = 'printago' )
@app.route( '/' )
def index( ) :
buff = BytesIO( )
# p = canvas.Canvas( 'someName.pdf', pagesize = landscape( letter ) )
p = canvas.Canvas( buff, pagesize = landscape( letter ) )
p.setFont( 'Helvetica', 40, leading = None )
p.drawCentredString( 415, 300, 'Hallo YOU' )
p.showPage( )
p.save( )
pdf = buff.getvalue( )
buff.close( )
return Response( pdf, status_code = 200, headers = { 'Content-Type' : 'application/pdf' } )
What am I doing wrong? 我究竟做错了什么?
I have virtual environment for this project, installed reportlab via pip in it, and after turning on debugging there is error Unable to import module 我有这个项目的虚拟环境,通过pip安装了reportlab,打开调试后出现错误无法导入模块
'app': No module named reportlab.pdfgen
And I was able to pass "No module" error by adding reportlab to requirments.txt file, however after this there is other error related to JSON serialization and utf-8 我可以通过将reportlab添加到requirments.txt文件来传递“No module”错误,但是在此之后还存在与JSON序列化和utf-8相关的其他错误
An error occurred during JSON serialization of response: 'utf8' codec can't decode byte 0x93 in position 10: invalid start byte
I would like to add that is working as expected when tested locally with command chalice local
我想补充说,当使用命令chalice local
进行本地测试时,它正在按预期工作
We also got the can't decode byte 0x93 in position 10: invalid start byte
error, which turned out to be a header put in the generated PDF itself by reportlab. 我们还得到了can't decode byte 0x93 in position 10: invalid start byte
错误,结果是由reportlab生成的PDF本身的头。 The first two lines of the PDF when opened in vim look like this: 在vim中打开时,PDF的前两行如下所示:
%PDF-1.4
%<93><8c><8b><9e> ReportLab Generated PDF document http://www.reportlab.com
Our workaround was to change the way that we send the PDF out to the client in our python-based cgi script: 我们的解决方法是改变我们在基于python的cgi脚本中将PDF发送到客户端的方式:
import codecs
print(codecs.open("/full/path/to/some.pdf", "r", encoding="utf-8", errors="ignore").read())
and the key there was the errors="ignore"
... which is ugly and bad and we've made this a priority to replace. 而关键是errors="ignore"
......这是丑陋和糟糕的 ,我们已经把它作为替代的优先事项。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.