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

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
Wall

from fpdf import FPDF

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

# Code 2
pdf =  PDF("L", "mm", "Letter")
Reply
#2
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()
    canvas.setFont('Times-Roman',12)
    canvas.setFillColorRGB(0,0,255)
    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.setTitle(Title)
    c.saveState()
    c.setFont('Times-Bold',16)
    c.setFillColorRGB(255,0,0)
    c.drawCentredString(height/2.0, width-50, Title)
    # adjust count to put more or less data on 1 page
    count = 0
    startY = 500
    c.setFont('Times-Roman',16)
    c.setFillColorRGB(0,0,255)
    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.setFont('Times-Roman',14)
        c.setFillColorRGB(0,0,255)
        c.drawString(100, startY, text)
        count +=1
        # next line 30 points lower
        startY = startY-30
        addPageNumber(c)
        if count%10 == 0:
            startY = 500
            c.showPage()
    c.save()

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])
        my_data.append(tup)

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


Possibly Related Threads…
Thread Author Replies Views Last Post
  fpdf star character issue KatMac 3 320 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 696 Apr-19-2021, 01:23 PM
Last Post: KatMac
  FPDF question DPaul 2 392 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,079 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

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