Apache SuperSet提供了将报告转换为PDF的功能。可以通过安装Wkhtmltopdf软件并将其配置为Apache SuperSet中的默认选项来实现这一点。以下是在SuperSet中将报告转换为PDF的代码示例:
from flask_appbuilder.reports.views import DirectByChartView
from flask_appbuilder import expose
class MyDirectByChartView(DirectByChartView):
@expose("/show/")
def show(self):
result = super(MyDirectByChartView, self).show()
if result and isinstance(result, dict):
if self.export_type == 'pdf':
filename = self.render_to_file()
return self.serve_file(filename)
return result
def render_to_file(self):
pdf_path = super(MyDirectByChartView, self).render_to_file()
# Call wkhtmltopdf to generate pdf from html file
# The following example is based on Ubuntu Linux
from subprocess import Popen, PIPE
wkhtmltopdf_path = '/usr/local/bin/wkhtmltopdf'
pdf_file = pdf_path + '.pdf'
Popen([wkhtmltopdf_path, pdf_path, pdf_file], stdout=PIPE, stderr=PIPE).communicate()
return pdf_file
在上面的代码中,如果请求的导出类型为PDF(即“self.export_type == 'pdf'”),则会在方法“render_to_file”中使用Wkhtmltopdf软件生成报告的PDF版本。这样,PDF文件就可以直接下载或展示给用户。