简体   繁体   English

ReportLab将页面大小设置为Letter

[英]ReportLab Set Page Size to Letter

I am trying to set my page size to letter while still adding a flowable style. 我试图将页面大小设置为字母,同时仍添加可流动的样式。 Please help me refine my code. 请帮助我完善代码。

# 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()

The goal is to be able to add flows but not alter the size of the page. 目标是能够添加流,但不能更改页面的大小。 We need this report to be in leter size. 我们需要此报告的大小。

Try This.... 尝试这个....

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

It works........ 有用........

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM