13 Commits
Author SHA1 Message Date
ari 7e24c64b5b Centralized all exceptions 2019-12-19 22:04:01 -05:00
ari cdcd2eabe2 ensured tests are passing 2019-12-19 21:58:44 -05:00
ari fdd2fbd85a removed old exception files 2019-12-19 20:18:49 -05:00
ari 087012dd81 all errors caught in one location 2019-12-19 20:08:06 -05:00
ari 630b3b2418 removed unused cmd_record 2019-12-12 21:45:22 -05:00
ari 441abc3f55 version bump 2019-12-11 22:18:51 -05:00
ari 152d3aaf55 v1.3.12 2019-12-11 22:16:28 -05:00
ari 36a9371657 generated docs 2019-12-08 14:45:28 -05:00
ari a29d465ea4 added support for ignore bad char flag
- updated unit tests to include
- using the textstate.to_unichr abstraction
2019-12-08 14:44:46 -05:00
ari 841576d9b8 exposed exceptions to top module
updated dependencies
2019-12-07 23:15:55 -05:00
ari bfc0b6ba34 added venv 2019-12-07 23:15:12 -05:00
ari 336f5f1fba propograted exceptions 2019-12-07 22:58:13 -05:00
ari 1b1db13b5b updated deps
- upgraded to Pillow 6.2.0 (passes tests)
- removed unnecessary nose dep
2019-12-07 22:57:57 -05:00
62 changed files with 310 additions and 213 deletions

No files matched your search

