如何使用reportlab在PDF中添加总页数 [英] How to add total page count in PDF using reportlab

查看:2087
本文介绍了如何使用reportlab在PDF中添加总页数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$


response = HttpResponse(mimetype ='application / pdf')
response ['Content-Disposition'] ='attachment; filename = ANALYSIS_REPORT.pdf'
buffer = StringIO()
doc = SimpleDocTemplate(buffer)
doc.sample_no = 12345
document = []
doc.build(document,onLaterPages = header_footer)

def header_footer(canvas,doc):
canvas.saveState()

canvas.setFont(Times- Bold,11)
canvas.setFillColor(灰色)
canvas.setStrokeColor('#5B80B2')
canvas.drawCentredString(310,800,'HEADER ONE GOES HERE')
canvas.drawString(440,780,'Sample No:%s'%doc.sample_no)

canvas.setFont('Times-Roman',5)
canvas.drawString(565 ,4,页%d%doc.page)

我上面的代码我可以打包显示页码,但我的问题是如何我可以显示Y的页X,其中Y是页数,X是当前页。



我遵循这个 http://code.activestate.com/recipes/546511-page-x-of-y-with- reportlab / ,但是他们使用 canvasmaker 进行了解释,其中我在构建中使用 OnlaterPages 参数。



$ p

解决方案

这是一个改进的配方 http://code.activestate.com/recipes/576832/ ,它们应该与图像一起使用。


   def analysis_report(request):

       response = HttpResponse(mimetype='application/pdf')
       response['Content-Disposition'] = 'attachment;filename=ANALYSIS_REPORT.pdf'
       buffer = StringIO()
       doc = SimpleDocTemplate(buffer)
       doc.sample_no = 12345
       document = []
       doc.build(document, onLaterPages=header_footer)

   def header_footer(canvas, doc):
       canvas.saveState()

       canvas.setFont("Times-Bold", 11)
       canvas.setFillColor(gray)
       canvas.setStrokeColor('#5B80B2')
       canvas.drawCentredString(310, 800, 'HEADER ONE GOES HERE')
       canvas.drawString(440, 780, 'Sample No: %s' %doc.sample_no)

       canvas.setFont('Times-Roman', 5)
       canvas.drawString(565, 4, "Page %d" % doc.page)

I above code i can bale to display the page number, but my question is how can i display "Page X of Y" where Y is page count and X is current page.

I followed this http://code.activestate.com/recipes/546511-page-x-of-y-with-reportlab/, but they explained using canvasmaker, where as i'm using OnlaterPages argument in build.

How can i achieve the above thing using canvasmaker or is there any solution using OnLaterPages ?

解决方案

Here is the improved recipe http://code.activestate.com/recipes/576832/ which should work with images.

这篇关于如何使用reportlab在PDF中添加总页数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