Reportlab中的Reportlab动态信息

时间:2016-03-27 09:40:07

标签: python reportlab platypus

我必须使用ReportLab将长格式的文本文档转换为PDF。我的目标是逐页阅读文本文件(文本文件中的1页是可变长度并由换页确定)。然后,对于每个页面,我必须使用页面模板的onPage功能编写页面的静态部分,同时将动态部分插入到pdf文件的一个或多个页面中。

问题是我的onPage函数在我为文档调用build之前未被调用。在阅读文本页面时,动态创建pdf页面的替代解决方案是什么。

以下是尝试编写页面静态内容的脚本部分。

#Globals
formlines = []
header = []
footer = []
detail_lines = []
def processForm():
    # First 15 lines header
    # Then variable number of detail lines
    # Last 15 line footer
  header = formlines[0:14]
  print "Processing form\n"
  footer = formlines[-15:-1]
  detail_lines = formlines[15:-15]

def firstPgCanv(c, doc):
  # Process header
  pract_addr = header[0][0:43] + "\n" + header[1] + "\n" + header[2][0:43]  
  remit_to = header[8][44:] + "\n" + header[9][44:] + "\n" + header[10][44:]
  guar_addr = header[8][1:42] + "\n" + header[9][1:42] + "\n" + header[10][1:42]

  c.saveState()
  c.translate(.3 * inch, 0 * inch)

  tx = c.beginText(.3 * inch,height- .35 * inch)
  tx.textLines(header_addr)
  c.drawText(tx)

  curY = curY - gap 
# Create text object
  tx = c.beginText(.3 * inch, curY * inch)
  tx.textLines(guar_addr)
  c.drawText(tx)

  tx = c.beginText(4.3 * inch, curY * inch)
  tx.textLines(remit_to)
  c.drawText(tx)

  c.restoreState()

def main(argv=None):
  if argv is None:
    argv = sys.argv

  args = sys.argv[1:]

  #Open source file
  src_file  = sys.argv[1]
  dest = sys.argv[2]
  dest_file = os.path.join(os.path.dirname(src_file), dest);

  f=open(src_file, 'r')
  #Document Template
  doc = BaseDocTemplate(dest_file,
                        pagesize=letter,
                        leftMargin=.3*inch,
                        rightMargin= .1 * inch,
                        topMargin= .1 * inch,
                        bottomMargin=.3 * inch,
                        showBoundary=1)

  # Frames
  frameT = Frame(doc.leftMargin + 2*inch, doc.bottomMargin, doc.width - 2.01*inch, doc.height - 4.1*inch, id='normal', showBoundary=0)
  frameB = Frame(doc.leftMargin+2, doc.bottomMargin, 7.5*inch, 10*inch, id='small', showBoundary=1)

  # Page Templates
  doc.addPageTemplates([PageTemplate(id='First',frames=frameT,onPage=firstPgCanv),
                        PageTemplate(id='Later',frames=frameB,onPage=othPgCanv)
                      ])

  Elements = []
  # Process the input file
  while 1:
    ln = f.readline()
    if len(ln) == 0:    # EOF
      break

    s = chomp(ln)

    ff = s.find("\f")    
    if ff != -1:        # \f found along with first line of next form
      frag = s.split("\f")
      final = frag.pop()
      formlines.append(final)
      processForm()
      Elements.append(NextPageTemplate('First'))
      Elements.append(PageBreak)
      # Here I will write few more pages of page Template 'Later'
    else:
      formlines.append(s)

# EOF -- close and flush last page 
  f.close()  
  doc.build(Elements)

if __name__ == "__main__":
    sys.exit(main())

1 个答案:

答案 0 :(得分:0)

经过一番研究后,我得出结论,没有办法动态处理它。剩下的唯一解决方法是将整个文件访问到列表中。然后onPage页面模板的功能使用全局变量来跟踪页面#并根据页面#访问相应的列表行。

相关问题