Enable Dark Mode!
operations-in-pdf-using-python.jpg
By: Noushid Khan

Operations in PDF Using Python

Technical Odoo 13

In pdf we can perform needed operations. We can use many functions using different packages like PyPDF2 , reportlab and pdfkit.
 
In using PyPDF2 
a) Decrypting PDF Using a password.
b) Get Outline Details of Page’s.
c) Using Destination objects can find the page numbers.
d)Finding PDF page numbers.
e) Getting page using page number.
f) Finding page number using page object.

In using reportlab
a) Write a string to PDF
b) Change the paper size to the needed type of size.
c) Different Drawing Operations 

Decrypting PDF Using Password:
Some PDF files may be encrypted using passwords; here we can use a decryption method.
 
pdf_file = open(“pdf path”, “mode”)
pdf_reader = PdfFileReader(pdf_file).decrypt(“password”)

Example:
pdf_file = open(“example.pdf”, “rb”)
pdf_reader = PdfFileReader(pdf_file).decrypt(“123”)

Outline Details of Page’s:
We can take the page's outline details like title and other properties.
pdf_file = open(“pdf path”, “mode”)
pdf_reader = PdfFileReader(pdf_file)
print(pdf_reader.getOutlines())
              or
print(pdf_reader.outlines)

Example:
         pdf_file = open(“example.pdf”, “rb”)
         pdf_reader = PdfFileReader(pdf_file)
         print(pdf_reader.getOutlines())

Output:
[{'/Title': 'Slide 1', '/Page': IndirectObject(1, 0), '/Type': '/XYZ', '/Left': 0, '/Top': 791.9, '/Zoom': 0}, {'/Title': 'Slide 2', '/Page': IndirectObject(4, 0), '/Type': '/XYZ', '/Left': 0, '/Top': 791.9, '/Zoom': 0}]

Destination Object Get Page Number:
The destination object brings the page number of the current object. The destination object will be the outline details of the current page.if the output of page number returns as -1 then the page is not found.
pdf_file = open(“pdf path”, “mode”)
pdf_reader = PdfFileReader(pdf_file)
outlines = pdf_reader.getOutlines()

for outline in outlines:
	page_number = pdf_reader.getDestinationPageNumber(outline)

Example: 
        pdf_file = open(“example.pdf”, “rb”)
        pdf_reader = PdfFileReader(pdf_file)
        outlines = pdf_reader.getOutlines()
        for outline in outlines:
        page_number = pdf_reader.getDestinationPageNumber(outline)
        print(page_number)
  
Output:
0
1

Find PDF Total pages:
PDF pages can be found using the getNumPages() function. It will return the total number of pages.

Code:
pdf_file = open(“pdf path”, “mode”)
pdf_reader = PdfFileReader(pdf_file)
pdf_pages = pdf_reader.getNumPages()
             or 
pdf_pages = pdf_reader.numPages

Example:
pdf_file = open(“example.pdf”, “rb”)
pdf_reader = PdfFileReader(pdf_file)
pdf_pages = pdf_reader.getNumPages()
print(pdf_pages)
Output:
2

Getting Page using Page Number:
We can get a page as page object using getPage() function with page number as input.
pdf_file = open(“pdf path”, “mode”)
pdf_reader = PdfFileReader(pdf_file)
pdf_page = pdf_reader.getPage(page number)

Example:
pdf_file = open(“example.pdf”, “rb”)
pdf_reader = PdfFileReader(pdf_file)
pdf_page = pdf_reader.getPage(0)
print(pdf_page)

Output:
{'/Type': '/Page', '/Parent': IndirectObject(7, 0), '/Resources':     IndirectObject(34, 0), '/MediaBox': [0, 0, 611.97165, 791.97165], '/Group': {'/S': '/Transparency', '/CS': '/DeviceRGB', '/I': <PyPDF2.generic.BooleanObject object at 0x7f4b37c735c0>}, '/Contents': IndirectObject(2, 0)}

Page Number Using Page Object:
We can find page number using page object  in getPageNumber(page) here we can give page object as input.
pdf_file = open(“pdf path”, “mode”)
        pdf_reader = PdfFileReader(pdf_file)
        pdf_page = pdf_reader.getPageNumber(page object)

Example:
pdf_file = open(“example.pdf”, “rb”)
        pdf_reader = PdfFileReader(pdf_file)
page_number = pdf_reader.getPageNumber(pdf_page)
        print(page_number)

Output:
0

Write String to PDF:
We can write string to pdf in a specific position based on graph measurement using x, y coordinates.

        from reportlab.pdfgen import canvas 
        c = canvas.Canvas(“pdf path”)
        c.drawString(x-coordinate, y-coordinate , “String To Write”)
        c.showPage()
        c.save()

Example
        c = canvas.Canvas(“new.pdf”)
        c.drawString(100, 100, “Hello World”)
        c.showPage()
        c.save()

Change paper size:
We can change paper size to needed size like A4, A3 ..etc and it can be use Default size also.
It need to import paper sizes from the reportlab like 
from reportlab.pagesizes import letter , A4
letter and A4 are the default paper size. We can also import sizes like A3, A2 ..etc.

from reportlab.pagesizes import page size
from reportlab.pdfgen import canvas 
        c = canvas.Canvas(“pdf path”, pagesize= page size)
        c.save()

Example:
from reportlab.pagesizes import A4
from reportlab.pdfgen import canvas 
        c = canvas.Canvas(“new.pdf” , pagesize= A4)
        c.save()

Different Drawing Operations:
We can Perform Different Drawing Operations Like Drawing Lines in pdf or rectangle etc.... we can perform many type operations may be these operations needed some kind of units for the measurement.
We can take unit measurements in reportlab.lib.units like
from reportlab.lib.units import inch 

Line Methods:
It draws a straight line in the canvas.

canvas.line(x1,y1, x2, y2)
canvas.lines(line list)

        from reportlab.pdfgen import canvas 
        c = canvas.Canvas(“pdf path”)
        c.line(x1, y1, x2, y2)
        c.lines(linelist)
        c.showPage()
        c.save()

Example:
from reportlab.pdfgen import canvas
c = canvas.Canvas(“new.pdf”)
c.line(0,0,100,100)
lines = []
x1 = 0
y1 = 0
x2 = 100
y2 = 100
for i in range(5):
line_data = (x1, y1, x2, y2)
lines.append(line_data)
x2 = x2 + 10
y2 = y2 + 10
c.lines(lines)
c.showPage()
c.save()

Grid Method:
For drawing grid in the pdf.
canvas.grid(xlist, ylist)

    from reportlab.pdfgen import canvas 
    c = canvas.Canvas(“pdf path”)
    canvas.grid(xlist, ylist)
    c.showPage()
    c.save()

Example:
from reportlab.pdfgen import canvas 
        c = canvas.Canvas(“new.pdf”)
        c.grid(range(0, int(17 * inch / 2), int(60 / 2)), range(0, int(22 * 60 / 2), int(60 / 2)))
        c.showPage()
        c.save()


If you need any assistance in odoo, we are online, please chat with us.



0
Comments



Leave a comment



whatsapp_icon
location

Calicut

Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635

location

Kochi

Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.

location

Bangalore

Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message