标签:exp mat 文本编辑 open arch item string sub mis
class CourseMaterialExportPdfAPIView(generics.GenericAPIView):
permission_classes = (IsAuthenticatedOrHasOpenid,)
def get(self, request, *args, **kwargs):
pk = kwargs.get(‘pk‘)
try:
material = CourseMaterial.objects.get(id=pk)
content = material.content
import re
# 处理视频内容,因为不能展示视频,所以将视频的html代码转换为视频内容和连接
pattern = re.compile(r‘<video.*?>‘)
video_list = pattern.findall(content)
sub_dic = dict()
for video_tem_code in video_list:
res = re.search(‘src=(.*?) ‘, video_tem_code)
url = res.group(1)
repl = ‘<p>视频内容地址:%s</p>‘ % url
sub_dic[video_tem_code] = repl
for string, repl in sub_dic.items():
content = content.replace(string, repl)
# 需要安装xhtml2pdf模块 pip install
from xhtml2pdf import pisa
# 处理汉字乱码问题
from xhtml2pdf.default import DEFAULT_FONT
DEFAULT_FONT[‘helvetica‘] = ‘yh‘
#导出,直接下载pdf
response = HttpResponse(content_type=‘application/pdf‘)
response[‘Content-Disposition‘] = ‘attachment; filename=%s.pdf‘ % material.name
#content为富文本的内容, response为保存生成的pdf的地方
pisa.CreatePDF(StringIO(content), response)
return response
except CourseMaterial.DoesNotExist:
return Response({‘status‘: 0, ‘error‘: ‘活动不存在‘})
标签:exp mat 文本编辑 open arch item string sub mis
原文地址:https://www.cnblogs.com/robert-zhou/p/12621140.html