Julian Olivares
Julian Olivares

Reputation: 131

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.

Upvotes: 0

Views: 15328

Answers (1)

Javed
Javed

Reputation: 1753

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........

Upvotes: 3

Related Questions