如何知道框架的结尾以创建一个新的reportlab [英] how to know the end of a frame to create a new one reportlab
问题描述
我使用reportlab构建了一个pdf文件,以便更好地控制页面(颜色,填充,返回行...).我创建了一个框架.
I built a pdf using reportlab, to have more controle one the page (colors- padding- return to line...) I created a frame.
问题在于,我的文本来自数据库,需要多个页面.
the issue is that, my text is coming from a database and it need more than one page.
我想知道如何辨别页面/框架的结尾,我应该为其余文本创建一个新的结尾.
I want to know how to tell that its the end of the page/frame and I should create a new one for the rest of text.
import MySQLdb
import pprint
import sys
import csv
from datetime import *
from reportlab.pdfgen import canvas
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.lib.pagesizes import landscape, letter, A4
from reportlab.lib.units import cm, inch
from reportlab.platypus import Image, Paragraph, Frame, KeepInFrame
styles = getSampleStyleSheet()
styleN = styles['Normal']
styleH = styles['Heading1']
story = []
pdf_file_name = 'report_.pdf'
c = canvas.Canvas(pdf_file_name)
f = Frame(0.5*inch, inch, 7*inch, 10.5*inch, showBoundary=1)
story.append(Paragraph("<font size='12' face='times' color='blue'>>SOME TEXT!</font>",styleN))
story.append(Paragraph("<font size='10' face='times'>SOME TEXT</font>", styleN) )
story.append( Paragraph('MORE TEXT HERE', styleN ) )
f.addFromList(story,c)
del story [:]
c.save()
推荐答案
根据reportlab API,您不应直接使用Frame
,而应使用诸如Paragraph
之类的Flowable
和它将为您创建和操作框架.例如,您应该使用f.addFromList(story,c)
According to the reportlab API, you should not be using a Frame
directly, rather you should be using a Flowable
such as Paragraph
instead and that will create and manipulate the Frames for you. For example, instead of f.addFromList(story,c)
you ought to use
for p in story:
while f.add(p, c) == 0:
f.split(p, c)
f = Frame(0.5*inch, inch, 7*inch, 10.5*inch, showBoundary=1)
这样,f.add
成功返回1,当它不能容纳p
时返回0,这种情况发生时,我们使用f.split
尽可能多地从p
提取到填充 f
,而不是用新的框架替换f
.
This way, f.add
will return 1 on success and 0 when it can't fit p
into it, when that happens we use f.split
to extract as much as possible from p
to fill up f
than replace f
with a new Frame.
这篇关于如何知道框架的结尾以创建一个新的reportlab的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!