首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用PyFPDF制作内联粗体文本?

如何使用PyFPDF制作内联粗体文本?
EN

Stack Overflow用户
提问于 2020-03-18 09:47:39
回答 2查看 1.2K关注 0票数 1
代码语言:javascript
复制
from fpdf import FPDF

pdf1 = FPDF

pdf1.multi_cell(0, 5, 'This is my disclaimer. THESE WORDS NEED TO BE BOLD. These words do not need to be bold.', 0, 0, 'L')

pdf1.output("sample.pdf")
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-03-18 13:45:47

我对php使用fpdf,也有同样的需求。但正如奥利维尔在评论中提到的那样,这是不可能的。所以我写了一个快速的解决方案,我在下面编写了一个粗略的python实现。也许不是最漂亮的解决方案,但它可能为你服务。

代码语言:javascript
复制
from fpdf import FPDF

class FPDF_bold_multicell(FPDF):

    def write_multicell_with_styles(self,max_width, cell_height, text_list):
        startx = self.get_x()
        self.set_font('Arial', '', 16)

        #loop through differenct sections in different styles
        for text_part in text_list:
            #check and set style
            try:
                current_style = text_part['style']
                self.set_font('Arial', current_style, 16)
            except KeyError:
                self.set_font('Arial', '', 16)

            #loop through words and write them down
            space_width = self.get_string_width(' ')
            for word in text_part['text'].split(' '):
                current_pos = self.get_x()
                word_width = self.get_string_width(word)
                #check for newline
                if (current_pos + word_width) > (startx + max_width):
                    #return 
                    self.set_y(self.get_y() + cell_height)
                    self.set_x(startx)
                pdf1.cell(word_width, 5, word)
                #add a space
                self.set_x(self.get_x() + space_width)



pdf1 = FPDF_bold_multicell()
pdf1.add_page()

text_list = [{'text':'This is my disclaimer.'},
            {'style':'B','text':'THESE WORDS NEED TO BE BOLD.'},
            {'text':'These words do not need to be bold.'},]

pdf1.write_multicell_with_styles(50, 5, text_list)
pdf1.output("sample.pdf")
票数 2
EN

Stack Overflow用户

发布于 2022-11-03 15:15:13

我尝试过这个解决方案,它是有帮助的。我在注释#检查换行符之后添加了一个新命令。

代码语言:javascript
复制
#check for newline
if max_width == 0:
    max_width = self.w - self.r_margin - self.x

因此,此命令将验证宽度,如果was 0将使用all行。

代码语言:javascript
复制
from fpdf import FPDF

class FPDF_bold_multicell(FPDF):

def write_multicell_with_styles(self,max_width, cell_height, text_list):
    startx = self.get_x()
    self.set_font('Arial', '', 16)

    #loop through differenct sections in different styles
    for text_part in text_list:
        #check and set style
        try:
            current_style = text_part['style']
            self.set_font('Arial', current_style, 16)
        except KeyError:
            self.set_font('Arial', '', 16)

        #loop through words and write them down
        space_width = self.get_string_width(' ')
        for word in text_part['text'].split(' '):
            current_pos = self.get_x()
            word_width = self.get_string_width(word)
            #check for newline
            if max_width == 0:
                max_width = self.w - self.r_margin - self.x
            if (current_pos + word_width) > (startx + max_width):
                #return 
                self.set_y(self.get_y() + cell_height)
                self.set_x(startx)
            pdf1.cell(word_width, 5, word)
            #add a space
            self.set_x(self.get_x() + space_width)


pdf1 = FPDF_bold_multicell()
pdf1.add_page()

text_list = [{'text':'This is my disclaimer.'},
        {'style':'B','text':'THESE WORDS NEED TO BE BOLD.'},
        {'text':'These words do not need to be bold.'},]

pdf1.write_multicell_with_styles(0, 5, text_list)
pdf1.output("sample.pdf")
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60736940

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档