2017-07-06 8 views
0

ここに私のコードです。私の最初の関数はstyleの配列を作成するために、/Lib/site-packages/reportlab/lib/styles.pyソースコードに基づいています。カモノはどのように太字と斜体を「推測」しますか?

def create_styles(p_tuples): 
    retour = StyleSheet1() 
    parent = None 
    for p_name, font_name, font in p_tuples: 
     # (!) change path if Linux: 
     ttf_file = "C:/Windows/Fonts/{}.ttf".format(font) 
     pdfmetrics.registerFont(TTFont(font_name, ttf_file)) 
     if parent is None: 
      p = ParagraphStyle(name=p_name, 
           fontName=font_name, 
           fontSize=10, 
           leading=12) 
      retour.add(p) 
      parent = p 
     else: 
      retour.add(ParagraphStyle(name=p_name, 
             parent=parent, 
             fontName=font_name, 
             fontSize=10, 
             leading=12)) 
    return retour 

その後、私は私のインストールされているフォントと私自身の配列を作成:

def render_to_response(self, context, **response_kwargs): 
    # this is a response for Django, but the question is about styles 
    response = HttpResponse(content_type='application/pdf; charset=utf-8') 
    # ! Hint : no filename -> the browser extracts it from the URL! 
    # -> create URLs like http://pdfreportlab.com/extract/myfilename.pdf 
    # this is the way to go to have 100% working UTF-8 filenames! 
    response['Content-Disposition'] = 'attachment; filename=""' 

    my_styles = self.create_styles([ 
     ('ms-regular', 'montserrat-regular', 
     'Montserrat-Regular'), 
     ('ms-black', 'montserrat-black', 
     'Montserrat-Black'), 
     ('ms-black-italic', 'montserrat-black-italic', 
     'Montserrat-BlackItalic'), 
     ('ms-bold', 'montserrat-bold', 
     'Montserrat-Bold'), 
     ('ms-bold-italic', 'montserrat-bold-italic', 
     'Montserrat-BoldItalic'), 
    ]) 
    doc = SimpleDocTemplate(response) 
    elements = [] 
    c = canvas.Canvas(response, pagesize=A4,) 
    for idx in my_styles.byName: 
     p = Paragraph("Hello World <i>italic</i> <b>bold</b>", 
         style=my_styles[idx]) 
     width, height = p.wrapOn(c, A4[0], A4[1]) 
     elements.append(p) 
    doc.build(elements) 
    return response 

すべてが非常に(を除いて取り組んでいます厄介な事実)、<i></i><b></b>のタグは無視されます。スタイル内の現在のフォントのみを使用します。

どのようにしてタグを考慮に入れて、最終的にはテキスト自体のタグでスタイルを取得するようにコードを変更できますか?

答えて

1

フォントを自動的に選択したい場合は、フォントファミリを登録する必要があります。フォントバリエーションごとに異なるスタイルを作成しているので、<i><b>タグは効果がありません。渡されたスタイルが単一のフォントを参照するため、それらに使用するフォント。ここで

は、フォントファミリを使用してスタイルを構築する方法は次のとおりです。

from reportlab.lib.styles import ParagraphStyle 
from reportlab.pdfbase.pdfmetrics import registerFont, registerFontFamily 
from reportlab.pdfbase.ttfonts import TTFont 


def create_paragraph_style(name, font_name, **kwargs): 
    ttf_path = "C:/Windows/Fonts/{}.ttf" 
    family_args = {} # store arguments for the font family creation 
    for font_type in ("normal", "bold", "italic", "boldItalic"): # recognized font variants 
     if font_type in kwargs: # if this type was passed... 
      font_variant = "{}-{}".format(font_name, font_type) # create font variant name 
      registerFont(TTFont(font_variant, ttf_path.format(kwargs[font_type]))) 
      family_args[font_type] = font_variant # add it to font family arguments 
    registerFontFamily(font_name, **family_args) # register a font family 
    return ParagraphStyle(name=name, fontName=font_name, fontSize=10, leading=12) 

次に、あなたのようにあなたの段落スタイルを作成することができます。

あなたがそれらとTTFファイルを持っていることを、当然のことながら、仮定
pstyle = create_paragraph_style("ms", "montserrat", 
           normal="Montserrat-Regular", 
           bold="Montserrat-Bold", 
           boldItalic="Montserrat-BoldItalic") 

あなたのフォントディレクトリの名前。

あなたはその後、(あなたが親の継承がしたい場合は特に - ParagraphStyle作成に転送するために、あなたが引数として追加してください)あなたのスタイルシートに追加することができ

または直接としてそれを使用します(

p = Paragraph("Hello World <i>italic</i> <b>bold</b> <b><i>boldItalic</i></b>", style=pstyle) 

スタンドアロンのイタリック体は、我々がそれを家族で定義しなかったので影響を受けません)。

+0

'(" normal "、" bold "、" italic "、" boldItalic "):#認識されたフォントの変形はすべてのシステムでハードコードされていますか?デファクトスタンダードですか? –

+0

@OlivierPons - 'reportlab.pdfbase.pdfmetrics.registerFontFamily()'関数のハードコーディングされた引数です(署名は 'registerFontFamily(family、normal = None、bold = None、italic = None、boldItalic = None)')。 ) - 上記のコードでは短縮形ですので、ある引数セットから別の引数セットに名前を変換/渡す必要はありません。 – zwer

+0

ハードコードされたこのソリューションを試してみると、http://two.pairlist.net/pipermail/reportlab-users/2010-October/009795.htmlのようになります。私はあなたの解決策が動作しないことを知った(これはバグだと思うので)。 (1)あなたのループから 'normal'を削除し、(2)ループの直前にこの行を追加してください:' registerFont(TTFont(font_family、ttf_path.format(kwargs ['normal'] ))) '(' 'normal''は常にkwargsに渡されると推論します)。これのために3時間。 –

関連する問題