+1
View File
@@ -14,3 +14,4 @@ tests/samples/output
**__pycache**\ **__pycache**\
.mypy_cache .mypy_cache
.ipynb_checkpoints .ipynb_checkpoints
venv
+1 -1
View File
@@ -1 +1 @@
v1.3.9 v1.3.13
+10 -4
View File
@@ -4,10 +4,16 @@ WK_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cd $WK_DIR cd $WK_DIR
cd .. cd ..
for filename in ./tests/test*.py; do # for filename in ./tests/test*.py; do
if python $filename ; then # if python $filename ; then
echo woohoo # echo woohoo
# else
# exit 1
# fi
# done
if python ./test.py ; then
echo "PASSED TESTS"
else else
exit 1 exit 1
fi fi
done
+1
View File
@@ -37,6 +37,7 @@ This generator-function yields individual pages which contain their respected it
- `password`: [str](#) defaults to None - `password`: [str](#) defaults to None
- `caching`: [bool](#) defaults to True - `caching`: [bool](#) defaults to True
- `check_extractable`: [bool](#) defaults to True - `check_extractable`: [bool](#) defaults to True
- `ignore_bad_chars`: [bool](#) defaults to False
- `pagenos`: [List[int]](#) defaults to None - `pagenos`: [List[int]](#) defaults to None
- `debug_level`: [logging.levels](#https://docs.python.org/3/library/logging.html#levels) defaults logging.WARNING - `debug_level`: [logging.levels](#https://docs.python.org/3/library/logging.html#levels) defaults logging.WARNING
+1
View File
@@ -140,6 +140,7 @@ for page in PDFInterpreter("/path/to/pdf.pdf"):
<li><code>password</code>: <a href="#">str</a> defaults to None </li> <li><code>password</code>: <a href="#">str</a> defaults to None </li>
<li><code>caching</code>: <a href="#">bool</a> defaults to True </li> <li><code>caching</code>: <a href="#">bool</a> defaults to True </li>
<li><code>check_extractable</code>: <a href="#">bool</a> defaults to True</li> <li><code>check_extractable</code>: <a href="#">bool</a> defaults to True</li>
<li><code>ignore_bad_chars</code>: <a href="#">bool</a> defaults to False</li>
<li><code>pagenos</code>: <a href="#">List[int]</a> defaults to None</li> <li><code>pagenos</code>: <a href="#">List[int]</a> defaults to None</li>
<li><code>debug_level</code>: <a href="#https://docs.python.org/3/library/logging.html#levels">logging.levels</a> defaults logging.WARNING</li> <li><code>debug_level</code>: <a href="#https://docs.python.org/3/library/logging.html#levels">logging.levels</a> defaults logging.WARNING</li>
</ul> </ul>
+1 -1
View File
@@ -240,5 +240,5 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.</p>
<!-- <!--
MkDocs version : 1.0.4 MkDocs version : 1.0.4
Build Date UTC : 2019-01-11 20:22:59 Build Date UTC : 2019-12-08 19:44:26
--> -->
+1 -1
View File
@@ -1 +1 @@
{"config":{"lang":["en"],"prebuild_index":false,"separator":"[\\s\\-]+"},"docs":[{"location":"","text":"PDFMajor Latest Release PDF-Major is a fork of PDFMiner.six . It is meant to be a more light-weight implementation that makes fewer assumptions on the data. The ultimate goal of this project is to provide a simpler, faster and more functional library that both casual and low-level developers could build upon. Documentation See documentation here . Basic Example from pdfmajor.interpreter import PDFInterpreter for page in PDFInterpreter(\"/path/to/pdf.pdf\"): print(\"page start\", page.page_num) for item in page: print(\" >\", item) print(\"page end\", page.page_num) Why Another Fork? PDFMiner was designed to run in python 2.7>, PDFMiner.six was written to bring cross-version support for python 2 and 3. However, after attempting to build and extend upon PDFMiner.six I found it rather difficult to extend and identify additional details regarding the items encoded in the pdf. This library will attempt to expose as much information as possible to end-users, without having them solely rely on to-xml or to-json exports. Features Functional extraction method based on generators Parse, analyze, and convert PDF documents. PDF-1.7 specification support. (well, almost) Font-Color extraction Shape fill and stroke color extraction CJK languages and vertical writing scripts support. Various font types (Type1, TrueType, Type3, and CID) support. Basic encryption (RC4) support. Note : We took out the layout-analysis process in this version (there is no more LTTextHorizontal or LTTextVertical). While the mathematics behind the grouping process was sound, the coupling of the layout-analysis process with the parsing and interpretation process produced unfriendly-code. This feature could be brought back by running your own implementation of it on the pdfmajor.interpreter.PageInterpreter class, but at the current time is not supported. How to Install Source Install Python 3.6.4 or newer. clone this repo git clone https://github.com/asosnovsky/pdfmajor * install repo python setup.py install Pypi Install Python 3.6.4 or newer. install repo pip install pdfmajor Terms and Conditions (This is so-called MIT/X License) Copyright (c) 2018-2019 Ariel Sosnovsky Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.","title":"Home"},{"location":"#pdfmajor","text":"Latest Release PDF-Major is a fork of PDFMiner.six . It is meant to be a more light-weight implementation that makes fewer assumptions on the data. The ultimate goal of this project is to provide a simpler, faster and more functional library that both casual and low-level developers could build upon.","title":"PDFMajor"},{"location":"#documentation","text":"See documentation here .","title":"Documentation"},{"location":"#basic-example","text":"from pdfmajor.interpreter import PDFInterpreter for page in PDFInterpreter(\"/path/to/pdf.pdf\"): print(\"page start\", page.page_num) for item in page: print(\" >\", item) print(\"page end\", page.page_num)","title":"Basic Example"},{"location":"#why-another-fork","text":"PDFMiner was designed to run in python 2.7>, PDFMiner.six was written to bring cross-version support for python 2 and 3. However, after attempting to build and extend upon PDFMiner.six I found it rather difficult to extend and identify additional details regarding the items encoded in the pdf. This library will attempt to expose as much information as possible to end-users, without having them solely rely on to-xml or to-json exports.","title":"Why Another Fork?"},{"location":"#features","text":"Functional extraction method based on generators Parse, analyze, and convert PDF documents. PDF-1.7 specification support. (well, almost) Font-Color extraction Shape fill and stroke color extraction CJK languages and vertical writing scripts support. Various font types (Type1, TrueType, Type3, and CID) support. Basic encryption (RC4) support. Note : We took out the layout-analysis process in this version (there is no more LTTexLine truncated {"config":{"lang":["en"],"prebuild_index":false,"separator":"[\\s\\-]+"},"docs":[{"location":"","text":"PDFMajor Latest Release PDF-Major is a fork of PDFMiner.six . It is meant to be a more light-weight implementation that makes fewer assumptions on the data. The ultimate goal of this project is to provide a simpler, faster and more functional library that both casual and low-level developers could build upon. Documentation See documentation here . Basic Example from pdfmajor.interpreter import PDFInterpreter for page in PDFInterpreter(\"/path/to/pdf.pdf\"): print(\"page start\", page.page_num) for item in page: print(\" >\", item) print(\"page end\", page.page_num) Why Another Fork? PDFMiner was designed to run in python 2.7>, PDFMiner.six was written to bring cross-version support for python 2 and 3. However, after attempting to build and extend upon PDFMiner.six I found it rather difficult to extend and identify additional details regarding the items encoded in the pdf. This library will attempt to expose as much information as possible to end-users, without having them solely rely on to-xml or to-json exports. Features Functional extraction method based on generators Parse, analyze, and convert PDF documents. PDF-1.7 specification support. (well, almost) Font-Color extraction Shape fill and stroke color extraction CJK languages and vertical writing scripts support. Various font types (Type1, TrueType, Type3, and CID) support. Basic encryption (RC4) support. Note : We took out the layout-analysis process in this version (there is no more LTTextHorizontal or LTTextVertical). While the mathematics behind the grouping process was sound, the coupling of the layout-analysis process with the parsing and interpretation process produced unfriendly-code. This feature could be brought back by running your own implementation of it on the pdfmajor.interpreter.PageInterpreter class, but at the current time is not supported. How to Install Source Install Python 3.6.4 or newer. clone this repo git clone https://github.com/asosnovsky/pdfmajor * install repo python setup.py install Pypi Install Python 3.6.4 or newer. install repo pip install pdfmajor Terms and Conditions (This is so-called MIT/X License) Copyright (c) 2018-2019 Ariel Sosnovsky Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.","title":"Home"},{"location":"#pdfmajor","text":"Latest Release PDF-Major is a fork of PDFMiner.six . It is meant to be a more light-weight implementation that makes fewer assumptions on the data. The ultimate goal of this project is to provide a simpler, faster and more functional library that both casual and low-level developers could build upon.","title":"PDFMajor"},{"location":"#documentation","text":"See documentation here .","title":"Documentation"},{"location":"#basic-example","text":"from pdfmajor.interpreter import PDFInterpreter for page in PDFInterpreter(\"/path/to/pdf.pdf\"): print(\"page start\", page.page_num) for item in page: print(\" >\", item) print(\"page end\", page.page_num)","title":"Basic Example"},{"location":"#why-another-fork","text":"PDFMiner was designed to run in python 2.7>, PDFMiner.six was written to bring cross-version support for python 2 and 3. However, after attempting to build and extend upon PDFMiner.six I found it rather difficult to extend and identify additional details regarding the items encoded in the pdf. This library will attempt to expose as much information as possible to end-users, without having them solely rely on to-xml or to-json exports.","title":"Why Another Fork?"},{"location":"#features","text":"Functional extraction method based on generators Parse, analyze, and convert PDF documents. PDF-1.7 specification support. (well, almost) Font-Color extraction Shape fill and stroke color extraction CJK languages and vertical writing scripts support. Various font types (Type1, TrueType, Type3, and CID) support. Basic encryption (RC4) support. Note : We took out the layout-analysis process in this version (there is no more LTTexLine truncated
+2 -2
View File
@@ -2,12 +2,12 @@
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url> <url>
<loc>None</loc> <loc>None</loc>
<lastmod>2019-01-11</lastmod> <lastmod>2019-12-08</lastmod>
<changefreq>daily</changefreq> <changefreq>daily</changefreq>
</url> </url>
<url> <url>
<loc>None</loc> <loc>None</loc>
<lastmod>2019-01-11</lastmod> <lastmod>2019-12-08</lastmod>
<changefreq>daily</changefreq> <changefreq>daily</changefreq>
</url> </url>
</urlset> </urlset>
Binary file not shown.
+1 -1
View File
@@ -97,7 +97,7 @@ OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
""" """
__version__ = (1, 3, 9) __version__ = (1, 3, 13)
if __name__ == '__main__': if __name__ == '__main__':
print( print(
+9 -2
View File
@@ -10,6 +10,7 @@ from .json import convert_to_json
from .text import convert_to_text from .text import convert_to_text
from .yaml import convert_to_yaml from .yaml import convert_to_yaml
from ..utils import logging from ..utils import logging
from ..execptions import ConverterException
def convert_file( def convert_file(
input_file: str, input_file: str,
@@ -22,6 +23,7 @@ def convert_file(
check_extractable: bool = True, check_extractable: bool = True,
pagenos: Optional[List[int]] = None, pagenos: Optional[List[int]] = None,
dont_export_images: bool = False, dont_export_images: bool = False,
ignore_bad_chars: bool = False,
debug_level: int = logging.WARNING, debug_level: int = logging.WARNING,
out_type: str = 'html', out_type: str = 'html',
): ):
@@ -33,7 +35,7 @@ def convert_file(
) )
) )
if re.search(re.escape(out_type) + r'$', output_file ) is None: if re.search(re.escape(out_type) + r'$', output_file ) is None:
raise Exception("Please make sure that the file name and output type match!") raise ConverterException("Please make sure that the file name and output type match!")
if out_type == 'html': if out_type == 'html':
return convert_to_html( return convert_to_html(
input_file_path=input_file, input_file_path=input_file,
@@ -45,6 +47,7 @@ def convert_file(
password=password, password=password,
caching=caching, caching=caching,
check_extractable=check_extractable, check_extractable=check_extractable,
ignore_bad_chars=ignore_bad_chars,
pagenos=pagenos, pagenos=pagenos,
debug_level=debug_level, debug_level=debug_level,
) )
@@ -59,6 +62,7 @@ def convert_file(
caching=caching, caching=caching,
check_extractable=check_extractable, check_extractable=check_extractable,
pagenos=pagenos, pagenos=pagenos,
ignore_bad_chars=ignore_bad_chars,
dont_export_images=dont_export_images, dont_export_images=dont_export_images,
debug_level=debug_level, debug_level=debug_level,
) )
@@ -73,6 +77,7 @@ def convert_file(
caching=caching, caching=caching,
check_extractable=check_extractable, check_extractable=check_extractable,
pagenos=pagenos, pagenos=pagenos,
ignore_bad_chars=ignore_bad_chars,
dont_export_images=dont_export_images, dont_export_images=dont_export_images,
debug_level=debug_level, debug_level=debug_level,
) )
@@ -87,6 +92,7 @@ def convert_file(
caching=caching, caching=caching,
check_extractable=check_extractable, check_extractable=check_extractable,
pagenos=pagenos, pagenos=pagenos,
ignore_bad_chars=ignore_bad_chars,
dont_export_images=dont_export_images, dont_export_images=dont_export_images,
debug_level=debug_level, debug_level=debug_level,
) )
@@ -101,7 +107,8 @@ def convert_file(
caching=caching, caching=caching,
check_extractable=check_extractable, check_extractable=check_extractable,
pagenos=pagenos, pagenos=pagenos,
ignore_bad_chars=ignore_bad_chars,
dont_export_images=dont_export_images, dont_export_images=dont_export_images,
debug_level=debug_level, debug_level=debug_level,
) )
else: raise Exception("Please specify out_type as 'html' or 'xml' or 'json' or 'text' or 'yaml'") else: raise ConverterException("Please specify out_type as 'html' or 'xml' or 'json' or 'text' or 'yaml'")
+2
View File
@@ -16,6 +16,7 @@ def convert_to_html(
maxpages: int = 0, maxpages: int = 0,
password: Optional[str] = None, password: Optional[str] = None,
caching: bool = True, caching: bool = True,
ignore_bad_chars: bool = False,
check_extractable: bool = True, check_extractable: bool = True,
pagenos: List[int] = None, pagenos: List[int] = None,
debug_level: int = logging.WARNING, debug_level: int = logging.WARNING,
@@ -25,6 +26,7 @@ def convert_to_html(
password=password, password=password,
caching=caching, caching=caching,
check_extractable=check_extractable, check_extractable=check_extractable,
ignore_bad_chars=ignore_bad_chars,
pagenos=pagenos, pagenos=pagenos,
debug_level=debug_level debug_level=debug_level
) )
+2
View File
@@ -28,6 +28,7 @@ def convert_to_json(
password: str = Optional[None], password: str = Optional[None],
caching: bool = True, caching: bool = True,
check_extractable: bool = True, check_extractable: bool = True,
ignore_bad_chars: bool = False,
pagenos: Optional[List[int]] = None, pagenos: Optional[List[int]] = None,
debug_level: int = logging.WARNING, debug_level: int = logging.WARNING,
): ):
@@ -36,6 +37,7 @@ def convert_to_json(
password=password, password=password,
caching=caching, caching=caching,
check_extractable=check_extractable, check_extractable=check_extractable,
ignore_bad_chars=ignore_bad_chars,
pagenos=pagenos, pagenos=pagenos,
debug_level=debug_level debug_level=debug_level
) )
+2
View File
@@ -14,6 +14,7 @@ def convert_to_text(
password: str = None, password: str = None,
caching: bool = True, caching: bool = True,
check_extractable: bool = True, check_extractable: bool = True,
ignore_bad_chars: bool = False,
pagenos: List[int] = None, pagenos: List[int] = None,
debug_level: int = logging.WARNING, debug_level: int = logging.WARNING,
): ):
@@ -22,6 +23,7 @@ def convert_to_text(
password=password, password=password,
caching=caching, caching=caching,
check_extractable=check_extractable, check_extractable=check_extractable,
ignore_bad_chars=ignore_bad_chars,
pagenos=pagenos, pagenos=pagenos,
debug_level=debug_level debug_level=debug_level
) )
+3 -2
View File
@@ -2,8 +2,9 @@ from typing import Optional
from io import TextIOWrapper from io import TextIOWrapper
from contextlib import contextmanager from contextlib import contextmanager
from pdfmajor.execptions import FileAccessException
class HTMLMaker: class HTMLMaker:
class AccessError(Exception): pass
def __init__(self, file_path: str, codec: str = 'utf-8'): def __init__(self, file_path: str, codec: str = 'utf-8'):
self.file_path = file_path self.file_path = file_path
self.codec = codec self.codec = codec
@@ -23,7 +24,7 @@ class HTMLMaker:
text = text.encode(self.codec) text = text.encode(self.codec)
self.outfile.write(text) self.outfile.write(text)
else: else:
raise self.AccessError("Attempting to write to file without an open connection") raise FileAccessException("Attempting to write to file without an open connection")
def write(self, text: str, lineend: str = '\n', deep_space: str = ' '): def write(self, text: str, lineend: str = '\n', deep_space: str = ' '):
text = deep_space*self.__levels_deep + text + lineend text = deep_space*self.__levels_deep + text + lineend
+3 -4
View File
@@ -2,6 +2,7 @@ from typing import Set
from json import dumps as json_dump from json import dumps as json_dump
from io import TextIOWrapper from io import TextIOWrapper
from contextlib import contextmanager from contextlib import contextmanager
from pdfmajor.execptions import FileAccessException
@contextmanager @contextmanager
def JSONMaker(file_path: str, codec: str = 'utf-8'): def JSONMaker(file_path: str, codec: str = 'utf-8'):
@@ -10,7 +11,6 @@ def JSONMaker(file_path: str, codec: str = 'utf-8'):
yield obj yield obj
class JSONMakerObject: class JSONMakerObject:
class AccessError(Exception): pass
def __init__(self, outfile: TextIOWrapper, codec: str = 'utf-8', levels_deep: int = 0): def __init__(self, outfile: TextIOWrapper, codec: str = 'utf-8', levels_deep: int = 0):
self.outfile: TextIOWrapper = outfile self.outfile: TextIOWrapper = outfile
self.codec = codec self.codec = codec
@@ -32,7 +32,7 @@ class JSONMakerObject:
text = text.encode(self.codec) text = text.encode(self.codec)
self.outfile.write(text) self.outfile.write(text)
else: else:
raise self.AccessError("Attempting to write to file without an open connection") raise FileAccessException("Attempting to write to file without an open connection")
def write(self, text: str, deep_space: str = ' ', prefix:str = ''): def write(self, text: str, deep_space: str = ' ', prefix:str = ''):
text = prefix + (deep_space*self.levels_deep) + text text = prefix + (deep_space*self.levels_deep) + text
@@ -71,7 +71,6 @@ class JSONMakerObject:
class JSONMakerArray: class JSONMakerArray:
class AccessError(Exception): pass
def __init__(self, outfile: TextIOWrapper, codec: str = 'utf-8', levels_deep: int = 0): def __init__(self, outfile: TextIOWrapper, codec: str = 'utf-8', levels_deep: int = 0):
self.outfile: TextIOWrapper = outfile self.outfile: TextIOWrapper = outfile
self.codec = codec self.codec = codec
@@ -93,7 +92,7 @@ class JSONMakerArray:
text = text.encode(self.codec) text = text.encode(self.codec)
self.outfile.write(text) self.outfile.write(text)
else: else:
raise self.AccessError("Attempting to write to file without an open connection") raise FileAccessException("Attempting to write to file without an open connection")
def write(self, text: str, deep_space: str = ' ', prefix:str = ''): def write(self, text: str, deep_space: str = ' ', prefix:str = ''):
text = prefix + (deep_space*self.levels_deep) + text text = prefix + (deep_space*self.levels_deep) + text
+2 -2
View File
@@ -1,8 +1,8 @@
from io import TextIOWrapper from io import TextIOWrapper
from contextlib import contextmanager from contextlib import contextmanager
from pdfmajor.execptions import FileAccessException
class XMLMaker: class XMLMaker:
class AccessError(Exception): pass
def __init__(self, file_path: str, codec: str = 'utf-8'): def __init__(self, file_path: str, codec: str = 'utf-8'):
self.file_path = file_path self.file_path = file_path
self.codec = codec self.codec = codec
@@ -25,7 +25,7 @@ class XMLMaker:
text = text.encode(self.codec) text = text.encode(self.codec)
self.outfile.write(text) self.outfile.write(text)
else: else:
raise self.AccessError("Attempting to write to file without an open connection") raise FileAccessException("Attempting to write to file without an open connection")
def write(self, text: str, lineend: str = '\n', deep_space: str = ' '): def write(self, text: str, lineend: str = '\n', deep_space: str = ' '):
text = deep_space*self.__levels_deep + text + lineend text = deep_space*self.__levels_deep + text + lineend
+2 -2
View File
@@ -1,6 +1,7 @@
from json import dumps as json_dump from json import dumps as json_dump
from io import TextIOWrapper from io import TextIOWrapper
from contextlib import contextmanager from contextlib import contextmanager
from pdfmajor.execptions import FileAccessException
@contextmanager @contextmanager
def YAMLMaker(file_path: str, codec: str = 'utf-8'): def YAMLMaker(file_path: str, codec: str = 'utf-8'):
@@ -9,7 +10,6 @@ def YAMLMaker(file_path: str, codec: str = 'utf-8'):
yield obj yield obj
class YAMLMakerWriter: class YAMLMakerWriter:
class AccessError(Exception): pass
def __init__(self, outfile: TextIOWrapper, codec: str = 'utf-8', levels_deep: int = -1): def __init__(self, outfile: TextIOWrapper, codec: str = 'utf-8', levels_deep: int = -1):
self.outfile: TextIOWrapper = outfile self.outfile: TextIOWrapper = outfile
self.codec = codec self.codec = codec
@@ -30,7 +30,7 @@ class YAMLMakerWriter:
text = text.encode(self.codec) text = text.encode(self.codec)
self.outfile.write(text) self.outfile.write(text)
else: else:
raise self.AccessError("Attempting to write to file without an open connection") raise FileAccessException("Attempting to write to file without an open connection")
def _write(self, text: str, append_deep: bool = True, deep_space: str = ' ', prefix:str = ''): def _write(self, text: str, append_deep: bool = True, deep_space: str = ' ', prefix:str = ''):
if not append_deep: if not append_deep:
+2
View File
@@ -45,6 +45,7 @@ def convert_to_xml(
maxpages: int = 0, maxpages: int = 0,
password: str = None, password: str = None,
caching: bool = True, caching: bool = True,
ignore_bad_chars: bool = False,
check_extractable: bool = True, check_extractable: bool = True,
pagenos: List[int] = None, pagenos: List[int] = None,
debug_level: int = logging.WARNING, debug_level: int = logging.WARNING,
@@ -54,6 +55,7 @@ def convert_to_xml(
password=password, password=password,
caching=caching, caching=caching,
check_extractable=check_extractable, check_extractable=check_extractable,
ignore_bad_chars=ignore_bad_chars,
pagenos=pagenos, pagenos=pagenos,
debug_level=debug_level debug_level=debug_level
) )
+2
View File
@@ -28,6 +28,7 @@ def convert_to_yaml(
password: str = None, password: str = None,
caching: bool = True, caching: bool = True,
check_extractable: bool = True, check_extractable: bool = True,
ignore_bad_chars: bool = False,
pagenos: List[int] = None, pagenos: List[int] = None,
debug_level: int = logging.WARNING, debug_level: int = logging.WARNING,
): ):
@@ -36,6 +37,7 @@ def convert_to_yaml(
password=password, password=password,
caching=caching, caching=caching,
check_extractable=check_extractable, check_extractable=check_extractable,
ignore_bad_chars=ignore_bad_chars,
pagenos=pagenos, pagenos=pagenos,
debug_level=debug_level debug_level=debug_level
) )
+105
View File
@@ -0,0 +1,105 @@
class PDFMajorException(Exception):
pass
# Interpreter Errors
class InterpreterError(PDFMajorException):
pass
class EmptyDocumentError(InterpreterError):
pass
class CommandProcessorError(InterpreterError):
pass
class InvalidOperation(CommandProcessorError):
pass
class RepeatedCommand(CommandProcessorError):
pass
class FontError(InterpreterError):
pass
class UnicodeNotDefined(FontError):
pass
class MissingFont(FontError):
pass
# Parser Errors
class ParserError(PDFMajorException):
pass
class PDFNoOutlines(ParserError):
pass
class PDFDestinationNotFound(ParserError):
pass
class PDFNoValidXRef(ParserError):
pass
class CMapNotFound(ParserError):
pass
class PSException(ParserError):
pass
class PDFValueError(ParserError):
pass
class PDFObjectNotFound(ParserError):
pass
class PDFException(ParserError):
pass
class PDFTypeError(PDFException):
pass
class PDFSyntaxError(PDFException):
pass
class PDFTextExtractionNotAllowed(PDFException):
pass
class PSEOF(PSException):
pass
class PSSyntaxError(PSException):
pass
class PSTypeError(PSException):
pass
class PSValueError(PSException):
pass
class CCITTExeception(ParserError):
pass
class EOFB(CCITTExeception):
pass
class InvalidData(CCITTExeception):
pass
class ByteSkip(CCITTExeception):
pass
class PDFTextExtractionNotAllowed(ParserError):
pass
# converter error
class ConverterException(PDFMajorException):
pass
class FileAccessException(ConverterException):
pass
+4 -4
View File
@@ -5,9 +5,8 @@ from .commands import PDFStateStack
from .commands import process_command_stream, prep_state from .commands import process_command_stream, prep_state
class PageInterpreter: class PageInterpreter:
class PDFInterpreterError(Exception): pass
def __init__(self, page: PDFPage, page_num: int, font_cache: dict = {}): def __init__(self, page: PDFPage, page_num: int, font_cache: dict = None, ignore_bad_chars = False):
(x0, y0, x1, y1) = page.mediabox (x0, y0, x1, y1) = page.mediabox
if page.rotate == 90: if page.rotate == 90:
ctm = [0, -1, 1, 0, -y0, x1] ctm = [0, -1, 1, 0, -y0, x1]
@@ -19,7 +18,7 @@ class PageInterpreter:
ctm = [1, 0, 0, 1, -x0, -y0] ctm = [1, 0, 0, 1, -x0, -y0]
# Locals # Locals
self.font_cache = font_cache self.font_cache = font_cache if font_cache is not None else {}
self.page = page self.page = page
self.page_num = page_num self.page_num = page_num
self.height = y1-y0 self.height = y1-y0
@@ -30,7 +29,8 @@ class PageInterpreter:
PDFStateStack(), PDFStateStack(),
ctm=ctm, ctm=ctm,
resources=page.resources, resources=page.resources,
font_cache=self.font_cache font_cache=self.font_cache,
ignore_bad_chars=ignore_bad_chars
) )
+7 -3
View File
@@ -1,5 +1,7 @@
from typing import List from typing import List
from pdfmajor.execptions import EmptyDocumentError
from ..parser.PDFPage import PDFPage from ..parser.PDFPage import PDFPage
from ..utils import set_log_level, get_logger, logging from ..utils import set_log_level, get_logger, logging
from .commands.state import PDFStateStack, PDFColorSpace, PREDEFINED_COLORSPACE from .commands.state import PDFStateStack, PDFColorSpace, PREDEFINED_COLORSPACE
@@ -13,7 +15,6 @@ from .PageInterpreter import PageInterpreter
log = get_logger(__name__) log = get_logger(__name__)
class PDFInterpreter: class PDFInterpreter:
class AccessError(Exception): pass
def __init__(self, def __init__(self,
input_file_path: str, input_file_path: str,
@@ -23,6 +24,7 @@ class PDFInterpreter:
password: str = None, password: str = None,
pagenos: List[int] = None, pagenos: List[int] = None,
preload: bool = False, preload: bool = False,
ignore_bad_chars: bool = False,
debug_level: int = logging.WARNING, debug_level: int = logging.WARNING,
): ):
self.input_file_path = input_file_path self.input_file_path = input_file_path
@@ -31,6 +33,7 @@ class PDFInterpreter:
self.check_extractable = check_extractable self.check_extractable = check_extractable
self.password = password self.password = password
self.pagenos = pagenos self.pagenos = pagenos
self.ignore_bad_chars = ignore_bad_chars
self.debug_level = debug_level self.debug_level = debug_level
self.__pages = [] self.__pages = []
if preload: if preload:
@@ -52,12 +55,13 @@ class PDFInterpreter:
check_extractable=self.check_extractable check_extractable=self.check_extractable
) )
for page_num, page in enumerate(pages): for page_num, page in enumerate(pages):
self.__pages.append(PageInterpreter(page, page_num, font_cache)) self.__pages.append(PageInterpreter(page, page_num, font_cache, ignore_bad_chars=self.ignore_bad_chars))
yield self.__pages[-1] yield self.__pages[-1]
log.info(f"Done Reading {len(self.__pages)} pages.") log.info(f"Done Reading {len(self.__pages)} pages.")
set_log_level(logging.WARNING) set_log_level(logging.WARNING)
if len(self.__pages) == 0: if len(self.__pages) == 0:
raise self.AccessError("No pages found in pdf-file") raise EmptyDocumentError("No pages found in pdf-file")
def __iter__(self): def __iter__(self):
if len(self.__pages) > 0: if len(self.__pages) > 0:
+3 -3
View File
@@ -1,5 +1,6 @@
from typing import List from typing import List
from pdfmajor.execptions import RepeatedCommand
from pdfmajor.utils import MATRIX_IDENTITY, mult_matrix from pdfmajor.utils import MATRIX_IDENTITY, mult_matrix
from pdfmajor.parser.PSStackParser import literal_name from pdfmajor.parser.PSStackParser import literal_name
from pdfmajor.parser.PDFStream import PDFStream, list_value, dict_value from pdfmajor.parser.PDFStream import PDFStream, list_value, dict_value
@@ -7,17 +8,16 @@ from pdfmajor.parser.constants import LITERAL_FORM, LITERAL_IMAGE
from .state import PDFStateStack, PDFColorSpace from .state import PDFStateStack, PDFColorSpace
from .state.Curves import CurveMethod, CurvePath, CurvePoint from .state.Curves import CurveMethod, CurvePath, CurvePoint
class PDFCommands: class PDFCommands:
commands = {} commands = {}
class InvalidOperation(Exception): pass
class RepeatedCommand(Exception): pass
@classmethod @classmethod
def __add(cls, cmd_name: str, cmd: callable): def __add(cls, cmd_name: str, cmd: callable):
if not (cmd_name in cls.commands.keys()): if not (cmd_name in cls.commands.keys()):
cls.commands[cmd_name] = cmd cls.commands[cmd_name] = cmd
return cmd return cmd
raise cls.RepeatedCommand(cmd_name) raise RepeatedCommand(cmd_name)
@classmethod @classmethod
def add(cls, *cmd_names: List[str]): def add(cls, *cmd_names: List[str]):
+3 -5
View File
@@ -1,5 +1,6 @@
from typing import List from typing import List
from pdfmajor.execptions import CommandProcessorError
from pdfmajor.parser.PDFContentParser import PDFContentParser, PDFStream from pdfmajor.parser.PDFContentParser import PDFContentParser, PDFStream
from pdfmajor.parser.PSStackParser import PSKeyword, keyword_name from pdfmajor.parser.PSStackParser import PSKeyword, keyword_name
from pdfmajor.utils import get_logger from pdfmajor.utils import get_logger
@@ -17,14 +18,10 @@ from .utils import init_resources
log = get_logger('process_command_stream') log = get_logger('process_command_stream')
class CommandProcessorError(Exception): pass
def process_command_stream(streams: List[PDFStream], font_cache: dict = None, state: PDFStateStack = None): def process_command_stream(streams: List[PDFStream], font_cache: dict = None, state: PDFStateStack = None):
if font_cache is None: if font_cache is None:
font_cache = {} font_cache = {}
parser = PDFContentParser(streams) parser = PDFContentParser(streams)
# from .cmd_record import CmdRecord
# history = CmdRecord()
for obj in parser: for obj in parser:
if isinstance(obj, PSKeyword): if isinstance(obj, PSKeyword):
name = keyword_name(obj) name = keyword_name(obj)
@@ -71,9 +68,10 @@ def process_command_stream(streams: List[PDFStream], font_cache: dict = None, st
yield complete_item yield complete_item
state.complete_layout_items = [] state.complete_layout_items = []
def prep_state(state: PDFStateStack, ctm: tuple, resources: dict, font_cache: dict) -> PDFStateStack: def prep_state(state: PDFStateStack, ctm: tuple, resources: dict, font_cache: dict, ignore_bad_chars: bool = False) -> PDFStateStack:
state.t_matrix = ctm state.t_matrix = ctm
state.resources = resources state.resources = resources
state.text.ignore_bad_chars = ignore_bad_chars
# set some global states. # set some global states.
state.graphics.ncolspace = state.graphics.scolspace = None state.graphics.ncolspace = state.graphics.scolspace = None
@@ -1,9 +0,0 @@
class CmdRecord:
def __init__(self, file_name: str = "./cmd_record"):
self.file = open(file_name, 'w')
self.file.write("c_name,func_name,args\n")
def write(self, c_name, func_name, args):
self.file.write(f"{c_name},{func_name},{args}\n")
def close(self):
self.file.close()
+10 -9
View File
@@ -1,3 +1,4 @@
from pdfmajor.execptions import InvalidOperation
from pdfmajor.utils import mult_matrix, MATRIX_IDENTITY, apply_matrix_pt, get_logger from pdfmajor.utils import mult_matrix, MATRIX_IDENTITY, apply_matrix_pt, get_logger
from pdfmajor.parser.PSStackParser import literal_name from pdfmajor.parser.PSStackParser import literal_name
from pdfmajor.parser.PDFStream import PDFStream, list_value, dict_value from pdfmajor.parser.PDFStream import PDFStream, list_value, dict_value
@@ -253,7 +254,7 @@ def set_stroke_colorspace(stack: PDFStateStack, name: str) -> PDFStateStack:
stack.graphics.scolspace = stack.colorspace_map[literal_name(name)] stack.graphics.scolspace = stack.colorspace_map[literal_name(name)]
return stack return stack
except KeyError: except KeyError:
raise PDFCommands.InvalidOperation('Undefined ColorSpace: %r' % name) raise InvalidOperation('Undefined ColorSpace: %r' % name)
# setcolorspace-non-strokine # setcolorspace-non-strokine
@PDFCommands.add('cs') @PDFCommands.add('cs')
@@ -262,7 +263,7 @@ def set_nonstroke_colorspace(stack: PDFStateStack, name: str) -> PDFStateStack:
stack.graphics.ncolspace = stack.colorspace_map[literal_name(name)] stack.graphics.ncolspace = stack.colorspace_map[literal_name(name)]
return stack return stack
except KeyError: except KeyError:
raise PDFCommands.InvalidOperation('Undefined ColorSpace: %r' % name) raise InvalidOperation('Undefined ColorSpace: %r' % name)
# setgray-stroking # setgray-stroking
@PDFCommands.add('G') @PDFCommands.add('G')
@@ -326,7 +327,7 @@ def set_nonstroke_cmyk(stack: PDFStateStack, c, m, y, k) -> PDFStateStack:
@PDFCommands.add('SCN','SC') @PDFCommands.add('SCN','SC')
def set_stroke_colorspace_custom(stack: PDFStateStack) -> PDFStateStack: def set_stroke_colorspace_custom(stack: PDFStateStack) -> PDFStateStack:
if stack.graphics.scolspace is None: if stack.graphics.scolspace is None:
raise PDFCommands.InvalidOperation('No colorspace specified!') raise InvalidOperation('No colorspace specified!')
components = stack.pop(stack.graphics.scolspace.ncomponents) components = stack.pop(stack.graphics.scolspace.ncomponents)
stack.graphics.set_stroke_color( stack.graphics.set_stroke_color(
stack.graphics.scolspace, stack.graphics.scolspace,
@@ -337,7 +338,7 @@ def set_stroke_colorspace_custom(stack: PDFStateStack) -> PDFStateStack:
@PDFCommands.add('scn','sc') @PDFCommands.add('scn','sc')
def set_nonstroke_colorspace_custom(stack: PDFStateStack) -> PDFStateStack: def set_nonstroke_colorspace_custom(stack: PDFStateStack) -> PDFStateStack:
if stack.graphics.ncolspace is None: if stack.graphics.ncolspace is None:
raise PDFCommands.InvalidOperation('No colorspace specified!') raise InvalidOperation('No colorspace specified!')
components = stack.pop(stack.graphics.ncolspace.ncomponents) components = stack.pop(stack.graphics.ncolspace.ncomponents)
stack.graphics.set_nostroke_color( stack.graphics.set_nostroke_color(
stack.graphics.ncolspace, stack.graphics.ncolspace,
@@ -353,7 +354,7 @@ def begin_text(stack: PDFStateStack) -> PDFStateStack:
if stack.current_textblock is None: if stack.current_textblock is None:
stack.current_textblock = LTTextBlock() stack.current_textblock = LTTextBlock()
else: else:
raise PDFCommands.InvalidOperation("Last TextBlock did not clear") raise InvalidOperation("Last TextBlock did not clear")
return stack return stack
@PDFCommands.add('ET') @PDFCommands.add('ET')
@@ -394,7 +395,7 @@ def set_font(stack: PDFStateStack, fontid, fontsize) -> PDFStateStack:
stack.text.font = stack.fontmap[literal_name(fontid)] stack.text.font = stack.fontmap[literal_name(fontid)]
return stack return stack
except KeyError: except KeyError:
raise PDFCommands.InvalidOperation('Undefined Font id: %r' % fontid) raise InvalidOperation('Undefined Font id: %r' % fontid)
# setrendering # setrendering
@PDFCommands.add('Tr') @PDFCommands.add('Tr')
@@ -444,9 +445,9 @@ def nextline(stack: PDFStateStack) -> PDFStateStack:
@PDFCommands.add('TJ') @PDFCommands.add('TJ')
def add_char_blocks(stack: PDFStateStack, seq: bytearray) -> PDFStateStack: def add_char_blocks(stack: PDFStateStack, seq: bytearray) -> PDFStateStack:
if stack.text.font is None: if stack.text.font is None:
raise PDFCommands.InvalidOperation("No Font Specified") raise InvalidOperation("No Font Specified")
if stack.current_textblock is None: if stack.current_textblock is None:
raise PDFCommands.InvalidOperation("No TextBlock initilized") raise InvalidOperation("No TextBlock initilized")
stack.current_textblock.add_char_block( stack.current_textblock.add_char_block(
seq, seq,
stack.t_matrix, stack.t_matrix,
@@ -487,7 +488,7 @@ def insert_xobject(stack: PDFStateStack, xobjid) -> PDFStateStack:
try: try:
xobj = PDFStream.validated_stream(stack.xobjmap[xobjid]) xobj = PDFStream.validated_stream(stack.xobjmap[xobjid])
except KeyError: except KeyError:
raise PDFCommands.InvalidOperation('Undefined xobject id: %r' % xobjid) raise InvalidOperation('Undefined xobject id: %r' % xobjid)
# log.info('Processing xobj: %r', xobj) # log.info('Processing xobj: %r', xobj)
subtype = xobj.get('Subtype') subtype = xobj.get('Subtype')
if subtype is LITERAL_FORM and 'BBox' in xobj: if subtype is LITERAL_FORM and 'BBox' in xobj:
@@ -2,7 +2,6 @@ from .PDFColor import PDFColor, PDFColorSpace
from .PDFColorSpace import PREDEFINED_COLORSPACE from .PDFColorSpace import PREDEFINED_COLORSPACE
class PDFGraphicState(object): class PDFGraphicState(object):
class InvalidOperation(Exception): pass
def __init__(self): def __init__(self):
self.linewidth = 0 self.linewidth = 0
@@ -58,7 +58,7 @@ class PDFText(PDFItem):
if needcharspace: if needcharspace:
textstate.linematrix[idx] += textstate.charspace textstate.linematrix[idx] += textstate.charspace
text = textstate.font.to_unichr(cid) text = textstate.to_unichr(cid)
assert isinstance(text, str), str(type(text)) assert isinstance(text, str), str(type(text))
matrix = translate_matrix(textstate.matrix, textstate.linematrix) matrix = translate_matrix(textstate.matrix, textstate.linematrix)
@@ -42,7 +42,7 @@ class TextDecoder:
if needcharspace: if needcharspace:
textstate.linematrix[idx] += textstate.charspace textstate.linematrix[idx] += textstate.charspace
text = textstate.font.to_unichr(cid) text = textstate.to_unichr(cid)
assert isinstance(text, str), str(type(text)) assert isinstance(text, str), str(type(text))
matrix = translate_matrix(textstate.matrix, textstate.linematrix) matrix = translate_matrix(textstate.matrix, textstate.linematrix)
@@ -2,6 +2,7 @@
from abc import abstractmethod from abc import abstractmethod
from io import BytesIO from io import BytesIO
from pdfmajor.execptions import UnicodeNotDefined
from pdfmajor.parser.EncodingDB import EncodingDB from pdfmajor.parser.EncodingDB import EncodingDB
from pdfmajor.parser.PSStackParser import LIT from pdfmajor.parser.PSStackParser import LIT
from pdfmajor.parser.PSStackParser import PSLiteral from pdfmajor.parser.PSStackParser import PSLiteral
@@ -10,11 +11,11 @@ from pdfmajor.parser.PDFStream import resolve1
from pdfmajor.parser.PDFStream import int_value from pdfmajor.parser.PDFStream import int_value
from pdfmajor.parser.PDFStream import num_value from pdfmajor.parser.PDFStream import num_value
from pdfmajor.parser.PDFStream import list_value from pdfmajor.parser.PDFStream import list_value
from pdfmajor.parser.PDFStream import PDFException
from pdfmajor.parser.PDFStream import PDFStream from pdfmajor.parser.PDFStream import PDFStream
from pdfmajor.parser.cmapdb import FileUnicodeMap from pdfmajor.parser.cmapdb import FileUnicodeMap
from pdfmajor.parser.cmapdb import CMapParser from pdfmajor.parser.cmapdb import CMapParser
## Literals ## Literals
## ##
LITERAL_STANDARD_ENCODING = LIT('StandardEncoding') LITERAL_STANDARD_ENCODING = LIT('StandardEncoding')
@@ -22,8 +23,6 @@ LITERAL_TYPE1C = LIT('Type1C')
class PDFFont(object): class PDFFont(object):
class PDFFontError(PDFException): pass
class PDFUnicodeNotDefined(PDFFontError): pass
def __init__(self, descriptor, widths, default_width=None): def __init__(self, descriptor, widths, default_width=None):
self.descriptor = descriptor self.descriptor = descriptor
@@ -77,7 +76,7 @@ class PDFFont(object):
except KeyError: except KeyError:
try: try:
return self.widths[self.to_unichr(cid)] * self.hscale return self.widths[self.to_unichr(cid)] * self.hscale
except (KeyError, self.PDFUnicodeNotDefined): except (KeyError, UnicodeNotDefined):
return self.default_width * self.hscale return self.default_width * self.hscale
def char_disp(self, cid): def char_disp(self, cid):
@@ -137,4 +136,4 @@ class PDFSimpleFont(PDFFont):
try: try:
return self.cid2unicode[cid] return self.cid2unicode[cid]
except KeyError: except KeyError:
raise self.PDFUnicodeNotDefined(None, cid) raise UnicodeNotDefined(None, cid)
@@ -1,13 +1,10 @@
## TrueTypeFont ## TrueTypeFont
## ##
import struct import struct
from pdfmajor.execptions import CMapNotFound
from pdfmajor.parser.cmapdb import FileUnicodeMap from pdfmajor.parser.cmapdb import FileUnicodeMap
class TrueTypeFont(object): class TrueTypeFont(object):
class CMapNotFound(Exception):
pass
def __init__(self, name, fp): def __init__(self, name, fp):
self.name = name self.name = name
self.fp = fp self.fp = fp
@@ -27,7 +24,7 @@ class TrueTypeFont(object):
def create_unicode_map(self): def create_unicode_map(self):
if 'cmap' not in self.tables: if 'cmap' not in self.tables:
raise TrueTypeFont.CMapNotFound raise CMapNotFound
(base_offset, _) = self.tables['cmap'] (base_offset, _) = self.tables['cmap']
fp = self.fp fp = self.fp
fp.seek(base_offset) fp.seek(base_offset)
@@ -1,5 +1,6 @@
from pdfmajor.execptions import PSEOF
from pdfmajor.parser.PSStackParser import PSStackParser from pdfmajor.parser.PSStackParser import PSStackParser
from pdfmajor.parser.PSStackParser import PSEOF, KWD from pdfmajor.parser.PSStackParser import KWD
from pdfmajor.parser.PSStackParser import PSLiteral from pdfmajor.parser.PSStackParser import PSLiteral
from pdfmajor.parser.PSStackParser import literal_name from pdfmajor.parser.PSStackParser import literal_name
from pdfmajor.parser.EncodingDB import name2unicode from pdfmajor.parser.EncodingDB import name2unicode
@@ -1,3 +1,4 @@
from pdfmajor.execptions import FontError
from pdfmajor.utils import get_logger, settings from pdfmajor.utils import get_logger, settings
from pdfmajor.parser.PSStackParser import literal_name from pdfmajor.parser.PSStackParser import literal_name
from pdfmajor.parser.PDFStream import list_value from pdfmajor.parser.PDFStream import list_value
@@ -16,13 +17,13 @@ def get_font(objid: int, spec: dict, cached_fonts: dict = {}):
log.debug('get_font: create: objid=%r, spec=%r', objid, spec) log.debug('get_font: create: objid=%r, spec=%r', objid, spec)
if settings.STRICT: if settings.STRICT:
if spec['Type'] is not LITERAL_FONT: if spec['Type'] is not LITERAL_FONT:
raise PDFFont.PDFFontError('Type is not /Font') raise FontError('Type is not /Font')
# Create a Font object. # Create a Font object.
if 'Subtype' in spec: if 'Subtype' in spec:
subtype = literal_name(spec['Subtype']) subtype = literal_name(spec['Subtype'])
else: else:
if settings.STRICT: if settings.STRICT:
raise PDFFont.PDFFontError('Font Subtype is not specified.') raise FontError('Font Subtype is not specified.')
subtype = 'Type1' subtype = 'Type1'
if subtype in ('Type1', 'MMType1'): if subtype in ('Type1', 'MMType1'):
# Type1 Font # Type1 Font
@@ -47,7 +48,7 @@ def get_font(objid: int, spec: dict, cached_fonts: dict = {}):
font = get_font(None, subspec, cached_fonts) font = get_font(None, subspec, cached_fonts)
else: else:
if settings.STRICT: if settings.STRICT:
raise PDFFont.PDFFontError('Invalid Font spec: %r' % spec) raise FontError('Invalid Font spec: %r' % spec)
font = PDFType1Font(spec) # this is so wrong! font = PDFType1Font(spec) # this is so wrong!
if objid: if objid:
cached_fonts[objid] = font cached_fonts[objid] = font
@@ -1,5 +1,6 @@
from io import BytesIO from io import BytesIO
from pdfmajor.execptions import FontError, UnicodeNotDefined, CMapNotFound
from pdfmajor.parser.PSStackParser import literal_name from pdfmajor.parser.PSStackParser import literal_name
from pdfmajor.parser.PDFStream import int_value from pdfmajor.parser.PDFStream import int_value
from pdfmajor.parser.PDFStream import num_value from pdfmajor.parser.PDFStream import num_value
@@ -24,7 +25,7 @@ class PDFType1Font(PDFSimpleFont):
self.basefont = literal_name(spec['BaseFont']) self.basefont = literal_name(spec['BaseFont'])
except KeyError: except KeyError:
if settings.STRICT: if settings.STRICT:
raise self.PDFFontError('BaseFont is missing') raise FontError('BaseFont is missing')
self.basefont = 'unknown' self.basefont = 'unknown'
try: try:
(descriptor, widths) = FontMetricsDB.get_metrics(self.basefont) (descriptor, widths) = FontMetricsDB.get_metrics(self.basefont)
@@ -86,7 +87,7 @@ class PDFCIDFont(PDFFont):
self.basefont = literal_name(spec['BaseFont']) self.basefont = literal_name(spec['BaseFont'])
except KeyError: except KeyError:
if strict: if strict:
raise self.PDFFontError('BaseFont is missing') raise FontError('BaseFont is missing')
self.basefont = 'unknown' self.basefont = 'unknown'
self.cidsysteminfo = dict_value(spec.get('CIDSystemInfo', {})) self.cidsysteminfo = dict_value(spec.get('CIDSystemInfo', {}))
self.cidcoding = '%s-%s' % (resolve1(self.cidsysteminfo.get('Registry', b'unknown')).decode("latin1"), self.cidcoding = '%s-%s' % (resolve1(self.cidsysteminfo.get('Registry', b'unknown')).decode("latin1"),
@@ -95,19 +96,19 @@ class PDFCIDFont(PDFFont):
name = literal_name(spec['Encoding']) name = literal_name(spec['Encoding'])
except KeyError: except KeyError:
if strict: if strict:
raise self.PDFFontError('Encoding is unspecified') raise FontError('Encoding is unspecified')
name = 'unknown' name = 'unknown'
try: try:
self.cmap = CMapDB.get_cmap(name) self.cmap = CMapDB.get_cmap(name)
except CMapDB.CMapNotFound as e: except CMapNotFound as e:
if strict: if strict:
raise self.PDFFontError(e) raise FontError(e)
self.cmap = CMap() self.cmap = CMap()
try: try:
descriptor = dict_value(spec['FontDescriptor']) descriptor = dict_value(spec['FontDescriptor'])
except KeyError: except KeyError:
if strict: if strict:
raise self.PDFFontError('FontDescriptor is missing') raise FontError('FontDescriptor is missing')
descriptor = {} descriptor = {}
ttf = None ttf = None
if 'FontFile2' in descriptor: if 'FontFile2' in descriptor:
@@ -123,12 +124,12 @@ class PDFCIDFont(PDFFont):
if ttf: if ttf:
try: try:
self.unicode_map = ttf.create_unicode_map() self.unicode_map = ttf.create_unicode_map()
except TrueTypeFont.CMapNotFound: except CMapNotFound:
pass pass
else: else:
try: try:
self.unicode_map = CMapDB.get_unicode_map(self.cidcoding, self.cmap.is_vertical()) self.unicode_map = CMapDB.get_unicode_map(self.cidcoding, self.cmap.is_vertical())
except CMapDB.CMapNotFound as e: except CMapNotFound as e:
pass pass
self.vertical = self.cmap.is_vertical() self.vertical = self.cmap.is_vertical()
@@ -171,4 +172,4 @@ class PDFCIDFont(PDFFont):
raise KeyError(cid) raise KeyError(cid)
return self.unicode_map.get_unichr(cid) return self.unicode_map.get_unichr(cid)
except KeyError: except KeyError:
raise self.PDFUnicodeNotDefined(self.cidcoding, cid) raise UnicodeNotDefined(self.cidcoding, cid)
@@ -1,5 +1,6 @@
from .PDFFont import PDFFont, get_font from pdfmajor.execptions import MissingFont, UnicodeNotDefined
from pdfmajor.utils import MATRIX_IDENTITY from pdfmajor.utils import MATRIX_IDENTITY
from .PDFFont import PDFFont, get_font
## PDFTextState ## PDFTextState
## ##
@@ -16,14 +17,27 @@ class PDFTextState(object):
self.rise: int = 0 self.rise: int = 0
self.matrix = MATRIX_IDENTITY self.matrix = MATRIX_IDENTITY
self.linematrix = [0, 0] self.linematrix = [0, 0]
self.ignore_bad_chars = False
def __repr__(self): def __repr__(self):
return ('<PDFTextState: font=%r, fontsize=%r, charspace=%r, wordspace=%r, ' return ('<PDFTextState: font=%r, fontsize=%r, charspace=%r, wordspace=%r, '
' scaling=%r, leading=%r, render=%r, rise=%r, ' ' scaling=%r, leading=%r, render=%r, rise=%r, '
' matrix=%r, linematrix=%r>' % ' matrix=%r, linematrix=%r ignore_bad_chars=%r>' %
(self.font, self.fontsize, self.charspace, self.wordspace, (self.font, self.fontsize, self.charspace, self.wordspace,
self.scaling, self.leading, self.render, self.rise, self.scaling, self.leading, self.render, self.rise,
self.matrix, self.linematrix)) self.matrix, self.linematrix, self.ignore_bad_chars))
def to_unichr(self, cid: int) -> str:
if self.font is None:
raise MissingFont
try:
return self.font.to_unichr(cid)
except UnicodeNotDefined as e:
if self.ignore_bad_chars:
return ""
else:
raise e
def copy(self): def copy(self):
obj = self.__class__() obj = self.__class__()
@@ -37,6 +51,7 @@ class PDFTextState(object):
obj.rise = self.rise obj.rise = self.rise
obj.matrix = self.matrix obj.matrix = self.matrix
obj.linematrix = self.linematrix obj.linematrix = self.linematrix
obj.ignore_bad_chars = self.ignore_bad_chars
return obj return obj
def reset_matrices(self): def reset_matrices(self):
@@ -13,7 +13,7 @@ class LTComponent(LTItem):
if isinstance(bbox, Bbox): if isinstance(bbox, Bbox):
self.bbox = bbox self.bbox = bbox
else: else:
raise Exception("Invalid BBox") raise AssertionError("Expected a BBox")
def __repr__(self): def __repr__(self):
return ('<%s %s>' % return ('<%s %s>' %
@@ -33,7 +33,7 @@ def __render_string_along(idx: int, seq: bytearray,
if needcharspace: if needcharspace:
textstate.linematrix[idx] += textstate.charspace textstate.linematrix[idx] += textstate.charspace
text = textstate.font.to_unichr(cid) text = textstate.to_unichr(cid)
assert isinstance(text, str), str(type(text)) assert isinstance(text, str), str(type(text))
matrix = translate_matrix(textstate.matrix, textstate.linematrix) matrix = translate_matrix(textstate.matrix, textstate.linematrix)
+3 -3
View File
@@ -4,11 +4,11 @@ import operator
from typing import List from typing import List
from io import BytesIO from io import BytesIO
from pdfmajor.execptions import PSEOF, PSTypeError
from ..utils import int2byte, choplist, settings from ..utils import int2byte, choplist, settings
from .PSStackParser import literal_name from .PSStackParser import literal_name
from .PSStackParser import PSStackParser from .PSStackParser import PSStackParser
from .PSStackParser import PSEOF, PSTypeError
from .PSStackParser import KWD from .PSStackParser import KWD
from .PDFStream import PDFStream from .PDFStream import PDFStream
@@ -101,9 +101,9 @@ class PDFContentParser(PSStackParser):
obj = PDFStream(d, data) obj = PDFStream(d, data)
self.push((pos, obj)) self.push((pos, obj))
self.push((pos, self.KEYWORD_EI)) self.push((pos, self.KEYWORD_EI))
except PSTypeError: except PSTypeError as e:
if settings.STRICT: if settings.STRICT:
raise raise e
else: else:
self.push((pos, token)) self.push((pos, token))
return return
@@ -3,7 +3,6 @@ import hashlib as md5
from Crypto.Cipher import ARC4, AES from Crypto.Cipher import ARC4, AES
from Crypto.Hash import SHA256 from Crypto.Hash import SHA256
from .exceptions import * # pylint: disable=W0614
from ..PDFStream import int_value, str_value, dict_value from ..PDFStream import int_value, str_value, dict_value
from ..PSStackParser import literal_name from ..PSStackParser import literal_name
from ...utils import int2byte, struct from ...utils import int2byte, struct
+2 -2
View File
@@ -2,15 +2,15 @@ import re
from logging import getLogger from logging import getLogger
from typing import Tuple, Optional, Dict from typing import Tuple, Optional, Dict
from pdfmajor.execptions import PSEOF, PDFSyntaxError
from ...utils import settings, choplist, nunpack from ...utils import settings, choplist, nunpack
from ..PSStackParser import PSEOF
from ..PSStackParser import KWD from ..PSStackParser import KWD
from ..PDFParser import PDFStreamParser from ..PDFParser import PDFStreamParser
from ..PDFStream import PDFStream from ..PDFStream import PDFStream
from ..PDFStream import dict_value from ..PDFStream import dict_value
from .constants import LITERAL_OBJSTM, LITERAL_XREF from .constants import LITERAL_OBJSTM, LITERAL_XREF
from .exceptions import PDFNoValidXRef, PDFSyntaxError
log = getLogger(__name__) log = getLogger(__name__)
+13 -5
View File
@@ -4,15 +4,23 @@ from Crypto.Cipher import ARC4, AES
from Crypto.Hash import SHA256 from Crypto.Hash import SHA256
from logging import getLogger from logging import getLogger
from pdfmajor.execptions import (
ParserError,
PDFNoOutlines,
PDFDestinationNotFound,
PDFNoValidXRef,
PSEOF,
PDFObjectNotFound,
PDFTypeError,
PDFSyntaxError
)
from ...utils import settings, choplist, decode_text from ...utils import settings, choplist, decode_text
from ..PSStackParser import KWD from ..PSStackParser import KWD
from ..PSStackParser import literal_name from ..PSStackParser import literal_name
from ..PSStackParser import PSEOF
from ..PDFStream import int_value, str_value, dict_value, list_value, decipher_all from ..PDFStream import int_value, str_value, dict_value, list_value, decipher_all
from ..PDFStream import PDFObjectNotFound, PDFTypeError from ..PDFParser import PDFStreamParser, PDFStream
from ..PDFParser import PDFStreamParser, PDFStream, PDFSyntaxError
from .exceptions import *
from .PDFSecurityHandler import PDFStandardSecurityHandler from .PDFSecurityHandler import PDFStandardSecurityHandler
from .PDFSecurityHandler import PDFStandardSecurityHandlerV4 from .PDFSecurityHandler import PDFStandardSecurityHandlerV4
from .PDFSecurityHandler import PDFStandardSecurityHandlerV5 from .PDFSecurityHandler import PDFStandardSecurityHandlerV5
@@ -181,7 +189,7 @@ class PDFDocument(object):
def getobj(self, objid): def getobj(self, objid):
assert objid != 0 assert objid != 0
if not self.xrefs: if not self.xrefs:
raise PDFException('PDFDocument is not initialized') raise ParserError('PDFDocument is not initialized')
log.debug('getobj: objid=%r', objid) log.debug('getobj: objid=%r', objid)
if objid in self._cached_objs: if objid in self._cached_objs:
(obj, genno) = self._cached_objs[objid] (obj, genno) = self._cached_objs[objid]
-22
View File
@@ -1,22 +0,0 @@
from ..PDFParser import PDFSyntaxError
from ..PDFStream import PDFException
## Exceptions
##
class PDFNoValidXRef(PDFSyntaxError):
pass
class PDFNoOutlines(PDFException):
pass
class PDFDestinationNotFound(PDFException):
pass
class PDFEncryptionError(PDFException):
pass
class PDFPasswordIncorrect(PDFEncryptionError):
pass
class PDFTextExtractionNotAllowed(PDFEncryptionError):
pass
+3 -2
View File
@@ -1,11 +1,12 @@
import logging import logging
from pdfmajor.execptions import PDFTextExtractionNotAllowed, PDFObjectNotFound
from ..utils import settings from ..utils import settings
from .constants import LIT from .constants import LIT
from .PDFStream import PDFObjectNotFound
from .PDFStream import resolve1, int_value, list_value, dict_value from .PDFStream import resolve1, int_value, list_value, dict_value
from .PDFParser import PDFParser from .PDFParser import PDFParser
from .PDFDocument import PDFDocument, PDFTextExtractionNotAllowed from .PDFDocument import PDFDocument
from ..utils import get_logger from ..utils import get_logger
+5 -9
View File
@@ -2,12 +2,13 @@
import logging import logging
from io import BytesIO, TextIOWrapper from io import BytesIO, TextIOWrapper
from ..utils import settings from ..utils import settings
from pdfmajor.execptions import (
PSSyntaxError,
PSEOF,
PDFSyntaxError
)
from .PSStackParser import PSStackParser from .PSStackParser import PSStackParser
from .PSStackParser import PSSyntaxError
from .PSStackParser import PSEOF
from .PSStackParser import KWD from .PSStackParser import KWD
from .PDFStream import PDFException
from .PDFStream import PDFStream from .PDFStream import PDFStream
from .PDFStream.PDFObjRef import PDFObjRef from .PDFStream.PDFObjRef import PDFObjRef
from .PDFStream import int_value from .PDFStream import int_value
@@ -16,11 +17,6 @@ from .PDFStream import dict_value
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
## Exceptions
##
class PDFSyntaxError(PDFException):
pass
## PDFParser ## PDFParser
## ##
+1 -2
View File
@@ -1,12 +1,11 @@
import zlib import zlib
from ..PSStackParser import PSException from pdfmajor.execptions import PDFValueError, PDFObjectNotFound
from ..PSStackParser import PSObject from ..PSStackParser import PSObject
from ...utils import lzwdecode, settings from ...utils import lzwdecode, settings
from .constants import * # pylint: disable=W0614 from .constants import * # pylint: disable=W0614
from .types import * # pylint: disable=W0614 from .types import * # pylint: disable=W0614
from .exceptions import * # pylint: disable=W0614
## PDFObjRef ## PDFObjRef
## ##
-1
View File
@@ -5,7 +5,6 @@ from ...utils import ascii85decode, asciihexdecode, apply_png_predictor
from .constants import * from .constants import *
from .types import * from .types import *
from .exceptions import *
from .util import * from .util import *
from .ccitt import ccittfaxdecode from .ccitt import ccittfaxdecode
+9 -17
View File
@@ -12,6 +12,7 @@
import sys import sys
import array import array
from pdfmajor.execptions import EOFB, InvalidData, ByteSkip
from abc import abstractmethod from abc import abstractmethod
def get_bytes(data): def get_bytes(data):
@@ -319,15 +320,6 @@ class CCITTG4Parser(BitParser):
BitParser.add(UNCOMPRESSED, 'T00000', '00000000011') BitParser.add(UNCOMPRESSED, 'T00000', '00000000011')
BitParser.add(UNCOMPRESSED, 'T10000', '00000000010') BitParser.add(UNCOMPRESSED, 'T10000', '00000000010')
class EOFB(Exception):
pass
class InvalidData(Exception):
pass
class ByteSkip(Exception):
pass
def __init__(self, width, bytealign=False): def __init__(self, width, bytealign=False):
BitParser.__init__(self) BitParser.__init__(self)
self.width = width self.width = width
@@ -340,10 +332,10 @@ class CCITTG4Parser(BitParser):
try: try:
for m in (128, 64, 32, 16, 8, 4, 2, 1): for m in (128, 64, 32, 16, 8, 4, 2, 1):
self._parse_bit(byte & m) self._parse_bit(byte & m)
except self.ByteSkip: except ByteSkip:
self._accept = self._parse_mode self._accept = self._parse_mode
self._state = self.MODE self._state = self.MODE
except self.EOFB: except EOFB:
break break
return return
@@ -363,17 +355,17 @@ class CCITTG4Parser(BitParser):
self._accept = self._parse_uncompressed self._accept = self._parse_uncompressed
return self.UNCOMPRESSED return self.UNCOMPRESSED
elif mode == 'e': elif mode == 'e':
raise self.EOFB raise EOFB
elif isinstance(mode, int): elif isinstance(mode, int):
self._do_vertical(mode) self._do_vertical(mode)
self._flush_line() self._flush_line()
return self.MODE return self.MODE
else: else:
raise self.InvalidData(mode) raise InvalidData(mode)
def _parse_horiz1(self, n): def _parse_horiz1(self, n):
if n is None: if n is None:
raise self.InvalidData raise InvalidData
self._n1 += n self._n1 += n
if n < 64: if n < 64:
self._n2 = 0 self._n2 = 0
@@ -386,7 +378,7 @@ class CCITTG4Parser(BitParser):
def _parse_horiz2(self, n): def _parse_horiz2(self, n):
if n is None: if n is None:
raise self.InvalidData raise InvalidData
self._n2 += n self._n2 += n
if n < 64: if n < 64:
self._color = 1-self._color self._color = 1-self._color
@@ -401,7 +393,7 @@ class CCITTG4Parser(BitParser):
def _parse_uncompressed(self, bits): def _parse_uncompressed(self, bits):
if not bits: if not bits:
raise self.InvalidData raise InvalidData
if bits.startswith('T'): if bits.startswith('T'):
self._accept = self._parse_mode self._accept = self._parse_mode
self._color = int(bits[1]) self._color = int(bits[1])
@@ -449,7 +441,7 @@ class CCITTG4Parser(BitParser):
self._y += 1 self._y += 1
self._reset_line() self._reset_line()
if self.bytealign: if self.bytealign:
raise self.ByteSkip raise ByteSkip
return return
def _do_vertical(self, dx): def _do_vertical(self, dx):
-16
View File
@@ -1,16 +0,0 @@
from ..PSStackParser import PSException
class PDFException(PSException):
pass
class PDFTypeError(PDFException):
pass
class PDFValueError(PDFException):
pass
class PDFObjectNotFound(PDFException):
pass
class PDFNotImplementedError(PDFException):
pass
-1
View File
@@ -1,6 +1,5 @@
from .PDFObjRef import PDFObjRef from .PDFObjRef import PDFObjRef
from ...utils import settings, isnumber, int2byte from ...utils import settings, isnumber, int2byte
from .exceptions import PDFTypeError
from logging import getLogger from logging import getLogger
@@ -4,9 +4,10 @@ log = logging.getLogger(__name__)
from io import TextIOWrapper from io import TextIOWrapper
from typing import List, Tuple, Union from typing import List, Tuple, Union
from pdfmajor.execptions import PSEOF
from ...utils import int2byte from ...utils import int2byte
from .exceptions import PSEOF
from .constants import EOL, NONSPC, KWD, END_LITERAL, LIT, HEX from .constants import EOL, NONSPC, KWD, END_LITERAL, LIT, HEX
from .constants import SPC, HEX_PAIR from .constants import SPC, HEX_PAIR
from .constants import END_NUMBER, END_STRING, END_KEYWORD, END_HEX_STRING from .constants import END_NUMBER, END_STRING, END_KEYWORD, END_HEX_STRING
+9 -8
View File
@@ -1,11 +1,12 @@
import logging import logging
from io import TextIOWrapper from io import TextIOWrapper
from pdfmajor.execptions import PSSyntaxError, PSTypeError, PSEOF
from ...utils import choplist, settings from ...utils import choplist, settings
from .PSBaseParser import PSBaseParser from .PSBaseParser import PSBaseParser
from .types import * from .types import *
from .exceptions import *
from .constants import * from .constants import *
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@@ -121,9 +122,9 @@ class PSStackParser(PSBaseParser):
# end array # end array
try: try:
self.push(self.end_type('a')) self.push(self.end_type('a'))
except PSTypeError: except PSTypeError as e:
if settings.STRICT: if settings.STRICT:
raise raise e
elif token == KEYWORD_DICT_BEGIN: elif token == KEYWORD_DICT_BEGIN:
# begin dictionary # begin dictionary
self.start_type(pos, 'd') self.start_type(pos, 'd')
@@ -136,9 +137,9 @@ class PSStackParser(PSBaseParser):
# construct a Python dictionary. # construct a Python dictionary.
d = dict((literal_name(k), v) for (k, v) in choplist(2, objs) if v is not None) d = dict((literal_name(k), v) for (k, v) in choplist(2, objs) if v is not None)
self.push((pos, d)) self.push((pos, d))
except PSTypeError: except PSTypeError as e:
if settings.STRICT: if settings.STRICT:
raise raise e
elif token == KEYWORD_PROC_BEGIN: elif token == KEYWORD_PROC_BEGIN:
# begin proc # begin proc
self.start_type(pos, 'p') self.start_type(pos, 'p')
@@ -146,16 +147,16 @@ class PSStackParser(PSBaseParser):
# end proc # end proc
try: try:
self.push(self.end_type('p')) self.push(self.end_type('p'))
except PSTypeError: except PSTypeError as e:
if settings.STRICT: if settings.STRICT:
raise raise e
elif isinstance(token,PSKeyword): elif isinstance(token,PSKeyword):
log.debug('do_keyword: pos=%r, token=%r, stack=%r', pos, token, self.curstack) log.debug('do_keyword: pos=%r, token=%r, stack=%r', pos, token, self.curstack)
self.do_keyword(pos, token) self.do_keyword(pos, token)
else: else:
log.error('unknown token: pos=%r, token=%r, stack=%r', pos, token, self.curstack) log.error('unknown token: pos=%r, token=%r, stack=%r', pos, token, self.curstack)
self.do_keyword(pos, token) self.do_keyword(pos, token)
raise Exception("unknown tokens") raise PSSyntaxError("unknown tokens")
if self.context: if self.context:
continue continue
else: else:
@@ -1,20 +0,0 @@
## PS Exceptions
##
class PSException(Exception):
pass
class PSEOF(PSException):
pass
class PSSyntaxError(PSException):
pass
class PSTypeError(PSException):
pass
class PSValueError(PSException):
pass
+7 -10
View File
@@ -19,10 +19,13 @@ import pickle as pickle
import struct import struct
import logging import logging
from pdfmajor.execptions import (
CMapNotFound,
PSEOF,
PSSyntaxError,
)
from ...utils import choplist, nunpack, name2unicode from ...utils import choplist, nunpack, name2unicode
from ..PSStackParser import PSStackParser from ..PSStackParser import PSStackParser
from ..PSStackParser import PSSyntaxError
from ..PSStackParser import PSEOF
from ..PSStackParser import PSLiteral from ..PSStackParser import PSLiteral
from ..PSStackParser import KWD from ..PSStackParser import KWD
from ..PSStackParser import literal_name from ..PSStackParser import literal_name
@@ -30,9 +33,6 @@ from ..PSStackParser import literal_name
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
class CMapError(Exception):
pass
## CMapBase ## CMapBase
## ##
@@ -219,9 +219,6 @@ class CMapDB(object):
_cmap_cache = {} _cmap_cache = {}
_umap_cache = {} _umap_cache = {}
class CMapNotFound(CMapError):
pass
@classmethod @classmethod
def _load_data(klass, name): def _load_data(klass, name):
name = name.replace("\0", "") name = name.replace("\0", "")
@@ -238,7 +235,7 @@ class CMapDB(object):
finally: finally:
gzfile.close() gzfile.close()
else: else:
raise CMapDB.CMapNotFound(name) raise CMapNotFound(name)
@classmethod @classmethod
def get_cmap(klass, name): def get_cmap(klass, name):
@@ -325,7 +322,7 @@ class CMapParser(PSStackParser):
self.cmap.use_cmap(CMapDB.get_cmap(literal_name(cmapname))) self.cmap.use_cmap(CMapDB.get_cmap(literal_name(cmapname)))
except PSSyntaxError: except PSSyntaxError:
pass pass
except CMapDB.CMapNotFound: except CMapNotFound:
pass pass
return return
+3 -3
View File
@@ -10,17 +10,17 @@ import sys
with open('README.md') as readme_file: with open('README.md') as readme_file:
SETUP = { SETUP = {
"name": "pdfmajor", "name": "pdfmajor",
"version": "1.3.9", "version": "1.3.13",
"packages": find_packages(include=['pdfmajor*']), "packages": find_packages(include=['pdfmajor*']),
"install_requires": [ "install_requires": [
'pycryptodome==3.8.2', 'pycryptodome==3.8.2',
'chardet==3.0.4', 'chardet==3.0.4',
'Pillow==6.0.0' 'Pillow>=6.2.0'
], ],
'extras_require': { 'extras_require': {
'dev': [ 'dev': [
'tqdm==4.32.1', 'tqdm==4.32.1',
'nose==1.3.7' 'mkdocs==1.0.4'
] ]
}, },
"description": 'PDF parser', "description": 'PDF parser',
-1
View File
@@ -1 +0,0 @@
../pdfmajor
Binary file not shown.
+24
View File
@@ -0,0 +1,24 @@
from pathlib import Path
from unittest import TestCase, main
from pdfmajor.interpreter import PDFInterpreter
from pdfmajor.execptions import UnicodeNotDefined
CURRENT_FOLDER = Path("./")
class BadUnicode(TestCase):
def test_ensure_failure(self):
pdf = PDFInterpreter(CURRENT_FOLDER / "tests" / "samples" / "pdf" / "bad-unicode.pdf", ignore_bad_chars=False)
pages = list(pdf)
with self.assertRaises(UnicodeNotDefined):
list(pages[2])
def test_ensure_no_failure(self):
pdf = PDFInterpreter(CURRENT_FOLDER / "tests" / "samples" / "pdf" / "bad-unicode.pdf", ignore_bad_chars=True)
pages = list(pdf)
list(pages[2])
if __name__ == '__main__':
# Run Tests
main()
+1 -1
View File
@@ -14,7 +14,7 @@ NUM_LOOPS = 10
def count_elms(file_path: str): def count_elms(file_path: str):
count = 0 count = 0
for page in PDFInterpreter(file_path): for page in PDFInterpreter(file_path, ignore_bad_chars=True):
for item in page: for item in page:
if isinstance(item, LTContainer): if isinstance(item, LTContainer):
count += len([*item]) count += len([*item])
+1
View File
@@ -42,6 +42,7 @@ class ConversionTest(TestCase):
input_file=input_file_path, input_file=input_file_path,
output_file=output_file_path, output_file=output_file_path,
out_type=ext, out_type=ext,
ignore_bad_chars=True,
debug_level=logging.WARN, debug_level=logging.WARN,
) )
if ext == 'xml': if ext == 'xml':
+1 -1
View File
@@ -16,7 +16,7 @@ class TestInterpreter(TestCase):
for file_name in FILES: for file_name in FILES:
self.subTest(file_name=file_name) self.subTest(file_name=file_name)
file_path = os.path.join(INPUT_FOLDER, file_name) file_path = os.path.join(INPUT_FOLDER, file_name)
for page in PDFInterpreter(file_path, debug_level=logging.ERROR): for page in PDFInterpreter(file_path, debug_level=logging.ERROR, ignore_bad_chars=True):
self.assertTrue( isinstance(page, PageInterpreter) ) self.assertTrue( isinstance(page, PageInterpreter) )
for item in page: for item in page:
self.assertTrue( isinstance(item, LTItem) ) self.assertTrue( isinstance(item, LTItem) )
+2 -1
View File
@@ -5,7 +5,8 @@
import logging import logging
from unittest import TestCase, main from unittest import TestCase, main
from io import BytesIO from io import BytesIO
from pdfmajor.parser.PSStackParser import KWD, LIT, PSBaseParser, PSEOF, PSStackParser from pdfmajor.parser.PSStackParser import KWD, LIT, PSBaseParser, PSStackParser
from pdfmajor.execptions import PSEOF
## Simplistic Test cases ## Simplistic Test cases
## ##