python fpdf中multi_cell实现自动换行

3 篇文章 0 订阅

python 使用fpdf中的write不能实现自动换行,而multi_cell可以实现自动换行,具体看下面的代码

# Import FPDF class
from fpdf import FPDF

# Create instance of FPDF class
# Letter size paper, use inches as unit of measure
pdf=FPDF(format='letter', unit='in')

# Add new page. Without this you cannot create the document.
pdf.add_page()

# Remember to always put one of these at least once.
pdf.set_font('Times','',10.0) 

# Long meaningless piece of text
loremipsum = """Lorem ipsum dolor sit amet, vel ne quando dissentias. \
Ne his oporteat expetendis. Ei tantas explicari quo, sea vidit minimum \
menandri ea. His case errem dicam ex, mel eruditi tibique delicatissimi \
ut. At mea wisi dolorum contentiones, in malis vitae viderer mel.

Vis at dolores ocurreret splendide. Noster dolorum repudiare vis ei, te \
augue summo vis. An vim quas torquatos, electram posidonium eam ea, eros \
blandit ea vel. Reque summo assueverit an sit. Sed nibh conceptam cu, pro \
in graeci ancillae constituto, eam eu oratio soleat instructior. No deleniti \
quaerendum vim, assum saepe munere ea vis, te tale tempor sit. An sed debet \
ocurreret adversarium, ne enim docendi mandamus sea.
"""

effective_page_width = pdf.w - 2*pdf.l_margin

pdf.set_font('Times','B',10.0)
pdf.cell(1.0,0.0, 'Without multi_cell using effective page width:')
pdf.ln(0.25)
pdf.set_font('Times','',10.0)
# Cell is as wide as the effective page width
pdf.cell(effective_page_width, 0.0, loremipsum)
pdf.ln(0.5)
pdf.set_font('Times','B',10.0)
pdf.cell(1.0,0.0, 'Using multi_cell and effective page width:')
pdf.ln(0.25)

pdf.set_font('Times','',10.0)
# Cell is as wide as the effective page width
# and multi_cell requires declaring the height of the cell.
pdf.multi_cell(effective_page_width, 0.15, loremipsum)
pdf.ln(0.5)

# Cell half as wide as the effective page width
# and multi_cell requires declaring the height of the cell.
pdf.set_font('Times','B',10.0)
pdf.cell(1.0,0.0, 'Using multi_cell and half the effective page width:')
pdf.ln(0.25)


pdf.set_font('Times','',10.0)
pdf.multi_cell(effective_page_width/2, 0.15, loremipsum)
pdf.ln(0.5)

pdf.output('multi_cell.pdf','F')

在这里插入图片描述
这篇文章是否对你有帮助,如果有帮助愿意请博主喝杯咖啡哈

在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
非常抱歉,我提供的示例代码有误,pdfium库确实没有定义FPDF_PRINT_SETTINGS结构体。正确的做法是使用FPDF_SetPrintOption函数来设置打印选项。以下是修改后的示例代码: ```c++ #include <windows.h> #include "fpdfview.h" #include "fpdf_print.h" int main() { // 加载PDF文件 FPDF_DOCUMENT doc = FPDF_LoadDocument("test.pdf", nullptr); if (!doc) { printf("Failed to load PDF file!\n"); return -1; } // 获取打印机信息 PRINTER_INFO_2 printerInfo; ZeroMemory(&printerInfo, sizeof(printerInfo)); printerInfo.pPrinterName = L"Printer Name"; if (!GetPrinter(nullptr, 2, (LPBYTE)&printerInfo, 0, &dwNeeded)) { printf("Failed to get printer information!\n"); FPDF_CloseDocument(doc); return -1; } // 创建打印作业 HDC printerDC = CreateDC(nullptr, printerInfo.pDriverName, printerInfo.pPrinterName, nullptr); if (!printerDC) { printf("Failed to create printer DC!\n"); FPDF_CloseDocument(doc); return -1; } DOCINFO docInfo; ZeroMemory(&docInfo, sizeof(docInfo)); docInfo.cbSize = sizeof(docInfo); docInfo.lpszDocName = L"PDF Print Job"; StartDoc(printerDC, &docInfo); StartPage(printerDC); // 设置打印机属性 int pageCount = FPDF_GetPageCount(doc); FPDF_SetPrintOption(printerDC, FPDF_PRINTOPT_PAGENUM, 1); FPDF_SetPrintOption(printerDC, FPDF_PRINTOPT_SCALETYPE, FPDF_PRINTSCALING_NONE); FPDF_SetPrintOption(printerDC, FPDF_PRINTOPT_ORIENTATION, FPDF_PRINTPORTRAIT); FPDF_SetPrintOption(printerDC, FPDF_PRINTOPT_COPIES, 1); // 逐页打印 for (int i = 0; i < pageCount; i++) { FPDF_PAGE page = FPDF_LoadPage(doc, i); if (!page) { printf("Failed to load page %d!\n", i+1); break; } int pageWidth = FPDF_GetPageWidth(page); int pageHeight = FPDF_GetPageHeight(page); FPDF_PRINT_SETTINGS printSettings; ZeroMemory(&printSettings, sizeof(printSettings)); printSettings.structSize = sizeof(printSettings); printSettings.pageInfo.width = pageWidth; printSettings.pageInfo.height = pageHeight; printSettings.pageInfo.flags = FPDF_PRINTING; FPDF_PrintPage(printerDC, page, &printSettings); FPDF_ClosePage(page); } // 结束打印作业 EndPage(printerDC); EndDoc(printerDC); DeleteDC(printerDC); // 释放资源 FPDF_CloseDocument(doc); return 0; } ``` 在以上示例,使用了FPDF_SetPrintOption函数来设置打印选项,包括打印起始页码、缩放比例、方向和份数等。在逐页打印时,使用FPDF_PrintPage函数打印当前页,并传入FPDF_PRINT_SETTINGS结构体,设置打印页面大小和标志。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值