在日常的Python开发中,我们经常会遇到需要生成PDF文档的场景:自动化报告、数据导出、发票生成、证书制作等。虽然市面上有很多PDF生成库,但对于Windows开发者来说,fpdf库以其轻量级、易上手的特点成为了首选方案。
本文将通过实战案例,带你从零开始掌握fpdf库的使用,解决Python生成PDF的常见问题。无论你是想要生成简单的文本报告,还是制作包含图表、表格的复杂文档,这篇文章都能为你提供完整的解决方案。
在Python的PDF生成领域,常见的库有:
fpdf的核心优势:
首先安装fpdf库:
Bashpip install fpdf2
注意:建议使用fpdf2而不是原版fpdf,因为fpdf2修复了很多bug并添加了新特性。
fpdf的基本使用流程:
Pythonfrom fpdf import FPDF
from fpdf.enums import XPos, YPos
pdf = FPDF()
pdf.add_page()
# 添加宋体字体,需要给一个字体别名,比如 SimSun
pdf.add_font('SimSun', '', r'C:\Windows\Fonts\Dengb.ttf')
# 设置字体为刚添加的宋体
pdf.set_font('SimSun', size=12)
pdf.cell(200, 10, '你好,世界', new_x=XPos.LMARGIN, new_y=YPos.NEXT)
pdf.output('output.pdf')

SkiaSharp is a powerful 2D graphics library that can be used to draw various shapes. This article will provide a detailed introduction on how to use SkiaSharp to draw circles and ellipses in WinForms applications.
First, you need to install SkiaSharp related packages through NuGet Package Manager:
In SkiaSharp:
SKCanvas.DrawCircle() method to draw circlesSKCanvas.DrawOval() method to draw ellipsesSKPaint class to set drawing styles (color, line width, etc.)SkiaSharp 是一个强大的 2D 图形库,可以用来绘制各种图形。本文将详细介绍如何使用 SkiaSharp 在 WinForms 应用程序中绘制圆形和椭圆。
首先需要通过 NuGet 包管理器安装 SkiaSharp 相关包:
在 SkiaSharp 中:
SKCanvas.DrawCircle() 方法绘制圆形SKCanvas.DrawOval() 方法绘制椭圆SKPaint 类设置绘制样式(颜色、线宽等)SkiaSharp is the .NET version of Google's Skia graphics engine, providing powerful 2D graphics processing capabilities. This article will provide a detailed introduction on how to use SkiaSharp to draw various rectangles in WinForm applications.
First, you need to install the following packages through NuGet Package Manager:
C#using SkiaSharp;
using SkiaSharp.Views.Desktop;
namespace AppRectangle
{
public partial class Form1 : Form
{
private SKControl skControl;
public Form1()
{
InitializeComponent();
// Create SKControl control
skControl = new SKControl();
skControl.Dock = DockStyle.Fill;
skControl.PaintSurface += OnPaintSurface;
this.Controls.Add(skControl);
}
// Paint event handler
private void OnPaintSurface(object sender, SKPaintSurfaceEventArgs e)
{
// Get drawing canvas
SKCanvas canvas = e.Surface.Canvas;
// Clear canvas (using white background)
canvas.Clear(SKColors.White);
// Add drawing code here
DrawRectangles(canvas);
}
private void DrawRectangles(SKCanvas canvas)
{
// Create paint brush
using (var paint = new SKPaint())
{
// Enable anti-aliasing
paint.IsAntialias = true;
// 1. Draw basic rectangle
paint.Color = SKColors.Blue; // Set color to blue
paint.Style = SKPaintStyle.Fill; // Set fill style
canvas.DrawRect(50, 50, 200, 100, paint);
// 2. Draw stroked rectangle
paint.Color = SKColors.Red; // Set color to red
paint.Style = SKPaintStyle.Stroke; // Set to stroke style
paint.StrokeWidth = 3; // Set stroke width
canvas.DrawRect(50, 200, 200, 100, paint);
// 3. Draw rounded rectangle
paint.Color = SKColors.Green; // Set color to green
paint.Style = SKPaintStyle.Fill; // Set fill style
canvas.DrawRoundRect(50, 350, 200, 100, 20, 20, paint);
}
}
}
}

SkiaSharp 是 Google's Skia 图形引擎的 .NET 版本,提供了强大的 2D 图形处理能力。本文将详细介绍如何在 WinForm 应用程序中使用 SkiaSharp 绘制各种矩形。
首先需要通过 NuGet 包管理器安装以下包:
C#using SkiaSharp;
using SkiaSharp.Views.Desktop;
namespace AppRectangle
{
public partial class Form1 : Form
{
private SKControl skControl;
public Form1()
{
InitializeComponent();
// 创建 SKControl 控件
skControl = new SKControl();
skControl.Dock = DockStyle.Fill;
skControl.PaintSurface += OnPaintSurface;
this.Controls.Add(skControl);
}
// 绘制事件处理
private void OnPaintSurface(object sender, SKPaintSurfaceEventArgs e)
{
// 获取绘图画布
SKCanvas canvas = e.Surface.Canvas;
// 清空画布(使用白色背景)
canvas.Clear(SKColors.White);
// 在这里添加绘制代码
DrawRectangles(canvas);
}
private void DrawRectangles(SKCanvas canvas)
{
// 创建画笔
using (var paint = new SKPaint())
{
// 启用防锯齿
paint.IsAntialias = true;
// 1. 绘制基础矩形
paint.Color = SKColors.Blue; // 设置颜色为蓝色
paint.Style = SKPaintStyle.Fill; // 设置填充样式
canvas.DrawRect(50, 50, 200, 100, paint);
// 2. 绘制描边矩形
paint.Color = SKColors.Red; // 设置颜色为红色
paint.Style = SKPaintStyle.Stroke; // 设置为描边样式
paint.StrokeWidth = 3; // 设置描边宽度
canvas.DrawRect(50, 200, 200, 100, paint);
// 3. 绘制圆角矩形
paint.Color = SKColors.Green; // 设置颜色为绿色
paint.Style = SKPaintStyle.Fill; // 设置填充样式
canvas.DrawRoundRect(50, 350, 200, 100, 20, 20, paint);
}
}
}
}
