In daily Python development, we frequently encounter scenarios where PDF document generation is required: automated reports, data exports, invoice generation, certificate creation, etc. While there are many PDF generation libraries available, for Windows developers, the fpdf library has become the preferred solution due to its lightweight and easy-to-use characteristics.
This article will guide you from zero to mastering the fpdf library through practical cases, solving common problems in Python PDF generation. Whether you want to generate simple text reports or create complex documents containing charts and tables, this article can provide you with complete solutions.
In the Python PDF generation field, common libraries include:
Core advantages of fpdf:
First install the fpdf library:
Bashpip install fpdf2
Note: It's recommended to use fpdf2 instead of the original fpdf, as fpdf2 has fixed many bugs and added new features.
Basic fpdf usage workflow:
Pythonfrom fpdf import FPDF
from fpdf.enums import XPos, YPos
pdf = FPDF()
pdf.add_page()
# Add SimSun font, need to provide a font alias, e.g., SimSun
pdf.add_font('SimSun', '', r'C:\Windows\Fonts\Dengb.ttf')
# Set font to the newly added SimSun
pdf.set_font('SimSun', size=12)
pdf.cell(200, 10, 'Hello, World', new_x=XPos.LMARGIN, new_y=YPos.NEXT)
pdf.output('output.pdf')

在日常的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);
}
}
}
}
