Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
mat2
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
tails
mat2
Commits
acb9b2d1
Commit
acb9b2d1
authored
7 years ago
by
Julien (jvoisin) Voisin
Browse files
Options
Downloads
Patches
Plain Diff
Clean metadata
parent
df3c27d7
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/parsers/pdf.py
+16
-21
16 additions, 21 deletions
src/parsers/pdf.py
tests/test_libmat2.py
+2
-3
2 additions, 3 deletions
tests/test_libmat2.py
with
18 additions
and
24 deletions
src/parsers/pdf.py
+
16
−
21
View file @
acb9b2d1
...
...
@@ -7,17 +7,13 @@ import logging
import
tempfile
import
shutil
import
io
import
tempfile
import
cairo
import
gi
gi
.
require_version
(
'
Poppler
'
,
'
0.18
'
)
from
gi.repository
import
Poppler
try
:
from
PIL
import
Image
except
ImportError
:
Image
=
None
from
.
import
abstract
logging
.
basicConfig
(
level
=
logging
.
DEBUG
)
...
...
@@ -26,8 +22,9 @@ logging.basicConfig(level=logging.DEBUG)
class
PDFParser
(
abstract
.
AbstractParser
):
def
__init__
(
self
,
filename
):
super
().
__init__
(
filename
)
self
.
meta_list
=
{
'
title
'
,
'
author
'
,
'
subject
'
,
'
keywords
'
,
'
creator
'
,
'
producer
'
,
'
metadata
'
}
self
.
meta_list
=
{
'
author
'
,
'
creation-date
'
,
'
creator
'
,
'
format
'
,
'
keywords
'
,
'
metadata
'
,
'
mod-date
'
,
'
producer
'
,
'
subject
'
,
'
title
'
,
'
viewer-preferences
'
}
self
.
uri
=
'
file://
'
+
os
.
path
.
abspath
(
self
.
filename
)
self
.
password
=
None
...
...
@@ -37,25 +34,24 @@ class PDFParser(abstract.AbstractParser):
and shove those PNG into a new PDF. Metadata from the new
PDF are removed via Poppler, because there is no way to tell
cairo to not add
"
created by cairo
"
during rendering.
TODO: Improve the resolution
TODO: Don
'
t use a temp file
"""
document
=
Poppler
.
Document
.
new_from_file
(
self
.
uri
,
self
.
password
)
pages_count
=
document
.
get_n_pages
()
pdf_surface
=
cairo
.
PDFSurface
(
self
.
output_filename
,
128
,
128
)
_
,
tmp_path
=
tempfile
.
mkstemp
()
pdf_surface
=
cairo
.
PDFSurface
(
tmp_path
,
128
,
128
)
pdf_context
=
cairo
.
Context
(
pdf_surface
)
for
pagenum
in
range
(
document
.
get_n_pages
()
):
for
pagenum
in
range
(
pages_count
):
page
=
document
.
get_page
(
pagenum
)
page_width
,
page_height
=
page
.
get_size
()
logging
.
info
(
"
Rendering page %d/%d
"
,
pagenum
+
1
,
document
.
get_n_pages
()
)
logging
.
info
(
"
Rendering page %d/%d
"
,
pagenum
+
1
,
pages_count
)
img_surface
=
cairo
.
ImageSurface
(
cairo
.
FORMAT_ARGB32
,
int
(
page_width
)
*
2
,
int
(
page_height
)
*
2
)
img_context
=
cairo
.
Context
(
img_surface
)
img_context
.
scale
(
2
,
2
)
page
.
render_for_printing
_with_options
(
img_context
,
Poppler
.
PrintFlags
.
DOCUMENT
)
page
.
render_for_printing
(
img_context
)
img_context
.
show_page
()
buf
=
io
.
BytesIO
()
...
...
@@ -63,8 +59,6 @@ class PDFParser(abstract.AbstractParser):
img_surface
.
finish
()
buf
.
seek
(
0
)
#buf = self.__optimize_image_size(buf)
img
=
cairo
.
ImageSurface
.
create_from_png
(
buf
)
pdf_surface
.
set_size
(
page_width
*
2
,
page_height
*
2
)
pdf_context
.
set_source_surface
(
img
,
0
,
0
)
...
...
@@ -73,11 +67,12 @@ class PDFParser(abstract.AbstractParser):
pdf_surface
.
finish
()
# This is removing metadata
#document = Poppler.Document.new_from_file('file://' + os.path.abspath('OUT.pdf'), self.password)
#document.set_producer('totally not MAT2 ;)')
#document.set_creator('')
#document.save('file://' + os.path.abspath("OUT_clean.pdf"))
# This is removing metadata added by Poppler
document
=
Poppler
.
Document
.
new_from_file
(
'
file://
'
+
tmp_path
)
document
.
set_producer
(
''
)
document
.
set_creator
(
''
)
document
.
save
(
'
file://
'
+
os
.
path
.
abspath
(
self
.
output_filename
))
os
.
remove
(
tmp_path
)
return
True
...
...
This diff is collapsed.
Click to expand it.
tests/test_libmat2.py
+
2
−
3
View file @
acb9b2d1
...
...
@@ -31,6 +31,5 @@ class TestCleaning(unittest.TestCase):
self
.
assertTrue
(
ret
)
p
=
pdf
.
PDFParser
(
'
./tests/data/clean.pdf.cleaned
'
)
remaining_meta
=
{
'
creator
'
:
'
cairo 1.14.10 (http://cairographics.org)
'
,
'
producer
'
:
'
cairo 1.14.10 (http://cairographics.org)
'
}
self
.
assertEqual
(
p
.
get_meta
(),
remaining_meta
)
expected_meta
=
{
'
creation-date
'
:
-
1
,
'
format
'
:
'
PDF-1.5
'
,
'
mod-date
'
:
-
1
}
self
.
assertEqual
(
p
.
get_meta
(),
expected_meta
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment