Skip to content
This repository has been archived by the owner on Sep 25, 2019. It is now read-only.

Add unoconv-based script to normalize office docs #6

Open
wants to merge 1 commit into
base: qa/1.x
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions normalization/preservation-office-pdfa.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import subprocess
import sys

# Python 2/3 compatibility
try:
range = xrange
except NameError:
pass

MAX_TRIES = 16


def convert(document, target, max_tries=MAX_TRIES):
"""
Attempt to convert a document into a PDF at the target location.

This continues up to max_tries times; it's possible for initial calls
to fail due to delays in spinning up a LibreOffice server, where
retries may succeed.
"""
for _ in range(0, max_tries):
try:
# SelectPdfVersion=1 converts to PDF/A-1a, instead of
# the default PDF 1.4
subprocess.check_call(['unoconv', '-eSelectPdfVersion=1',
'--output', target, document])
return 0
except subprocess.CalledProcessError:
continue

return 1

if __name__ == '__main__':
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--file-full-name", dest="input")
parser.add_argument("--output-file-path", dest="output")
args, _ = parser.parse_known_args()

sys.exit(convert(args.input, args.output + '.pdf'))