【RPA】PDF生成器FPDF(Python篇)

FPDF 简介

FPDF 是一个 可用于创建 PDF 格式文件的纯 PHP 类,也就说不需要借助 PDFlib 库。FPDF首部的F代表自由,自由软件的自由。
Python中所用到的FPDF是基于PHP版本的FPDF。

安装

pip install fpdf

样例

列举以下示例,描绘如何通过代码生成PDF文件。

  1. 添加单行文字
from fpdf import FPDF

pdf = FPDF()
pdf.add_page()
pdf.set_font('Arial', 'B', 16)
pdf.cell(40, 10,'Hello, World!')
pdf.output('tutorial_01.pdf', 'F')

在这里插入图片描述

  1. 添加标题、脚注以及照片
from fpdf import FPDF

class PDF(FPDF):
    def header(self):
        # Logo
        self.image('logo_fb.jpg', 10, 8, 33)
        # Arial bold 15
        self.set_font('Arial', 'B', 15)
        # Move to the right
        self.cell(80)
        # Title
        self.cell(30, 10, 'Title', 1, 0, 'C')
        # Line break
        self.ln(20)

    # Page footer
    def footer(self):
        # Position at 1.5 cm from bottom
        self.set_y(-15)
        # Arial italic 8
        self.set_font('Arial', 'I', 8)
        # Page number
        self.cell(0, 10, 'Page ' + str(self.page_no()) + '/{nb}', 0, 0, 'C')

# Instantiation of inherited class
pdf = PDF()
pdf.alias_nb_pages()
pdf.add_page()
pdf.set_font('Times', '', 12)
for i in range(1, 41):
    pdf.cell(0, 10, 'Printing line number ' + str(i), 0, 1)
pdf.output('tutorial_02.pdf', 'F')

在这里插入图片描述
3. 中文支持

from fpdf import FPDF

pdf = FPDF()
pdf.add_page()
pdf.add_font('kaiti','',"C:\\Windows\\Fonts\\stkaiti.ttf",True)  
# 注意:也可将 stkai.ttf 放在与本脚本的同一文件夹内
# pdf.add_font('kaiti','',"stkaiti.ttf",True)  
pdf.set_font('kaiti','',16)
pdf.cell(40, 10, "你好,世界!") 
pdf.output("tutorial_05.pdf")

在这里插入图片描述

更进一步

  • 0
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值