我正在使用 xhtml2pdf。困难在于西里尔字母显示为黑色方块。搜索引擎建议您需要连接字体。xhtml2pdf 指令包含link_callback 函数,该函数负责链接到连接的资源。但我不明白我做错了什么。设置.py:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static_dev'),)
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
视图.py:
def link_callback(uri, rel):
result = finders.find(uri)
if result:
if not isinstance(result, (list, tuple)):
result = [result]
result = list(os.path.realpath(path) for path in result)
path = result[0]
else:
sUrl = settings.STATIC_URL # Typically /static/
sRoot = settings.STATIC_ROOT # Typically /home/userX/project_static/
mUrl = settings.MEDIA_URL # Typically /media/
mRoot = settings.MEDIA_ROOT # Typically /home/userX/project_static/media/
if uri.startswith(mUrl):
path = os.path.join(mRoot, uri.replace(mUrl, ""))
elif uri.startswith(sUrl):
path = os.path.join(sRoot, uri.replace(sUrl, ""))
else:
return uri
if not os.path.isfile(path):
raise Exception(
'media URI must start with %s or %s' % (sUrl, mUrl)
)
return path
HTML:
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<style>
@font-face {
font-family: DejaVuSans;
src: url({% static 'css/fonts/DejaVuSans.ttf' %});
}
* {
font-family: DejaVuSans;
}
</style>
</head>
<body>
<div class="font-family">Привет Мир!</div>
</body>
</html>
错误:
The joined path (C:\static\css\fonts\DejaVuSans.ttf) is located outside of the base path component (C:\Users\Guardian45\PycharmProjects\al_ko_v4\static_dev)
明白了路径。但仍然没有任何效果。打开 ttf 文件时,会发生 TTFError。搜索表明这是一个缺陷,似乎尚未修复。带有字体元数据的东西……完成。我使用了pdfkit。