Commit de761f07 authored by Gabriel Monnerat's avatar Gabriel Monnerat

implemented code to set metadata in pdf documents

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk/utils@43580 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 7a4120bc
......@@ -85,3 +85,26 @@ class PDFHandler(object):
Keyword arguments:
metadata -- expected an dictionary with metadata.
"""
text_template = "InfoKey: %s\nInfoValue: %s\n"
text_list = [text_template % (key.capitalize(), value) \
for key, value in metadata.iteritems()]
metadata_file = File(self.document.directory_name,
"".join(text_list),
"txt")
output_url = mktemp(suffix=".pdf",
dir=self.document.directory_name)
command = ["pdftk",
self.document.getUrl(),
"update_info",
metadata_file.getUrl(),
"output",
output_url
]
stdout, stderr = Popen(command,
stdout=PIPE,
stderr=PIPE,
env=self.environment).communicate()
try:
return open(output_url).read()
finally:
self.document.trash()
......@@ -54,6 +54,17 @@ class TestPDFHandler(HandlerTestCase):
self.assertNotEquals(metadata, {})
self.assertEquals(metadata["title"], 'Free Cloud Alliance Presentation')
def testsetMetadata(self):
"""Test if the metadata is inserted correctly"""
pdf_document = open("data/test.pdf").read()
handler = PDFHandler(self.tmp_url, pdf_document, "pdf", **self.kw)
metadata_dict = {"title": "Set Metadata Test", "creator": "gabriel\'@"}
new_document = handler.setMetadata(metadata_dict)
handler = PDFHandler(self.tmp_url, new_document, "pdf", **self.kw)
metadata = handler.getMetadata()
self.assertEquals(metadata["title"], 'Set Metadata Test')
self.assertEquals(metadata['creator'], 'gabriel\'@')
def test_suite():
suite = unittest.TestSuite()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment