【问题标题】:How to use custom font with weasyprint如何在 weasyprint 中使用自定义字体
【发布时间】:2019-03-13 13:17:27
【问题描述】:

我有一个 django 应用程序,我想从我的 django 视图创建一个 pdf。 我使用 weasyprint,由于某种原因它不接受我的自定义字体。 字体 url 正在工作,当我使用相同的字体呈现相同的 html 时,我看到了正确的字体,但我的 pdf 使用错误的字体呈现。我也尝试了base64字体字符串,但没有运气。我的渲染代码是:

import os
from weasyprint import HTML, CSS
from weasyprint.fonts import FontConfiguration

from django.conf import settings
from django.http import HttpResponse
from django.template.loader import get_template
from django.urls import reverse


def render_to_pdf(template_src, context_dict={}):
    font_config = FontConfiguration()
    font_string = '''
        @font-face {
          font-family: 'Titillium Web';
          font-style: normal;
          font-weight: 300;
          src: local('Titillium Web Light'), local('TitilliumWeb-Light'), url('http://X.X.X.X:8000/static/fonts/titillium_web.woff2') format('woff2');
        }
        *, div {font-family: 'Titillium Web';}
    '''

    template = get_template(template_src)
    rendered_html  = template.render(context_dict)
    pdf_file = HTML(string=rendered_html).write_pdf(stylesheets=[
        CSS(settings.BASE_DIR +  '/gui/executive_summary.css'),
        CSS(string=font_string)],font_config=font_config)
    response = HttpResponse(pdf_file, content_type='application/pdf')
    response['Content-Disposition'] = 'filename="report.pdf"'
    return response

知道我做错了什么吗?

【问题讨论】:

    标签: python django weasyprint


    【解决方案1】:

    您可以在文档中阅读:

    WeasyPrint 应该支持 FreeType 处理的任何字体格式(任何 广泛使用的格式WOFF2除外)

    SRC:http://weasyprint.readthedocs.io/en/latest/features.html#fonts

    【讨论】:

    • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接答案可能会失效。 - From Review
    • 我同意@yivi 的直接链接并没有真正回答问题,只有解决问题的部分应该作为答案包含在内,更不用说 weasyprint 文档并没有真正提供确切的解决方案字体问题
    • 源网站似乎已更改,不再包含有关 WOFF2 的异常。尽管如此,WOFF2 似乎仍然不起作用。有谁知道现在是否支持?
    • woff2_compress 是一个将 WOFF2 字体转换为 TrueType (.ttf) 字体的工具。见github.com/google/woff2。然后,您可以在 CSS 中使用 src: url(file://path/to/truetype/yourfont.ttf) 行。
    【解决方案2】:

    有两种方法 1)在线上传字体并将链接粘贴到url中。 例如:@font-face { font-family: Gentium; src: url(http://example.com/fonts/Gentium.otf); }

    2) 如果您想使用本地目录中的字体。 例如:@font-face{ font-family: Gothan Narrow; src: url(file:///home/javed/Downloads/fonts/GothamNarrow-Light.otf) }

    【讨论】:

    • 添加绝对路径 @font-face { font-family: kalpurush; src:url(文件:///mnt/d/djangoprojects/dj-boot-modal/librarydb/static/librarydb/fonts/kalpurush.ttf); } 解决了我的问题。
    【解决方案3】:

    如果你的 CSS 中有 @font-face 规则,你必须创建一个 FontConfiguration对象:

    fromweasyprintimport HTML, CSS
    from weasyprint.fonts import FontConfiguration
    
    font_config = FontConfiguration()
    html = HTML(string='<h1>The title</h1>')
    css = CSS(string='''
        @font-face {
            font-family: Gentium;
            src: url(http://example.com/fonts/Gentium.otf);
        }
        h1 { font-family: Gentium }''', font_config=font_config)
    html.write_pdf(
        '/tmp/example.pdf', stylesheets=[stylesheet],
        font_config=font_config)
    

    https://weasyprint.readthedocs.io/en/stable/tutorial.html?highlight=FontConfiguration

    【讨论】:

    • from weasyprint.text.fonts import FontConfiguration
    猜你喜欢
    • 2013-08-15
    • 1970-01-01
    • 2010-11-23
    • 2017-01-01
    • 2011-08-04
    • 2011-08-02
    • 2013-12-14
    • 2012-06-19
    • 2014-01-23
    相关资源
    最近更新 更多