Skip to content

Commit

Permalink
PDF Examples geekcomputers#528
Browse files Browse the repository at this point in the history
  • Loading branch information
NavonilDas committed Oct 1, 2019
1 parent 95374e8 commit 893eb80
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 0 deletions.
36 changes: 36 additions & 0 deletions PDF/basic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from fpdf import FPDF

# Author: @NavonilDas


pdf = FPDF()
# Set Author Name of the PDF
pdf.set_author('@NavonilDas')
# Set Subject of The PDF
pdf.set_subject('python')
# Set the Title of the PDF
pdf.set_title('Generating PDF with Python')
pdf.add_page()


# Set Font family Courier with font size 28
pdf.set_font("Courier",'',18)
# Add Text at (0,50)
pdf.text(0,50,"Example to generate PDF in python.")

# Set Font Family Courier with italic and font size 28
pdf.set_font("Courier",'i',28)
pdf.text(0,60,"This is an italic text") # Write text at 0,60


# Draw a Rectangle at (10,100) with Width 60,30
pdf.rect(10,100,60,30,'D')

# Set Fill color
pdf.set_fill_color(255,0,0) # Red = (255,0,0)

# Draw a Circle at (10,135) with diameter 50
pdf.ellipse(10,135,50,50,'F')

# Save the Output at Local File
pdf.output('output.pdf', 'F')
48 changes: 48 additions & 0 deletions PDF/header_footer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from fpdf import FPDF
# Author: @NavonilDas

class MyPdf(FPDF):
def header(self):
# Uncomment the line below to add logo if needed
# self.image('somelogo.png',12,10,25,25) # Draw Image ar (12,10) with height = 25 and width = 25
self.set_font('Arial','B',18)
self.text(27,10,'Generating PDF With python')
self.ln(10)

def footer(self):
# Set Position at 1cm (10mm) From Bottom
self.set_y(-10)
# Arial italic 8
self.set_font('Arial', 'I', 8)
# set Page number at the bottom
self.cell(0, 10,'Page No {}'.format(self.page_no()), 0, 0, 'C')
pass

from fpdf import FPDF

pdf = MyPdf()
# Set Author Name of the PDF
pdf.set_author('@NavonilDas')
# Set Subject of The PDF
pdf.set_subject('python')
# Set the Title of the PDF
pdf.set_title('Generating PDF with Python')
pdf.add_page()


# Set Font family Courier with font size 28
pdf.set_font("Courier",'',18)
# Add Text at (0,50)
pdf.text(0,50,"Example to generate PDF in python.")

# Set Font Family Courier with italic and font size 28
pdf.set_font("Courier",'i',28)
pdf.text(0,60,"This is an italic text") # Write text at 0,60

pdf.add_page()

# Center Text With border and a line break with height=10mm
pdf.cell(0, 10, 'Hello There', 1, 1, 'C')

# Save the Output at Local File
pdf.output('output.pdf', 'F')
44 changes: 44 additions & 0 deletions PDF/images.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from fpdf import FPDF
import os
from PIL import Image

# Author: @NavonilDas

# Example to Append all the images inside a folder to pdf
pdf = FPDF()

# Size of a A4 Page in mm Where P is for Potrail and L is for Landscape
A4_SIZE = {'P': {'w': 210, 'h': 297}, 'L': {'w': 297, 'h': 210}}
# pdf may produce empty page so we need to set auto page break as false
pdf.set_auto_page_break(0)

for filename in os.listdir('images'):
try:
# Read Image file so that we can cover the complete image properly and if invalid image file skip those file
img = Image.open("images\\"+filename)

# Read Width and Height
width,height = img.size

# Close opened Image
img.close()

# Convert Width and Height into mm from px as 1px = 0.2645833333 mm
width, height = float(width * 0.264583), float(height * 0.264583)

# Check if Width is greater than height then the image is in landscape else in potrait
orientation = 'P' if width < height else 'L'

# Read the minimum of A4 Size and the image size
width = min(A4_SIZE[orientation]['w'],width)
height = min(A4_SIZE[orientation]['h'],height)

# Add Page With an orientation
pdf.add_page(orientation=orientation)
# Add Image with their respective width and height in mm
pdf.image("images\\"+filename, 0, 0, width, height)

except OSError:
print("Skipped : "+filename)

pdf.output('output.pdf','F')
Binary file added PDF/images/ss.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions PDF/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Pillow==5.0.0
fpdf==1.7.2

0 comments on commit 893eb80

Please sign in to comment.