2017-12-11 21 views
2

私はdjangoアプリを持っています。私はdjangoビューからpdfを作成したいと思います。 私はweasyprintを使用しています。何らかの理由でカスタムフォントを受け入れません。 フォントurlが動作していて、同じhtmlを同じフォントフェースでレンダリングすると、正しいフォントが表示されますが、pdfは正しく表示されません。私もbase64フォントストリングを試しましたが、運はありません。私のレンダーコードはweasyprintでカスタムフォントを使う方法

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 

私は間違っていますか?

答えて

1

あなたが文書で読むことができるよう:

WeasyPrintは(広くWOFF2以外を使用していたすべての 形式)のFreeTypeで扱うすべてのフォント形式をサポートする必要があります

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

+0

ながらこのリンクは質問に答えるかもしれませんが、答えの本質的な部分をここに含めて参照用のリンクを提供する方が良いでしょう。リンクされたページが変更された場合、リンクのみの回答は無効になります。 - [レビューから](/レビュー/低品質の投稿/ 18240619) – yivi

関連する問題