编辑
2025-11-11
Python
00

Python PDF Generation Practical Guide: fpdf Library Makes Document Creation Simple

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.

🔍 Problem Analysis

Why Choose fpdf?

In the Python PDF generation field, common libraries include:

  • reportlab: Powerful but steep learning curve
  • weasyprint: Many dependencies, complex configuration in Windows environment
  • fpdf: Pure Python implementation, lightweight, concise API

Core advantages of fpdf:

  • ✅ Pure Python implementation, no additional dependencies required
  • ✅ Simple API design, quick to learn
  • ✅ Supports Chinese fonts
  • ✅ Low memory usage, excellent performance

💡 Solution

🚀 Environment Setup

First install the fpdf library:

Bash
pip 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 Usage Pattern

Basic fpdf usage workflow:

Python
from 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')

image.png

编辑
2025-11-11
Python
00

在日常的Python开发中,我们经常会遇到需要生成PDF文档的场景:自动化报告、数据导出、发票生成、证书制作等。虽然市面上有很多PDF生成库,但对于Windows开发者来说,fpdf库以其轻量级、易上手的特点成为了首选方案。

本文将通过实战案例,带你从零开始掌握fpdf库的使用,解决Python生成PDF的常见问题。无论你是想要生成简单的文本报告,还是制作包含图表、表格的复杂文档,这篇文章都能为你提供完整的解决方案。

🔍 问题分析

为什么选择fpdf?

在Python的PDF生成领域,常见的库有:

  • reportlab:功能强大但学习曲线陡峭
  • weasyprint:依赖较多,Windows环境配置复杂
  • fpdf:纯Python实现,轻量级,API简洁

fpdf的核心优势:

  • ✅ 纯Python实现,无需额外依赖
  • ✅ API设计简洁,上手快
  • ✅ 支持中文字体
  • ✅ 内存占用小,性能优异

💡 解决方案

🚀 环境准备

首先安装fpdf库:

Bash
pip install fpdf2

注意:建议使用fpdf2而不是原版fpdf,因为fpdf2修复了很多bug并添加了新特性。

🎨 基础使用模式

fpdf的基本使用流程:

Python
from 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')

image.png

编辑
2025-11-11
C#
00

Detailed Guide to Drawing Circles and Ellipses with SkiaSharp

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.

Environment Setup

First, you need to install SkiaSharp related packages through NuGet Package Manager:

  • SkiaSharp
  • SkiaSharp.Views
  • SkiaSharp.Views.WindowsForms

Basic Knowledge

In SkiaSharp:

  • Use SKCanvas.DrawCircle() method to draw circles
  • Use SKCanvas.DrawOval() method to draw ellipses
  • Use SKPaint class to set drawing styles (color, line width, etc.)
编辑
2025-11-11
C#
00

SkiaSharp 是一个强大的 2D 图形库,可以用来绘制各种图形。本文将详细介绍如何使用 SkiaSharp 在 WinForms 应用程序中绘制圆形和椭圆。

环境准备

首先需要通过 NuGet 包管理器安装 SkiaSharp 相关包:

  • SkiaSharp
  • SkiaSharp.Views
  • SkiaSharp.Views.WindowsForms

基础知识

在 SkiaSharp 中:

  • 使用 SKCanvas.DrawCircle() 方法绘制圆形
  • 使用 SKCanvas.DrawOval() 方法绘制椭圆
  • 使用 SKPaint 类设置绘制样式(颜色、线宽等)
编辑
2025-11-11
C#
00

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.

Environment Setup

First, you need to install the following packages through NuGet Package Manager:

  • SkiaSharp
  • SkiaSharp.Views
  • SkiaSharp.Views.WindowsForms

Basic Example

Creating Basic Form

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); } } } }

image.png