我必須去檢索一些我的代碼,但我似乎記得必須通過測量當前的X/Y位置來計算該值,然後根據我使用的任何保證金計算該值,然後確定是否更多信息可能適合或如果我需要一個新的頁面。我的項目是包裝長文本塊,這是相似的,但不是很相似。稍後我會在這裏更新一些代碼。
def newline(self, options, text = ''):
if getattr(self, 'lpp', None) == self.lines[self.pages]:
self.newpage()
if getattr(self, 'y', None) > self.h - self.bm * inch:
self.newpage()
在這種情況下,我不得不爲LPP屬性(每頁行數),這可能已經確定,所以我第一次檢查,看看是否該值存在,如果是這樣,如果我在的行數當前頁面。如果每頁的總行數沒有限制,那麼我會測試我的Y位置和底部邊距是多少。如有必要,在頁面中打補丁。這裏有一些遺漏,但這是一個普遍的想法。
def newline(self, options, text = ''):
if getattr(self, 'lpp', None) == self.lines[self.pages]:
self.newpage()
if getattr(self, 'y', None) > self.h - self.bm * inch:
self.newpage()
self.addLine()
self.putText(self.x, self.h - self.y, text)
def putText(self, x, y, text):
# If we actually place some text then we want to record that.
if len(text.strip()) > 0 and not self.hasText[self.pages]:
self.hasText[self.pages] = True
# Something here to handle word wrap.
if self.wrap:
lines = self._breakScan(text)
if len(lines) > 1:
self.c.drawString(x, y, lines[0])
self.newline('', ' '.join(lines[1:]))
elif lines:
self.c.drawString(x, y, lines[0])
else:
self.c.drawString(x, y, text)
在這裏,self.c
是我的畫布。我記錄了我在頁面上放置了多少行,因爲有時我們會重新包裝可能包含分頁符的文檔,所有這些都在我們的自定義標記中。
我想我明白了。換行是段落的一種方法嗎?它什麼時候被叫? – jammon 2011-06-10 14:29:36
@jammon - 對不起,我可以更詳細地解釋我的方法。在這個例子中,Self是一個自定義類。它將各種命令從自定義標記語法映射到reportlab中相應的api調用。他們的庫中可能沒有'newline()'方法。我會添加一些額外的代碼來進一步演示。 – 2011-06-10 16:50:48
的確非常感謝。我會盡力把我的大腦包裹起來。 Reportlab對初學者非常有用,但肯定有一些邊緣和任務,解決方案很不明顯。 – jammon 2011-06-11 14:44:49