fpdf orientation not working properly - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: fpdf orientation not working properly (/thread-33520.html) |
fpdf orientation not working properly - KatMac - May-01-2021 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 from fpdf import FPDF # Code 1 pdf = FPDF("L", "mm", "Letter") # Code 2 pdf = PDF("L", "mm", "Letter") RE: fpdf orientation not working properly - Pedroski55 - May-02-2021 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) |