wkhtmltopdf 转换后 表格不显示
def post(self, request, *args, **kwargs):
# 获取的直接是富文本中的html字符串
content = request.DATA.get('content')
file_name = request.DATA.get('file_name')
file_type = request.DATA.get('file_type')
direction = request.DATA.get('direction')
options = constants.PDFKIT_OPTIONS
# 横向 或 纵向
if direction:
options['orientation'] = 'Landscape'
# 判断如果有table 加上样式,就可以显示表格了
if '<table' in content:
content = '<style>table {max-width: 100%;} td {border: 1px solid #ccc;}</style>' + content
file_path = '{}_{}.pdf'.format(file_name, uuid.uuid4())
ExportPdfHelper(content, file_path, options).export_pdf()
'''
这里实际调用pdfkit.from_string方法,
pdfkit.from_string(self.content, self.file_path, configuration=self.config, options=self.options)
参数:
content:h5字符串
file_path:生成的pdf存放位置,
configuration: pdfkit.configuration(wkhtmltopdf=settings.WKHTMLTOPDF_PATH) 工具位置
options:
PDFKIT_OPTIONS = {
'page-size': 'Letter',
'margin-top': '0.75in',
'margin-right': '0.75in',
'margin-bottom': '0.75in',
'margin-left': '0.75in',
'encoding': "UTF-8",
'no-outline': None,
'quiet': '',
'enable-forms': '',
}
options 明细 https://www.jianshu.com/p/4d65857ffe5e
'''
ret, info = upload_file_to_qiniu(file_path, file_path)
qiniu_url = settings.QINIU_CDN_DOMAIN + "/" + ret['key']
return Response({'status': 1, 'file_url': qiniu_url})
# 如果不想表格被分页打断, border防止表格线变双行
'<style>' \
'table {max-width: 100%; ' border-collapse: collapse;} ' \
' td {border: 1px solid #ccc;}' \
'table {page-break-inside: avoid !important;}' \
'</style>'