Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
P
projeto-recortador-pdf
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Pedro Bueno
projeto-recortador-pdf
Commits
f33a8e6e
Commit
f33a8e6e
authored
May 14, 2026
by
2024105070012
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Resolvendo o Processamento do PDF em Python
parent
b23f3e60
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
20 additions
and
25 deletions
+20
-25
app.py
app.py
+20
-25
No files found.
app.py
View file @
f33a8e6e
...
@@ -4,24 +4,14 @@ import fitz # PyMuPDF
...
@@ -4,24 +4,14 @@ import fitz # PyMuPDF
import
os
import
os
import
zipfile
import
zipfile
from
PIL
import
Image
from
PIL
import
Image
import
sqlite3
app
=
Flask
(
__name__
)
app
=
Flask
(
__name__
)
UPLOAD_FOLDER
=
'uploads'
UPLOAD_FOLDER
=
'uploads'
os
.
makedirs
(
UPLOAD_FOLDER
,
exist_ok
=
True
)
os
.
makedirs
(
UPLOAD_FOLDER
,
exist_ok
=
True
)
def
init_db
():
conn
=
sqlite3
.
connect
(
'database.db'
)
conn
.
execute
(
'''CREATE TABLE IF NOT EXISTS configuracoes
(id INTEGER PRIMARY KEY AUTOINCREMENT, nome TEXT, coords TEXT)'''
)
conn
.
close
()
@app.route
(
'/'
)
@app.route
(
'/'
)
def
index
():
def
index
():
conn
=
sqlite3
.
connect
(
'database.db'
)
return
render_template
(
'index.html'
)
configs
=
conn
.
execute
(
'SELECT * FROM configuracoes'
)
.
fetchall
()
conn
.
close
()
return
render_template
(
'index.html'
,
configs
=
configs
)
@app.route
(
'/upload_preview'
,
methods
=
[
'POST'
])
@app.route
(
'/upload_preview'
,
methods
=
[
'POST'
])
def
upload_preview
():
def
upload_preview
():
...
@@ -38,13 +28,11 @@ def preview(filename, pagnumber):
...
@@ -38,13 +28,11 @@ def preview(filename, pagnumber):
if
not
os
.
path
.
exists
(
pdf_path
):
return
"Erro"
,
404
if
not
os
.
path
.
exists
(
pdf_path
):
return
"Erro"
,
404
doc
=
fitz
.
open
(
pdf_path
)
doc
=
fitz
.
open
(
pdf_path
)
# Garante que a página solicitada existe no PDF
total
=
len
(
doc
)
total
=
len
(
doc
)
if
pagnumber
>=
total
:
pagnumber
=
total
-
1
pagnumber
=
max
(
0
,
min
(
pagnumber
,
total
-
1
))
if
pagnumber
<
0
:
pagnumber
=
0
page
=
doc
[
pagnumber
]
page
=
doc
[
pagnumber
]
pix
=
page
.
get_pixmap
(
dpi
=
100
)
pix
=
page
.
get_pixmap
(
dpi
=
100
)
img_data
=
pix
.
tobytes
(
"png"
)
img_data
=
pix
.
tobytes
(
"png"
)
doc
.
close
()
doc
.
close
()
return
send_file
(
io
.
BytesIO
(
img_data
),
mimetype
=
'image/png'
)
return
send_file
(
io
.
BytesIO
(
img_data
),
mimetype
=
'image/png'
)
...
@@ -54,8 +42,14 @@ def processar():
...
@@ -54,8 +42,14 @@ def processar():
filename
=
request
.
form
[
'filename'
]
filename
=
request
.
form
[
'filename'
]
inicio
=
int
(
request
.
form
[
'inicio'
])
inicio
=
int
(
request
.
form
[
'inicio'
])
fim
=
int
(
request
.
form
[
'fim'
])
fim
=
int
(
request
.
form
[
'fim'
])
c
=
list
(
map
(
float
,
request
.
form
[
'coords'
]
.
split
(
','
)))
# x, y, w, h, dispW, dispH, realW, realH
data
=
list
(
map
(
float
,
request
.
form
[
'coords'
]
.
split
(
','
)))
x
,
y
,
w
,
h
,
dispW
,
dispH
,
realW
,
realH
=
data
scale_x
=
realW
/
dispW
scale_y
=
realH
/
dispH
pdf_path
=
os
.
path
.
join
(
UPLOAD_FOLDER
,
filename
)
pdf_path
=
os
.
path
.
join
(
UPLOAD_FOLDER
,
filename
)
doc
=
fitz
.
open
(
pdf_path
)
doc
=
fitz
.
open
(
pdf_path
)
zip_path
=
os
.
path
.
join
(
UPLOAD_FOLDER
,
"recortes.zip"
)
zip_path
=
os
.
path
.
join
(
UPLOAD_FOLDER
,
"recortes.zip"
)
...
@@ -63,15 +57,18 @@ def processar():
...
@@ -63,15 +57,18 @@ def processar():
with
zipfile
.
ZipFile
(
zip_path
,
'w'
)
as
zip_f
:
with
zipfile
.
ZipFile
(
zip_path
,
'w'
)
as
zip_f
:
for
p
in
range
(
inicio
,
fim
+
1
):
for
p
in
range
(
inicio
,
fim
+
1
):
if
p
>=
len
(
doc
):
break
if
p
>=
len
(
doc
):
break
page
=
doc
[
p
]
page
=
doc
[
p
]
pix
=
page
.
get_pixmap
(
dpi
=
300
)
pix
=
page
.
get_pixmap
(
dpi
=
300
)
img
=
Image
.
frombytes
(
"RGB"
,
[
pix
.
width
,
pix
.
height
],
pix
.
samples
)
img
_full
=
Image
.
frombytes
(
"RGB"
,
[
pix
.
width
,
pix
.
height
],
pix
.
samples
)
s
=
300
/
100
# Escala Preview vs Corte Final
dpi_m
=
300
/
100
area
=
(
c
[
0
]
*
s
,
c
[
1
]
*
s
,
(
c
[
0
]
+
c
[
2
])
*
s
,
(
c
[
1
]
+
c
[
3
])
*
s
)
left
=
x
*
scale_x
*
dpi_m
top
=
y
*
scale_y
*
dpi_m
right
=
(
x
+
w
)
*
scale_x
*
dpi_m
bottom
=
(
y
+
h
)
*
scale_y
*
dpi_m
img_cortada
=
img
.
crop
(
area
)
img_cortada
=
img
_full
.
crop
((
left
,
top
,
right
,
bottom
)
)
img_name
=
f
"pagina_{p}.png"
img_name
=
f
"pagina_{p
+1
}.png"
img_path
=
os
.
path
.
join
(
UPLOAD_FOLDER
,
img_name
)
img_path
=
os
.
path
.
join
(
UPLOAD_FOLDER
,
img_name
)
img_cortada
.
save
(
img_path
)
img_cortada
.
save
(
img_path
)
zip_f
.
write
(
img_path
,
img_name
)
zip_f
.
write
(
img_path
,
img_name
)
...
@@ -81,5 +78,4 @@ def processar():
...
@@ -81,5 +78,4 @@ def processar():
return
send_file
(
zip_path
,
as_attachment
=
True
)
return
send_file
(
zip_path
,
as_attachment
=
True
)
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
init_db
()
app
.
run
(
debug
=
True
)
app
.
run
(
debug
=
True
)
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment