3
   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)

上記のコードでは、ページ番号を表示することができますが、私の質問は、Y がページ数で X が現在のページである "Page X of Y" を表示するにはどうすればよいかということです。

私はこのhttp://code.activestate.com/recipes/546511-page-x-of-y-with-reportlab/に従いましたが、ビルドでOnlaterPages引数を使用しているため、canvasmakerを使用して説明しました。

canvasmaker を使用して上記のことを達成するにはどうすればよいですか、または OnLaterPages を使用して解決策はありますか?

4

2 に答える 2

4

これは、画像で動作する改良されたレシピhttp://code.activestate.com/recipes/576832/です。

于 2012-09-02T06:57:19.163 に答える
2

別の可能な回避策は、pyPDF (または機能を備えたその他の pdf-lib) を使用して合計ページ数を読み取り、doc.build()対応する を交換して収集した情報でストーリーを再構築することParagraph()です。このアプローチはよりハックかもしれませんが、サブクラス化を行わなくてもうまくいきます。

例:

from pyPdf import PdfFileReader
[...]
story.append(Paragraph('temp paragraph. this will be exchanged with the total page number'))
post_story = story[:] #copy the story because build consumes it
doc.build(story) #build the pdf with name temp.pdf
temp_pdf = PdfFileReader(file("temp.pdf", "rb"))
total_pages = cert_temp.getNumPages()
post_story[-1] = Paragraph('total pages: ' + str(total_pages))
doc.build(post_story)
于 2014-02-24T12:05:32.917 に答える