From cf3c89bf25673c1c8d9c1e2332f152c7a68af539 Mon Sep 17 00:00:00 2001 From: Ariel Sosnovsky Date: Fri, 9 Nov 2018 10:22:44 -0500 Subject: [PATCH] supporting ICCSBased colors --- pdfmajor/converters/XMLConverter.py | 2 +- pdfmajor/interpreter/PDFPageInterpreter.py | 46 ++++++++++++++++++-- samples/convert_samples.py | 50 ++++++++-------------- setup.py | 2 +- 4 files changed, 64 insertions(+), 36 deletions(-) diff --git a/pdfmajor/converters/XMLConverter.py b/pdfmajor/converters/XMLConverter.py index cc4e2bc..f3da096 100644 --- a/pdfmajor/converters/XMLConverter.py +++ b/pdfmajor/converters/XMLConverter.py @@ -33,7 +33,7 @@ def get_color(col: PDFGraphicStateColor): return f'gray({color_val})' elif color_type == "custom": # Todo: convert these to more friendly color types - return f"custom({col.custom['type']})" + return f"{col.custom['type']}({col.custom['defintion']})" return "" FIRST_CAP_RE = re.compile('(.)([A-Z][a-z]+)') diff --git a/pdfmajor/interpreter/PDFPageInterpreter.py b/pdfmajor/interpreter/PDFPageInterpreter.py index 6977023..411d263 100644 --- a/pdfmajor/interpreter/PDFPageInterpreter.py +++ b/pdfmajor/interpreter/PDFPageInterpreter.py @@ -351,6 +351,24 @@ class PDFPageInterpreter(object): #self.do_cs(LITERAL_DEVICE_CMYK) return + def __process_ICCBased(self, n: int, components: list): + if n == 1: + return { + "type": "gray", + "definition": components + } + elif n == 3: + return { + "type": "rgb", + "definition": components + } + elif n == 4: + return { + "type": "cmyk", + "definition": components + } + raise PDFInterpreterError("Unsupported ICCBased color") + # setcolor def __process_scn(self): if self.ncs: @@ -359,8 +377,14 @@ class PDFPageInterpreter(object): raise PDFInterpreterError('No colorspace specified!') components = self.pop(n) obj_id = components[0] + if self.ncs.name == "ICCBased": + return self.__process_ICCBased(n, components) if self.ncs.name not in self.resources.keys(): - raise PDFInterpreterError("Color space not in resources") + log.warning("Color space not in resources") + return { + "type": self.ncs.name, + "defintion": f"{n}|({','.join(map(str, components))})" + } if obj_id.name not in self.resources[self.ncs.name].keys(): raise PDFInterpreterError("Invalid Color reference") @@ -374,11 +398,27 @@ class PDFPageInterpreter(object): "defintion": color_data } def do_SCN(self): - self.graphicstate.scolor.custom = self.__process_scn() + processed_col = self.__process_scn() + if processed_col['type'] == 'rgb': + self.graphicstate.scolor.rgb = processed_col['definition'] + elif processed_col['type'] == 'gray': + self.graphicstate.scolor.gray = processed_col['definition'][0] + elif processed_col['type'] == 'cmyk': + self.graphicstate.scolor.cmyk = processed_col['definition'] + else: + self.graphicstate.scolor.custom = processed_col return def do_scn(self): - self.graphicstate.ncolor.custom = self.__process_scn() + processed_col = self.__process_scn() + if processed_col['type'] == 'rgb': + self.graphicstate.ncolor.rgb = processed_col['definition'] + elif processed_col['type'] == 'gray': + self.graphicstate.ncolor.gray = processed_col['definition'][0] + elif processed_col['type'] == 'cmyk': + self.graphicstate.ncolor.cmyk = processed_col['definition'] + else: + self.graphicstate.ncolor.custom = processed_col return def do_SC(self): diff --git a/samples/convert_samples.py b/samples/convert_samples.py index 40136e4..e8d37e7 100644 --- a/samples/convert_samples.py +++ b/samples/convert_samples.py @@ -8,7 +8,7 @@ from xml.etree import ElementTree def rel_path(*arg: List[str]) -> str: return os.path.join(os.path.dirname(__file__), *arg) -EXT = 'xml' +EXT = 'html' INPUT_FOLDER = rel_path("./pdf") OUTPUT_FOLDER = rel_path(f"./output/{EXT}") @@ -17,34 +17,22 @@ if not os.path.exists(OUTPUT_FOLDER): files = os.listdir(INPUT_FOLDER) -input_file_path = '/media/ari/sd/projects/School/Schulich/acp/pdf-extraction/data/input/Supplemental_User_Agreement_Settlement_Custodian.pdf' -output_file_path = os.path.join(OUTPUT_FOLDER, 'demo.xml') +TOTAL = len(files) +for idx, file_name in enumerate(files): + file_name_no_ext = re.sub(r'\.pdf$',"", file_name) + input_file_path = os.path.join(INPUT_FOLDER, file_name) + output_file_path = os.path.join(OUTPUT_FOLDER, file_name_no_ext + "." + EXT) -with open(input_file_path, 'rb') as input_file: - with open(output_file_path, 'wb') as output_file: - convert_file( - input_file=input_file, - output_file=output_file, - out_type=EXT, - debug=True, - ) - -# TOTAL = len(files) -# for idx, file_name in enumerate(files): -# file_name_no_ext = re.sub(r'\.pdf$',"", file_name) -# input_file_path = os.path.join(INPUT_FOLDER, file_name) -# output_file_path = os.path.join(OUTPUT_FOLDER, file_name_no_ext + "." + EXT) - -# if len(file_name) > len(file_name_no_ext): -# print(f"processing {file_name} {idx}/{TOTAL}") - # with open(input_file_path, 'rb') as input_file: - # with open(output_file_path, 'wb') as output_file: - # convert_file( - # input_file=input_file, - # output_file=output_file, - # out_type=EXT, - # debug=True, - # ) -# print(f" > testing format {file_name} {idx}/{TOTAL}") -# ElementTree.parse(output_file_path) -# print(f"processing {idx+1}/{TOTAL}") \ No newline at end of file + if len(file_name) > len(file_name_no_ext): + print(f"processing {file_name} {idx}/{TOTAL}") + with open(input_file_path, 'rb') as input_file: + with open(output_file_path, 'wb') as output_file: + convert_file( + input_file=input_file, + output_file=output_file, + out_type=EXT, + debug=True, + ) + print(f" > testing format {file_name} {idx}/{TOTAL}") + ElementTree.parse(output_file_path) + print(f"processing {idx+1}/{TOTAL}") \ No newline at end of file diff --git a/setup.py b/setup.py index d29795e..56839af 100644 --- a/setup.py +++ b/setup.py @@ -10,7 +10,7 @@ import sys with open('README.md') as readme_file: SETUP = { "name": "pdfmajor", - "version": "1.1.8", + "version": "1.1.9", "packages": find_packages(include=['pdfmajor*']), "install_requires": [ 'pycryptodome',