Skip to content

Commit

Permalink
formating
Browse files Browse the repository at this point in the history
  • Loading branch information
vinissou committed Oct 1, 2024
1 parent df1ed1a commit 1b5109b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 31 deletions.
8 changes: 5 additions & 3 deletions modules/page_sizes.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
iso = {"A4" : {"X" : 222, "Y" : 2004},
"A5" : {"X" : 22,"Y" : 204},
"A6" : {"X" : 33,"Y" : 333}}
iso = {
"A4": {"X": 222, "Y": 2004},
"A5": {"X": 22, "Y": 204},
"A6": {"X": 33, "Y": 333},
}

print(iso["A6"]["X"])
54 changes: 26 additions & 28 deletions pdf_page_size_fixer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,56 +2,54 @@
import fitz
from modules import page_sizes

parser = argparse.ArgumentParser()
parser = argparse.ArgumentParser()
parser.add_argument("--file", "-f", type=str, required=True)
args = parser.parse_args()


src = fitz.Document(args.file)
doc = fitz.Document()

print('\n FILE:',args.file)
print("\n FILE:", args.file)

for page in src:
imglist = src.get_page_images(page.number)[0]
imglist = src.get_page_images(page.number)[0]
rotation = page.rotation
page.set_rotation(0)

#TODO: add all formats and remove from here
A4Maior = 842.0
A4Menor = 595.0
pageH = imglist[2]
pageW = imglist[3]
mediaH = page.mediabox[2]
mediaW = page.mediabox[3]
# TODO: add all formats and remove from here
A4Maior = 842.0
A4Menor = 595.0
pageH = imglist[2]
pageW = imglist[3]
mediaH = page.mediabox[2]
mediaW = page.mediabox[3]
mediaMIN = min(page.mediabox[2], page.mediabox[3])
mediaMAX = max(page.mediabox[2], page.mediabox[3])



# Checks if it is landscape, portrait ignoring the rotation value
# as it was the only way found to maintain the original rotation
print('\nPage:', page.number+1, '\tRotation:', rotation, end="\t")
if pageH < pageW and mediaW == mediaMAX:
new_page= doc.new_page(width=A4Menor, height=A4Maior)
print('>> Portrait')
print("\nPage:", page.number + 1, "\tRotation:", rotation, end="\t")
if pageH < pageW and mediaW == mediaMAX:
new_page = doc.new_page(width=A4Menor, height=A4Maior)
print(">> Portrait")
elif pageH > pageW and mediaH == mediaMAX:
new_page= doc.new_page(width=A4Maior, height=A4Menor)
print('>> Landscape')
new_page = doc.new_page(width=A4Maior, height=A4Menor)
print(">> Landscape")
elif pageH == pageW:
new_page= doc.new_page(width=A4Maior, height=A4Menor)
print('>> Perfect square > converting to landscape')
else:
new_page= doc.new_page(width=mediaW, height=mediaH)
print('>> PAGE SIZE NOT RECOGNIZED > keeping the original')

new_page = doc.new_page(width=A4Maior, height=A4Menor)
print(">> Perfect square > converting to landscape")
else:
new_page = doc.new_page(width=mediaW, height=mediaH)
print(">> PAGE SIZE NOT RECOGNIZED > keeping the original")

new_page.show_pdf_page(page.rect, src, page.number)

#restore original rotation value of the page
#it needs to be after page.show_pdf_page
if rotation != 0:
# restore original rotation value of the page
# it needs to be after page.show_pdf_page
if rotation != 0:
page.set_rotation(rotation)

src.close()
doc.save("output.pdf")
print('\n\tPDF CONVERTED\n')
print("\n\tPDF CONVERTED\n")

0 comments on commit 1b5109b

Please sign in to comment.