ReportLab将页面大小设置为Letter

时间:2017-04-26 15:05:39

标签: python formatting reportlab

我正在尝试将页面大小设置为letter,同时仍添加可流动样式。请帮我改进我的代码。

# Sample platypus document
# From the FAQ at reportlab.org/oss/rl-toolkit/faq/#1.1

# Import some constructors, some paragraph styles and other conveniences from other modules.
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.rl_config import defaultPageSize
from reportlab.lib.pagesizes import letter
from reportlab.lib.units import inch
from reportlab.platypus import Image
from reportlab.lib.utils import ImageReader
from reportlab.pdfgen import canvas


PAGE_HEIGHT = defaultPageSize[0]
PAGE_WIDTH = defaultPageSize[0]
defaultPageSize = letter
styles = getSampleStyleSheet()
Title = "Comparison Index"
pageinfo = "platypus example"
Image3 = 'BTALogo.png'



# Define the fixed features of the first page of the document
def myFirstPage(canvas, doc):
    canvas.saveState()
    canvas.setFontSize(22)
    canvas.setFont('Helvetica-Bold', 36)
    canvas.drawString(50, 670, 'Health & Welfare')

    canvas.setFont('Helvetica-Bold', 24)
    canvas.drawString(50, 625, 'Benchmark Comparison Report')

    canvas.setFont('Helvetica', 16)
    canvas.drawString(50, 550, 'Prepared for:')

    canvas.setFont('Times-Roman', 12)
    canvas.drawString(inch, 0.25 * inch, "First Page / %s" % pageinfo)
    canvas.restoreState()

# Since we want pages after the first to look different from the first we define an alternate layout for
# the fixed features of the other pages.
# Note that the two functions use the pdfgen level canvas operations to  paint the annotations for the pages.
def myLaterPages(canvas, doc):
    canvas.saveState()
    canvas.setFont('Times-Roman', 23)
    canvas.drawString(inch, 0.75 * inch, "Page %d %s" % (doc.page, pageinfo))
    canvas.restoreState()

def myThirdPages(canvas, doc):
    canvas.saveState()
    canvas.setFont('Times-Roman', 9)
    canvas.drawString(inch, 0.75 * inch, "Page %d %s" % (doc.page, pageinfo))
    canvas.restoreState()


# Create a story and build the document
def go():

    doc = SimpleDocTemplate("Comparison Index.pdf", pagesize=letter)
    Story = [Spacer(1, 2 * inch)]
    style = styles["Normal"]
    for i in range(3):
        bogustext = ("Paragraph number %s. " % i) * 4
        p = Paragraph(bogustext, style)
        Story.append(p)
        Story.append(Spacer(1, 0.2 * inch))

    logo = "PlanOffered.jpg"
    im = Image(logo, 7 * inch, 2 * inch)
    Story.append(im)

    logo = "BTALogo.png"
    im2 = Image(logo, 4 * inch, 4 * inch)
    Story.append(im2)

    doc.build(Story, onFirstPage=myFirstPage, onLaterPages=myLaterPages)


if __name__ == "__main__":
    go()

目标是能够添加流量,但不能改变页面的大小。我们需要此报告的大小。

1 个答案:

答案 0 :(得分:1)

试试这个......

def PrintID(request,std_id):
    response = HttpResponse(content_type='application/pdf')
    response['Content-Disposition'] = 'attachment; 
    filename="somefilename.pdf"'
    buffer = BytesIO()

    p = canvas.Canvas(buffer)
    p.setFont('Helvetica-Bold', 36)

    p.setPageSize((400, 400))
    p.drawString(100, 100, "Hello world.")

# Close the PDF object cleanly, and we're done.
    p.showPage()
    p.save()
    pdf = buffer.getvalue()
    buffer.close()
    response.write(pdf)
    return response

它有效........