官网:https://pypi.org/project/django-xhtml2pdf/
1.新建django项目安装django-xhtml2pdf
pip install django-xhtml2pdf
2.views.py
from django_xhtml2pdf.utils import pdf_decorator @pdf_decorator(pdfname=str(time.strftime('%Y-%m-%d-%H-%M-%S',time.localtime())) + str(random.random()) + ".pdf") def pdf(request): return render(request, "pdf.html")
3.解决中文乱码问题
在项目static目录下新建font 目录结构 static-->font
下载 msyh.ttf
4.设置setting.py
from reportlab.pdfbase.ttfonts import TTFont from reportlab.pdfbase import pdfmetrics FONT_PATH = os.path.join(BASE_DIR, r'cjystatic\font\msyh.ttf') # 公钥 pdfmetrics.registerFont(TTFont('yh', FONT_PATH)) from xhtml2pdf.default import DEFAULT_FONT DEFAULT_FONT["helvetica"] = 'yh'
5.运行效果
来源:https://www.cnblogs.com/wangcongxing/p/12518327.html