gpt4 book ai didi

python - 如何使用 reportlab 在 PDF 中添加总页数

转载 作者:太空宇宙 更新时间:2023-11-03 13:21:32 25 4
gpt4 key购买 nike

   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)

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

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

我如何使用 canvasmaker 实现上述目标,或者是否有任何使用 OnLaterPages 的解决方案?

最佳答案

这是改进后的食谱 http://code.activestate.com/recipes/576832/这应该适用于图像。

关于python - 如何使用 reportlab 在 PDF 中添加总页数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12233787/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com