如何修复FPDF生成PDF时的文本重叠问题?
FPDF生成PDF文本重叠问题修复方案
问题原因
- 重复写入内容:原
investment_summary函数中,先通过pdf.write(5, text)写入全部文本,随后又遍历文本行,重复写入所有以-开头的列表项,导致内容叠加重叠。 - 未处理行坐标重置:列表项缩进后,后续非列表项未重置x坐标,导致排版混乱。
- 分页逻辑缺失:当内容超出单页容量时,未触发自动分页,底部内容溢出重叠。
修复步骤
- 移除重复的全量文本写入代码,仅保留逐行处理逻辑,确保每行内容只输出一次。
- 针对不同行类型(普通文本/列表项)设置正确的x坐标:普通文本重置为默认页边距位置,列表项缩进至15。
- 确保FPDF自动分页功能生效,
write方法默认支持自动分页,只需保证行高参数合理。 - 调整行间距参数,优化内容排版,避免拥挤。
修改后的完整代码
from fpdf import FPDF def investment_summary(pdf, text): pdf.set_font("Arial", size=8) # 移除重复的全量写入,改为逐行处理 for point in text.splitlines(): if not point.strip(): # 跳过空行并添加换行 pdf.ln(5) continue if point.startswith("-"): pdf.set_x(15) pdf.write(5, point.strip()) else: pdf.set_x(10) # 重置为默认左侧边距 pdf.write(5, point.strip()) pdf.ln(3) # 每行后添加小间距 def create_report(county_and_state, llm_text, analytics_location_path=None): pdf = FPDF() pdf.add_page() # 设置页边距,确保内容在页面范围内 pdf.set_left_margin(10) pdf.set_right_margin(10) pdf.set_text_color(r=50,g=108,b=175) pdf.set_font('Arial', 'B', 18) pdf.cell(w=0, h=10, txt="Verus-AI: " + county_and_state, ln=1, align='C') pdf.set_font('Arial', 'B', 16) pdf.cell(w=0, h=10, txt="Investment Summary", ln=1, align='L') investment_summary(pdf, llm_text) pdf.output(f'./example1.pdf', 'F') # 测试文本 text = """ Branch County is a rural county in southwestern Michigan, with a population of about 45,000. The county seat is Coldwater, which is also the largest city in the county. The county has a diverse economy, with agriculture, manufacturing, health care, and education as the main sectors. The county is also home to several state parks, lakes, and recreational trails. Pros of investing in Branch County, MI: - Low property taxes and affordable housing prices, compared to the state and national averages. - High rental demand and low vacancy rates, due to the lack of new construction and the presence of several colleges and universities in the county. - Potential for appreciation and cash flow, as the county has a stable and growing population, a low unemployment rate, and a strong school system. - Opportunity to diversify your portfolio and hedge against inflation, by investing in different types of properties, such as single-family homes, multi-family buildings, mobile homes, and land. Cons of investing in Branch County, MI: - Limited access to public transportation and major highways, which may affect the mobility and convenience of tenants and owners. - High crime rates and low quality of education in some areas, which may deter some potential renters and buyers. - Weather-related risks, such as harsh winters, floods, and tornadoes, which may require additional maintenance and insurance costs. - Lack of economic and demographic diversity, which may limit the future growth and demand for real estate in the county. Overall investment thesis: Branch County, MI is a viable option for real estate investors who are looking for a long-term, passive, and income-generating strategy. The county offers a combination of low-cost, high-demand, and appreciating properties, with a relatively low risk of market fluctuations and downturns. However, investors should also be aware of the potential drawbacks and challenges of investing in a rural and homogeneous market, and be prepared to deal with the environmental and social issues that may arise. Investors should also conduct thorough due diligence and research on the specific locations, neighborhoods, and properties that they are interested in, and seek professional advice and guidance as needed. """ # 调用测试 create_report("Branch County, MI", text)
内容的提问来源于stack exchange,提问作者Wolfy
相关产品推荐
相关产品推荐

