6 Commits
Author SHA1 Message Date
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
29 changed files with 108 additions and 50 deletions

No files matched your search

+2 -1
View File
@@ -13,4 +13,5 @@ tests/samples/output
./samples
**__pycache**\
.mypy_cache
.ipynb_checkpoints
.ipynb_checkpoints
venv
+1 -1
View File
@@ -1 +1 @@
v1.3.9
v1.3.10
+1
View File
@@ -37,6 +37,7 @@ This generator-function yields individual pages which contain their respected it
- `password`: [str](#) defaults to None
- `caching`: [bool](#) defaults to True
- `check_extractable`: [bool](#) defaults to True
- `ignore_bad_chars`: [bool](#) defaults to False
- `pagenos`: [List[int]](#) defaults to None
- `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>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>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>debug_level</code>: <a href="#https://docs.python.org/3/library/logging.html#levels">logging.levels</a> defaults logging.WARNING</li>
</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
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">
<url>
<loc>None</loc>
<lastmod>2019-01-11</lastmod>
<lastmod>2019-12-08</lastmod>
<changefreq>daily</changefreq>
</url>
<url>
<loc>None</loc>
<lastmod>2019-01-11</lastmod>
<lastmod>2019-12-08</lastmod>
<changefreq>daily</changefreq>
</url>
</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.
"""
__version__ = (1, 3, 9)
__version__ = (1, 3, 10)
if __name__ == '__main__':
print(
+2
View File
@@ -0,0 +1,2 @@
import pdfmajor.parser.exceptions as parser
import pdfmajor.interpreter.exceptions as interpreter
+4 -4
View File
@@ -5,9 +5,8 @@ from .commands import PDFStateStack
from .commands import process_command_stream, prep_state
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
if page.rotate == 90:
ctm = [0, -1, 1, 0, -y0, x1]
@@ -19,7 +18,7 @@ class PageInterpreter:
ctm = [1, 0, 0, 1, -x0, -y0]
# Locals
self.font_cache = font_cache
self.font_cache = font_cache if font_cache is not None else {}
self.page = page
self.page_num = page_num
self.height = y1-y0
@@ -30,7 +29,8 @@ class PageInterpreter:
PDFStateStack(),
ctm=ctm,
resources=page.resources,
font_cache=self.font_cache
font_cache=self.font_cache,
ignore_bad_chars=ignore_bad_chars
)
+3 -1
View File
@@ -23,6 +23,7 @@ class PDFInterpreter:
password: str = None,
pagenos: List[int] = None,
preload: bool = False,
ignore_bad_chars: bool = False,
debug_level: int = logging.WARNING,
):
self.input_file_path = input_file_path
@@ -31,6 +32,7 @@ class PDFInterpreter:
self.check_extractable = check_extractable
self.password = password
self.pagenos = pagenos
self.ignore_bad_chars = ignore_bad_chars
self.debug_level = debug_level
self.__pages = []
if preload:
@@ -52,7 +54,7 @@ class PDFInterpreter:
check_extractable=self.check_extractable
)
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]
log.info(f"Done Reading {len(self.__pages)} pages.")
set_log_level(logging.WARNING)
+4 -3
View File
@@ -7,17 +7,18 @@ from pdfmajor.parser.constants import LITERAL_FORM, LITERAL_IMAGE
from .state import PDFStateStack, PDFColorSpace
from .state.Curves import CurveMethod, CurvePath, CurvePoint
class PDFCommandsInvalidOperation(Exception): pass
class PDFCommandsRepeatedCommand(Exception): pass
class PDFCommands:
commands = {}
class InvalidOperation(Exception): pass
class RepeatedCommand(Exception): pass
@classmethod
def __add(cls, cmd_name: str, cmd: callable):
if not (cmd_name in cls.commands.keys()):
cls.commands[cmd_name] = cmd
return cmd
raise cls.RepeatedCommand(cmd_name)
raise PDFCommandsRepeatedCommand(cmd_name)
@classmethod
def add(cls, *cmd_names: List[str]):
+2 -1
View File
@@ -71,9 +71,10 @@ def process_command_stream(streams: List[PDFStream], font_cache: dict = None, st
yield complete_item
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.resources = resources
state.text.ignore_bad_chars = ignore_bad_chars
# set some global states.
state.graphics.ncolspace = state.graphics.scolspace = None
+10 -10
View File
@@ -3,7 +3,7 @@ from pdfmajor.parser.PSStackParser import literal_name
from pdfmajor.parser.PDFStream import PDFStream, list_value, dict_value
from pdfmajor.parser.constants import LITERAL_FORM, LITERAL_IMAGE
from .PDFCommands import PDFCommands
from .PDFCommands import PDFCommands, PDFCommandsInvalidOperation, PDFCommandsRepeatedCommand
from .state import PDFStateStack, PDFGraphicState, LTTextBlock
from .state.Curves import CurveMethod, CurvePath, CurvePoint
# from .state.PDFItem import PDFImage, PDFShape, PDFText, PDFXObject
@@ -253,7 +253,7 @@ def set_stroke_colorspace(stack: PDFStateStack, name: str) -> PDFStateStack:
stack.graphics.scolspace = stack.colorspace_map[literal_name(name)]
return stack
except KeyError:
raise PDFCommands.InvalidOperation('Undefined ColorSpace: %r' % name)
raise PDFCommandsInvalidOperation('Undefined ColorSpace: %r' % name)
# setcolorspace-non-strokine
@PDFCommands.add('cs')
@@ -262,7 +262,7 @@ def set_nonstroke_colorspace(stack: PDFStateStack, name: str) -> PDFStateStack:
stack.graphics.ncolspace = stack.colorspace_map[literal_name(name)]
return stack
except KeyError:
raise PDFCommands.InvalidOperation('Undefined ColorSpace: %r' % name)
raise PDFCommandsInvalidOperation('Undefined ColorSpace: %r' % name)
# setgray-stroking
@PDFCommands.add('G')
@@ -326,7 +326,7 @@ def set_nonstroke_cmyk(stack: PDFStateStack, c, m, y, k) -> PDFStateStack:
@PDFCommands.add('SCN','SC')
def set_stroke_colorspace_custom(stack: PDFStateStack) -> PDFStateStack:
if stack.graphics.scolspace is None:
raise PDFCommands.InvalidOperation('No colorspace specified!')
raise PDFCommandsInvalidOperation('No colorspace specified!')
components = stack.pop(stack.graphics.scolspace.ncomponents)
stack.graphics.set_stroke_color(
stack.graphics.scolspace,
@@ -337,7 +337,7 @@ def set_stroke_colorspace_custom(stack: PDFStateStack) -> PDFStateStack:
@PDFCommands.add('scn','sc')
def set_nonstroke_colorspace_custom(stack: PDFStateStack) -> PDFStateStack:
if stack.graphics.ncolspace is None:
raise PDFCommands.InvalidOperation('No colorspace specified!')
raise PDFCommandsInvalidOperation('No colorspace specified!')
components = stack.pop(stack.graphics.ncolspace.ncomponents)
stack.graphics.set_nostroke_color(
stack.graphics.ncolspace,
@@ -353,7 +353,7 @@ def begin_text(stack: PDFStateStack) -> PDFStateStack:
if stack.current_textblock is None:
stack.current_textblock = LTTextBlock()
else:
raise PDFCommands.InvalidOperation("Last TextBlock did not clear")
raise PDFCommandsInvalidOperation("Last TextBlock did not clear")
return stack
@PDFCommands.add('ET')
@@ -394,7 +394,7 @@ def set_font(stack: PDFStateStack, fontid, fontsize) -> PDFStateStack:
stack.text.font = stack.fontmap[literal_name(fontid)]
return stack
except KeyError:
raise PDFCommands.InvalidOperation('Undefined Font id: %r' % fontid)
raise PDFCommandsInvalidOperation('Undefined Font id: %r' % fontid)
# setrendering
@PDFCommands.add('Tr')
@@ -444,9 +444,9 @@ def nextline(stack: PDFStateStack) -> PDFStateStack:
@PDFCommands.add('TJ')
def add_char_blocks(stack: PDFStateStack, seq: bytearray) -> PDFStateStack:
if stack.text.font is None:
raise PDFCommands.InvalidOperation("No Font Specified")
raise PDFCommandsInvalidOperation("No Font Specified")
if stack.current_textblock is None:
raise PDFCommands.InvalidOperation("No TextBlock initilized")
raise PDFCommandsInvalidOperation("No TextBlock initilized")
stack.current_textblock.add_char_block(
seq,
stack.t_matrix,
@@ -487,7 +487,7 @@ def insert_xobject(stack: PDFStateStack, xobjid) -> PDFStateStack:
try:
xobj = PDFStream.validated_stream(stack.xobjmap[xobjid])
except KeyError:
raise PDFCommands.InvalidOperation('Undefined xobject id: %r' % xobjid)
raise PDFCommandsInvalidOperation('Undefined xobject id: %r' % xobjid)
# log.info('Processing xobj: %r', xobj)
subtype = xobj.get('Subtype')
if subtype is LITERAL_FORM and 'BBox' in xobj:
@@ -2,7 +2,6 @@ from .PDFColor import PDFColor, PDFColorSpace
from .PDFColorSpace import PREDEFINED_COLORSPACE
class PDFGraphicState(object):
class InvalidOperation(Exception): pass
def __init__(self):
self.linewidth = 0
@@ -58,7 +58,7 @@ class PDFText(PDFItem):
if needcharspace:
textstate.linematrix[idx] += textstate.charspace
text = textstate.font.to_unichr(cid)
text = textstate.to_unichr(cid)
assert isinstance(text, str), str(type(text))
matrix = translate_matrix(textstate.matrix, textstate.linematrix)
@@ -42,7 +42,7 @@ class TextDecoder:
if needcharspace:
textstate.linematrix[idx] += textstate.charspace
text = textstate.font.to_unichr(cid)
text = textstate.to_unichr(cid)
assert isinstance(text, str), str(type(text))
matrix = translate_matrix(textstate.matrix, textstate.linematrix)
@@ -10,10 +10,10 @@ from pdfmajor.parser.PDFStream import resolve1
from pdfmajor.parser.PDFStream import int_value
from pdfmajor.parser.PDFStream import num_value
from pdfmajor.parser.PDFStream import list_value
from pdfmajor.parser.PDFStream import PDFException
from pdfmajor.parser.PDFStream import PDFStream
from pdfmajor.parser.cmapdb import FileUnicodeMap
from pdfmajor.parser.cmapdb import CMapParser
from .execptions import PDFFontError, PDFUnicodeNotDefined
## Literals
##
@@ -22,8 +22,6 @@ LITERAL_TYPE1C = LIT('Type1C')
class PDFFont(object):
class PDFFontError(PDFException): pass
class PDFUnicodeNotDefined(PDFFontError): pass
def __init__(self, descriptor, widths, default_width=None):
self.descriptor = descriptor
@@ -77,7 +75,7 @@ class PDFFont(object):
except KeyError:
try:
return self.widths[self.to_unichr(cid)] * self.hscale
except (KeyError, self.PDFUnicodeNotDefined):
except (KeyError, PDFUnicodeNotDefined):
return self.default_width * self.hscale
def char_disp(self, cid):
@@ -137,4 +135,4 @@ class PDFSimpleFont(PDFFont):
try:
return self.cid2unicode[cid]
except KeyError:
raise self.PDFUnicodeNotDefined(None, cid)
raise PDFUnicodeNotDefined(None, cid)
@@ -4,6 +4,7 @@ from pdfmajor.parser.PDFStream import list_value
from pdfmajor.parser.PDFStream import dict_value
from pdfmajor.parser.PDFStream import resolve1
from pdfmajor.parser.constants import LITERAL_FONT
from .execptions import PDFFontError
from .fonts import PDFType1Font, PDFTrueTypeFont, PDFType3Font, PDFCIDFont, PDFFont
@@ -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)
if settings.STRICT:
if spec['Type'] is not LITERAL_FONT:
raise PDFFont.PDFFontError('Type is not /Font')
raise PDFFontError('Type is not /Font')
# Create a Font object.
if 'Subtype' in spec:
subtype = literal_name(spec['Subtype'])
else:
if settings.STRICT:
raise PDFFont.PDFFontError('Font Subtype is not specified.')
raise PDFFontError('Font Subtype is not specified.')
subtype = 'Type1'
if subtype in ('Type1', 'MMType1'):
# Type1 Font
@@ -47,7 +48,7 @@ def get_font(objid: int, spec: dict, cached_fonts: dict = {}):
font = get_font(None, subspec, cached_fonts)
else:
if settings.STRICT:
raise PDFFont.PDFFontError('Invalid Font spec: %r' % spec)
raise PDFFontError('Invalid Font spec: %r' % spec)
font = PDFType1Font(spec) # this is so wrong!
if objid:
cached_fonts[objid] = font
@@ -0,0 +1,4 @@
from pdfmajor.parser.PDFStream import PDFException
class PDFFontError(PDFException): pass
class PDFUnicodeNotDefined(PDFFontError): pass
@@ -15,6 +15,7 @@ from .PDFFont import PDFFont, PDFSimpleFont
from .util import FontMetricsDB, get_widths, get_widths2
from .Type1FontHeaderParser import Type1FontHeaderParser
from .TrueTypeFont import TrueTypeFont
from .execptions import PDFFontError, PDFUnicodeNotDefined
# PDFType1Font
class PDFType1Font(PDFSimpleFont):
@@ -24,7 +25,7 @@ class PDFType1Font(PDFSimpleFont):
self.basefont = literal_name(spec['BaseFont'])
except KeyError:
if settings.STRICT:
raise self.PDFFontError('BaseFont is missing')
raise PDFFontError('BaseFont is missing')
self.basefont = 'unknown'
try:
(descriptor, widths) = FontMetricsDB.get_metrics(self.basefont)
@@ -86,7 +87,7 @@ class PDFCIDFont(PDFFont):
self.basefont = literal_name(spec['BaseFont'])
except KeyError:
if strict:
raise self.PDFFontError('BaseFont is missing')
raise PDFFontError('BaseFont is missing')
self.basefont = 'unknown'
self.cidsysteminfo = dict_value(spec.get('CIDSystemInfo', {}))
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'])
except KeyError:
if strict:
raise self.PDFFontError('Encoding is unspecified')
raise PDFFontError('Encoding is unspecified')
name = 'unknown'
try:
self.cmap = CMapDB.get_cmap(name)
except CMapDB.CMapNotFound as e:
if strict:
raise self.PDFFontError(e)
raise PDFFontError(e)
self.cmap = CMap()
try:
descriptor = dict_value(spec['FontDescriptor'])
except KeyError:
if strict:
raise self.PDFFontError('FontDescriptor is missing')
raise PDFFontError('FontDescriptor is missing')
descriptor = {}
ttf = None
if 'FontFile2' in descriptor:
@@ -171,4 +172,4 @@ class PDFCIDFont(PDFFont):
raise KeyError(cid)
return self.unicode_map.get_unichr(cid)
except KeyError:
raise self.PDFUnicodeNotDefined(self.cidcoding, cid)
raise PDFUnicodeNotDefined(self.cidcoding, cid)
@@ -1,5 +1,6 @@
from .PDFFont import PDFFont, get_font
from pdfmajor.utils import MATRIX_IDENTITY
from .PDFFont import PDFFont, get_font
from .PDFFont.execptions import PDFUnicodeNotDefined, PDFFontError
## PDFTextState
##
@@ -16,14 +17,27 @@ class PDFTextState(object):
self.rise: int = 0
self.matrix = MATRIX_IDENTITY
self.linematrix = [0, 0]
self.ignore_bad_chars = False
def __repr__(self):
return ('<PDFTextState: font=%r, fontsize=%r, charspace=%r, wordspace=%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.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 PDFFontError("Missing font")
try:
return self.font.to_unichr(cid)
except PDFUnicodeNotDefined as e:
if self.ignore_bad_chars:
return ""
else:
raise e
def copy(self):
obj = self.__class__()
@@ -37,6 +51,7 @@ class PDFTextState(object):
obj.rise = self.rise
obj.matrix = self.matrix
obj.linematrix = self.linematrix
obj.ignore_bad_chars = self.ignore_bad_chars
return obj
def reset_matrices(self):
@@ -33,7 +33,7 @@ def __render_string_along(idx: int, seq: bytearray,
if needcharspace:
textstate.linematrix[idx] += textstate.charspace
text = textstate.font.to_unichr(cid)
text = textstate.to_unichr(cid)
assert isinstance(text, str), str(type(text))
matrix = translate_matrix(textstate.matrix, textstate.linematrix)
+3
View File
@@ -0,0 +1,3 @@
from .commands import CommandProcessorError
from .commands.PDFCommands import PDFCommandsRepeatedCommand, PDFCommandsInvalidOperation
from .commands.state.PDFTextState.PDFFont.execptions import *
+5
View File
@@ -0,0 +1,5 @@
from .cmapdb import CMapError
from .PDFStream.exceptions import *
from .PDFDocument.exceptions import *
from .PSStackParser.exceptions import *
from .PDFParser import PDFSyntaxError
+3 -3
View File
@@ -10,17 +10,17 @@ import sys
with open('README.md') as readme_file:
SETUP = {
"name": "pdfmajor",
"version": "1.3.9",
"version": "1.3.10",
"packages": find_packages(include=['pdfmajor*']),
"install_requires": [
'pycryptodome==3.8.2',
'chardet==3.0.4',
'Pillow==6.0.0'
'Pillow>=6.2.0'
],
'extras_require': {
'dev': [
'tqdm==4.32.1',
'nose==1.3.7'
'mkdocs==1.0.4'
]
},
"description": 'PDF parser',
Binary file not shown.
+23
View File
@@ -0,0 +1,23 @@
from pathlib import Path
from unittest import TestCase, main
from pdfmajor.interpreter import PDFInterpreter, exceptions
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(exceptions.PDFUnicodeNotDefined):
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()