Python Forum
fpdf orientation not working properly
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
fpdf orientation not working properly

I have a csv file that I converted to a pdf file using fpdf.

If I set the orientation using Code 1 my page is converted into landscape, but the header and footer do not show up.

If I set the orientation using Code 2, my page stays at the default portrait orientation and
the header and footer are displayed.

Don't know what is wrong

from fpdf import FPDF

# Code 1
pdf =  FPDF("L", "mm", "Letter")

# Code 2
pdf =  PDF("L", "mm", "Letter")
I am just an amateur, and I don't use FPDF.

I make a lot of pdfs for school quizzes.

Try with reportlab. reportlab is complicated but there is lots of help on the internet.

If you study and learn reportlab, you will have very fine control over your output pdf.

import pandas as pd

from reportlab.lib.enums import TA_JUSTIFY
from reportlab.lib.pagesizes import letter
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Image, PageBreak
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.lib.units import mm

def addPageNumber(canvas):
    Add the page number
    page_num = canvas.getPageNumber()
    text = f"Page {page_num}"
    canvas.drawString(20*mm, 20*mm, text)

def createMultiPage2(my_data, path2pdf):
    Create a multi-page document
    To set landscape or portrait
    from reportlab.lib.pagesizes import letter
    width, height = letter
    pagesize=(height,width) = landscape
    pagesize=(width, height) = portrait
    width, height = letter
    Title = "Reportlab PDF Test"
    #c = canvas.Canvas("canvas_page_num.pdf")
    c = Canvas(path2pdf + 'reportlab1.pdf', pagesize=(height,width)) # this is landscape
    c.drawCentredString(height/2.0, width-50, Title)
    # adjust count to put more or less data on 1 page
    count = 0
    startY = 500
    text = my_data[0][0] + ' ' + my_data[0][1]
    c.drawString(100, 530, text)
    # adjust count to put more or less data on 1 page
    count = 0
    startY = 500
    for p in range(1, len(my_data)):
        page_num = c.getPageNumber()
        text = my_data[p][0] + '                               ' + my_data[p][1]
        c.drawString(100, startY, text)
        count +=1
        # next line 30 points lower
        startY = startY-30
        if count%10 == 0:
            startY = 500

if __name__ == "__main__":
    csv_file = '/home/pedro/summer2021/OMR/21TE/question_data_csv/sWeek1_AK.csv'
    df = pd.read_csv(csv_file)
    my_cols = df[['Qnr','answers']] # gets the columns you want

    my_data = [('Question number', 'Answer')]
    # get the data you want from df, put it in a list of tuples (Qnr, correct answer)
    # not sure what df is, maybe bytes
    for i in range(len(my_cols)-1):
        this_data = str(my_cols[i:i+1])
        data_split = this_data.split()
        tup = (data_split[3], data_split[4])

    # now write the data to a pdf
    # every 10 sets of data, start a new page, change this to suit
    createMultiPage2(my_data, path2pdf)

Possibly Related Threads…
Thread Author Replies Views Last Post
  fpdf star character issue KatMac 3 319 May-01-2021, 06:22 PM
Last Post: KatMac
  fpdf adding a new font to my report KatMac 0 213 Apr-23-2021, 02:19 PM
Last Post: KatMac
Lightbulb Jupyter is not working properly brunolelli 3 464 Apr-23-2021, 03:22 AM
Last Post: Larz60+
  ModuleNotFoundError: No module named 'fpdf' KatMac 4 693 Apr-19-2021, 01:23 PM
Last Post: KatMac
  FPDF question DPaul 2 391 Oct-27-2020, 08:26 AM
Last Post: DPaul
  Better Understanding Of Object Orientation In Python JoeDainton123 3 647 Aug-30-2020, 02:49 PM
Last Post: deanhystad
  file.write not working properly mnh001 11 1,243 Nov-09-2019, 10:20 PM
Last Post: mnh001
  hatching not working properly with matplotlib Staph 3 950 Jul-28-2019, 07:17 AM
Last Post: ThomasL
  Installing FPDF Batman 3 4,078 Jun-04-2019, 04:58 AM
Last Post: heiner55
  Try and Exception not properly working with xpath sumandas89 0 956 Aug-29-2018, 02:33 PM
Last Post: sumandas89

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020