Comments (2)
好吧,既然没人说话,那就我补充一下解决方案吧:
很简单,只需要修改一个文件,添加一个文件,共2处位置,就可以实现中文喽!
先来看看效果吧
安装 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.
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)
- How to display page numbers in the footer
- link external style sheet not working HOT 3
- Allow files to be named and displayed on the screen
- Fix context request
- keep getting wrong operand type error on pdfs.
- Conditional PDF rendering based on query parameters
- Word wrap not working HOT 4
- Question
- Render dynamic template before creating pdf view
- utf-8 not working HOT 2
- css not working when updated to 0.2.0.dev1
- TypeError at /cart/ get_context_data() missing 1 required positional argument: 'request' HOT 2
- Add support for Django 3.0 HOT 9
- Django.utils.six Error on MacOS HOT 3
- project not maintained HOT 11
- Add style in html: OK but add static font in render, possible ???
- you have breaked my code
- error 1
- error 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from django-easy-pdf.