Giter Site home page Giter Site logo

Comments (2)

vdjango avatar vdjango commented on August 15, 2024 1

好吧,既然没人说话,那就我补充一下解决方案吧:

很简单,只需要修改一个文件,添加一个文件,共2处位置,就可以实现中文喽!
先来看看效果吧
image

安装 django-easy-pdf 后,找到它的位置 [Python36\Lib\site-packages\xhtml2pdf]
我们修改的是 xhtml2pdf 所以是这个路径,因为安装django-easy-pdf 的时候会安装上他

然后我们找到这个路径
Python36\Lib\site-packages\xhtml2pdf\default.py

DEFAULT_FONT = { 
    "courier": "Courier",
    "courier-bold": "Courier-Bold",
    "courier-boldoblique": "Courier-BoldOblique",
    "courier-oblique": "Courier-Oblique",

    # 修改这一行为 ["helvetica": "SimHei"]
    "helvetica": "helvetica", 
    
    # SimHei 为黑体,是系统的自带字体大部分系统都有这个字体,所以就用了它
    "helvetica": "SimHei"
    
    ........
}

然后在这里添加并注册字体
Python36\Lib\site-packages_init_,py

from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont

# 只要你系统中存在这个字体,就可以这样写,这里的SimHei.ttf我没有添加如何路径哦,它会从系统字库里面找
pdfmetrics.registerFont(TTFont('SimHei', 'SimHei.ttf'))

# 这一个方法,只有这个字体xhtml2pdf内部存在时可用
# from reportlab.pdfbase.cidfonts import UnicodeCIDFont
# pdfmetrics.registerFont(UnicodeCIDFont('STSong-Light'))

完整的例子:
Python36\Lib\site-packages\xhtml2pdf\default.py

DEFAULT_FONT = { 
    "courier": "Courier",
    "courier-bold": "Courier-Bold",
    "courier-boldoblique": "Courier-BoldOblique",
    "courier-oblique": "Courier-Oblique", 
    "helvetica": "SimHei"
    ........
}

Python36\Lib\site-packages_init_,py

from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont

pdfmetrics.registerFont(TTFont('SimHei', 'SimHei.ttf'))

from django-easy-pdf.

marksweb avatar marksweb commented on August 15, 2024

I know it's a few years later, but we've got unicode working just now by adding this meta to the template;

<meta http-equiv=Content-Type content="text/html;charset=utf-8">

from django-easy-pdf.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.