11 Commits
Author SHA1 Message Date
ari 7319b7f584 xml chars output bbx instead of width/height 2018-10-31 14:45:02 -04:00
ari 5b641d6243 comment out leading for now 2018-10-29 16:35:32 -04:00
ari 10d679d379 clean up 2018-10-29 16:26:57 -04:00
ari 5378a4f34c - better color grabbing for scn
- show custom type in converters
2018-10-29 16:25:50 -04:00
ari 42cbc8e39e - using snake case
- not outputting is_bold
2018-10-29 13:18:12 -04:00
ari 66004988f4 actually cleaning it all prior to deployment 2018-10-29 13:04:32 -04:00
ari 7722b99f70 Deployong new version - v1.1.4 2018-10-29 13:03:28 -04:00
ari 66b5fea65c - compatible xml-converter with char-blocks
- consitent attr naming convention for fonts
2018-10-29 13:03:11 -04:00
ari 659b19dae9 Deployong new version - v1.1.3 2018-10-29 12:51:48 -04:00
ari 337c1b4a76 - pulling out boldness from font
- better html converter
2018-10-29 12:51:34 -04:00
ari 76deefb9dc Deployong new version - v1.1.2 2018-10-28 23:52:59 -04:00
13 changed files with 155 additions and 80 deletions

No files matched your search

+1 -1
View File
@@ -1 +1 @@
v1.1.1 v1.1.5
+5 -1
View File
@@ -4,10 +4,14 @@ WK_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cd $WK_DIR cd $WK_DIR
cd .. cd ..
sh bin/clean.sh ./bin/clean.sh
python setup.py sdist bdist_wheel python setup.py sdist bdist_wheel
git add .
git commit
git push
git tag $(cat .version) git tag $(cat .version)
git push -u origin $(cat .version) git push -u origin $(cat .version)
+1 -1
View File
@@ -75,7 +75,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, 1, 1) __version__ = (1, 1, 5)
if __name__ == '__main__': if __name__ == '__main__':
print( print(
+43 -23
View File
@@ -26,6 +26,9 @@ def get_color(col: PDFGraphicStateColor):
return f"cmyk({','.join(map(str, color_val))})" return f"cmyk({','.join(map(str, color_val))})"
elif color_type == 'gray': elif color_type == 'gray':
return f'rgba(0,0,0,{1-color_val})' return f'rgba(0,0,0,{1-color_val})'
elif color_type == "custom":
# Todo: convert these to more friendly color types
return f"custom({col.custom['type']})"
return "none" return "none"
class HTMLConverter(PDFConverter): class HTMLConverter(PDFConverter):
@@ -42,8 +45,8 @@ class HTMLConverter(PDFConverter):
self.__current_width = None self.__current_width = None
self.write_header() self.write_header()
def write(self, text: str): def write(self, text: str, lineend: str = '\n', deep_space: str = ' '):
text = " "*self.__levels_deep + text + '\n' text = deep_space*self.__levels_deep + text + lineend
self.write_raw(text) self.write_raw(text)
def write_raw(self, text): def write_raw(self, text):
@@ -78,7 +81,13 @@ class HTMLConverter(PDFConverter):
self.__levels_deep -= 1 self.__levels_deep -= 1
@contextmanager @contextmanager
def place_elm_with_child(self, tag_name: str, attr_dict: Dict[str, str] = {}, css: Dict[str, str] = None): def place_elm_with_child(self, tag_name: str, attr_dict: Dict[str, str] = {}, css: Dict[str, str] = None, no_additional_char = False):
lineend = '\n'
deep_space = ' '
if no_additional_char:
lineend = ''
deep_space = ''
if css is not None: if css is not None:
attr_dict['style'] = ";".join([ attr_dict['style'] = ";".join([
f'{name}: {value}' f'{name}: {value}'
@@ -88,11 +97,11 @@ class HTMLConverter(PDFConverter):
f'{name}="{value}"' f'{name}="{value}"'
for name, value in attr_dict.items() for name, value in attr_dict.items()
]) ])
self.write(f"<{tag_name} {attrs}>") self.write(f"<{tag_name} {attrs}>", lineend=lineend)
self.__levels_deep += 1 self.__levels_deep += 1
yield yield
self.__levels_deep -= 1 self.__levels_deep -= 1
self.write(f"</{tag_name}>") self.write(f"</{tag_name}>", deep_space=deep_space)
def place_elm_close(self, tag_name: str, attr_dict: Dict[str, str] = {}, css: Dict[str, str] = None, as_childless = True): def place_elm_close(self, tag_name: str, attr_dict: Dict[str, str] = {}, css: Dict[str, str] = None, as_childless = True):
if css is not None: if css is not None:
@@ -201,28 +210,39 @@ class HTMLConverter(PDFConverter):
self.place_image(item) self.place_image(item)
elif isinstance(item, LTChar): elif isinstance(item, LTChar):
with self.place_elm_with_child('span', { 'class': 'char' }, { with self.place_elm_with_child('span', { 'class': 'char' }, {
'font-size': f'{item.size}px', 'width': item.width,
'font-family': item.fontname, 'min-width': item.width,
'font-weight': 'bold' if 'bold' in item.fontname.lower() else '', 'max-width': item.width,
'color': get_color(item.graphicstate.ncolor), 'height': item.height,
'position': 'absolute', 'min-height': item.height,
'left': f'{item.x0}', 'max-height': item.height,
'bottom': f'{item.y0}',
'transform': f'skew({item.font.italic_angle}deg, 0deg)'
}): }):
self.write(item.get_text()) self.write(item.get_text())
elif isinstance(item, LTCharBlock): elif isinstance(item, LTCharBlock):
with self.place_elm_with_child('span', { 'class': 'char-block' }, { with self.place_elm_with_child('span', { 'class': 'char-block' }, {
'font-size': f'{item.size}px', 'position': 'absolute',
'font-family': item.fontname, 'left': f'{item.x0}',
'font-weight': 'bold' if 'bold' in item.fontname.lower() else '', 'bottom': f'{item.y0}',
'color': get_color(item.graphicstate.ncolor), 'text-align': 'justify',
'position': 'absolute', 'font-size': f'{item.size}px',
'left': f'{item.x0}', 'font-family': item.fontname,
'bottom': f'{item.y0}', 'font-weight': item.font.font_weight,
'transform': f'skew({item.font.italic_angle}deg, 0deg)' 'color': get_color(item.graphicstate.ncolor),
}): 'width': item.width,
self.write(item.get_text()) 'min-width': item.width,
'max-width': item.width,
'height': item.height,
'min-height': item.height,
'max-height': item.height,
'transform': f'skew({item.font.italic_angle}deg, 0deg)',
'text-align': 'center',
'display': 'flex',
'flex-flow': 'row nowrap',
'justify-content': 'space-between',
# 'padding-left': item.font.leading
}):
for char in item:
render(char)
render(ltpage) render(ltpage)
def close(self): def close(self):
+35 -17
View File
@@ -1,4 +1,6 @@
import logging import logging
import re
import json
from typing import Dict from typing import Dict
@@ -28,8 +30,17 @@ def get_color(col: PDFGraphicStateColor):
return f"cmyk({','.join(map(str, color_val))})" return f"cmyk({','.join(map(str, color_val))})"
elif color_type == 'gray': elif color_type == 'gray':
return f'gray({color_val})' return f'gray({color_val})'
elif color_type == "custom":
# Todo: convert these to more friendly color types
return f"custom({col.custom['type']})"
return "" return ""
FIRST_CAP_RE = re.compile('(.)([A-Z][a-z]+)')
ALL_CAP_RE = re.compile('([a-z0-9])([A-Z])')
def convert_capcase_to_snakecase(text: str) -> str:
s1 = FIRST_CAP_RE.sub(r'\1-\2', text)
return ALL_CAP_RE.sub(r'\1-\2', s1).lower()
class XMLConverter(PDFConverter): class XMLConverter(PDFConverter):
def __init__(self, def __init__(self,
rsrcmgr, rsrcmgr,
@@ -113,23 +124,30 @@ class XMLConverter(PDFConverter):
}) })
def place_text(self, char: LTChar): def place_text(self, char: LTChar):
tag_name = 'char' with self.place_elm_with_child('char', {
if isinstance(char, LTCharBlock): 'x0': char.x0,
tag_name = 'char-block' 'x1': char.x1,
attr = { 'y0': char.y0,
'size': char.size, 'y1': char.y1,
"color": get_color(char.graphicstate.ncolor), }, no_additional_char=True):
"x0": char.x0,
"x1": char.x1,
"y0": char.y0,
"y1": char.y1,
}
for key, value in char.font.descriptor.items():
if key != "Type" and "FontFile" not in key:
attr["font-" + key] = value
with self.place_elm_with_child(tag_name, attr, no_additional_char=True):
self.write(char.get_text(), lineend='', deep_space="") self.write(char.get_text(), lineend='', deep_space="")
def render_char_block(self, char_block: LTCharBlock):
attr = {
'size': char_block.size,
"color": get_color(char_block.graphicstate.ncolor),
"x0": char_block.x0,
"x1": char_block.x1,
"y0": char_block.y0,
"y1": char_block.y1,
}
for key, value in char_block.font.descriptor.items():
if key != "Type" and "FontFile" not in key:
attr[convert_capcase_to_snakecase(key)] = value
with self.place_elm_with_child("char-block", attr):
for char in char_block:
self.place_text(char)
def render_curve(self, item: LTCurve): def render_curve(self, item: LTCurve):
attr = { attr = {
"x0": item.x0, "x0": item.x0,
@@ -164,8 +182,8 @@ class XMLConverter(PDFConverter):
render(child) render(child)
elif isinstance(item, LTImage): elif isinstance(item, LTImage):
self.place_image(item) self.place_image(item)
elif isinstance(item, LTChar) or isinstance(item, LTCharBlock): elif isinstance(item, LTCharBlock):
self.place_text(item) self.render_char_block(item)
render(ltpage) render(ltpage)
def close(self): def close(self):
+13
View File
@@ -531,6 +531,19 @@ class PDFFont(object):
@abstractmethod @abstractmethod
def to_unichr(self, cid): def to_unichr(self, cid):
raise NotImplementedError raise NotImplementedError
@property
def font_weight(self):
if 'FontWeight' in self.descriptor.keys():
return self.descriptor['FontWeight']
elif 'bold' in self.fontname.lower():
return 'bold'
else:
return None
@property
def is_bold(self):
return self.font_weight == 'bold' or self.font_weight > 549
# PDFSimpleFont # PDFSimpleFont
@@ -5,6 +5,7 @@ class PDFGraphicStateColor:
self.__rgb = None self.__rgb = None
self.__gray = None self.__gray = None
self.__cmyk = None self.__cmyk = None
self.__custom = None
def get_color(self): def get_color(self):
if self.__rgb is not None: if self.__rgb is not None:
@@ -13,6 +14,8 @@ class PDFGraphicStateColor:
return 'cmyk', self.cmyk return 'cmyk', self.cmyk
elif self.__gray is not None: elif self.__gray is not None:
return 'gray', self.gray return 'gray', self.gray
elif self.__custom is not None:
return 'custom', self.custom
else: return None, None else: return None, None
@property @property
@@ -34,6 +37,11 @@ class PDFGraphicStateColor:
if self.__gray is not None: return float(self.__gray) if self.__gray is not None: return float(self.__gray)
else: raise self.NotSet else: raise self.NotSet
@property
def custom(self):
if self.__custom is not None: return dict(self.__custom)
else: raise self.NotSet
@rgb.setter @rgb.setter
def rgb(self, rgb): def rgb(self, rgb):
self.__rgb = rgb self.__rgb = rgb
@@ -46,11 +54,16 @@ class PDFGraphicStateColor:
def gray(self, color): def gray(self, color):
self.__gray = color self.__gray = color
@custom.setter
def custom(self, color):
self.__custom = color
def copy(self): def copy(self):
cp = self.__class__() cp = self.__class__()
cp.gray = self.__gray cp.gray = self.__gray
cp.rgb = self.__rgb cp.rgb = self.__rgb
cp.cmyk = self.__cmyk cp.cmyk = self.__cmyk
cp.custom = self.__custom
return cp return cp
def __repr__(self): def __repr__(self):
@@ -58,4 +71,5 @@ class PDFGraphicStateColor:
if self.__gray is not None: rep += f' gray={self.__gray}' if self.__gray is not None: rep += f' gray={self.__gray}'
if self.__rgb is not None: rep += f' rgb=[{",".join(map(str, self.__rgb))}]' if self.__rgb is not None: rep += f' rgb=[{",".join(map(str, self.__rgb))}]'
if self.__cmyk is not None: rep += f' cmyk=[{",".join(map(str, self.__cmyk))}]' if self.__cmyk is not None: rep += f' cmyk=[{",".join(map(str, self.__cmyk))}]'
if self.__custom is not None: rep += f' custom={self.__custom}'
return rep + '/>' return rep + '/>'
+36 -32
View File
@@ -1,6 +1,7 @@
import logging import logging
import operator import operator
from collections import OrderedDict
from typing import List from typing import List
@@ -26,7 +27,7 @@ from .PDFGraphicState import PDFGraphicState
from .PDFTextState import PDFTextState from .PDFTextState import PDFTextState
from .PDFContentParser import PDFContentParser from .PDFContentParser import PDFContentParser
from ..utils import settings, mult_matrix, MATRIX_IDENTITY from ..utils import mult_matrix, MATRIX_IDENTITY
from .constants import LITERAL_FORM, LITERAL_IMAGE from .constants import LITERAL_FORM, LITERAL_IMAGE
from .types import CurvePoint, CurvePath from .types import CurvePoint, CurvePath
@@ -54,7 +55,7 @@ class PDFPageInterpreter(object):
self.resources = resources self.resources = resources
self.fontmap = {} self.fontmap = {}
self.xobjmap = {} self.xobjmap = {}
self.csmap = PREDEFINED_COLORSPACE.copy() self.csmap:OrderedDict = PREDEFINED_COLORSPACE.copy()
if not resources: if not resources:
return return
@@ -303,8 +304,7 @@ class PDFPageInterpreter(object):
try: try:
self.scs = self.csmap[literal_name(name)] self.scs = self.csmap[literal_name(name)]
except KeyError: except KeyError:
if settings.STRICT: raise PDFInterpreterError('Undefined ColorSpace: %r' % name)
raise PDFInterpreterError('Undefined ColorSpace: %r' % name)
return return
# setcolorspace-non-strokine # setcolorspace-non-strokine
@@ -312,8 +312,7 @@ class PDFPageInterpreter(object):
try: try:
self.ncs = self.csmap[literal_name(name)] self.ncs = self.csmap[literal_name(name)]
except KeyError: except KeyError:
if settings.STRICT: raise PDFInterpreterError('Undefined ColorSpace: %r' % name)
raise PDFInterpreterError('Undefined ColorSpace: %r' % name)
return return
# setgray-stroking # setgray-stroking
@@ -353,24 +352,33 @@ class PDFPageInterpreter(object):
return return
# setcolor # setcolor
def do_SCN(self): def __process_scn(self):
if self.scs:
n = self.scs.ncomponents
else:
if settings.STRICT:
raise PDFInterpreterError('No colorspace specified!')
n = 1
self.pop(n)
return
def do_scn(self):
if self.ncs: if self.ncs:
n = self.ncs.ncomponents n = self.ncs.ncomponents
else: else:
if settings.STRICT: raise PDFInterpreterError('No colorspace specified!')
raise PDFInterpreterError('No colorspace specified!') components = self.pop(n)
n = 1 obj_id = components[0]
self.pop(n) if self.ncs.name not in self.resources.keys():
raise PDFInterpreterError("Color space not in resources")
if obj_id.name not in self.resources[self.ncs.name].keys():
raise PDFInterpreterError("Invalid Color reference")
color_data = self.resources[self.ncs.name][obj_id.name].resolve()
if isinstance(color_data, PDFStream):
color_data = color_data.attrs
return {
"type": self.ncs.name,
"defintion": color_data
}
def do_SCN(self):
self.graphicstate.scolor.custom = self.__process_scn()
return
def do_scn(self):
self.graphicstate.ncolor.custom = self.__process_scn()
return return
def do_SC(self): def do_SC(self):
@@ -432,6 +440,8 @@ class PDFPageInterpreter(object):
def do_Tw(self, space): def do_Tw(self, space):
self.textstate.wordspace = space self.textstate.wordspace = space
return return
# def do_Th(self, )
# textscale # textscale
def do_Tz(self, scale): def do_Tz(self, scale):
@@ -448,9 +458,8 @@ class PDFPageInterpreter(object):
try: try:
self.textstate.font = self.fontmap[literal_name(fontid)] self.textstate.font = self.fontmap[literal_name(fontid)]
except KeyError: except KeyError:
if settings.STRICT: raise PDFInterpreterError('Undefined Font id: %r' % fontid)
raise PDFInterpreterError('Undefined Font id: %r' % fontid) # self.textstate.font = self.rsrcmgr.get_font(None, {})
self.textstate.font = self.rsrcmgr.get_font(None, {})
self.textstate.fontsize = fontsize self.textstate.fontsize = fontsize
return return
@@ -495,9 +504,7 @@ class PDFPageInterpreter(object):
# show-pos # show-pos
def do_TJ(self, seq: bytearray): def do_TJ(self, seq: bytearray):
if self.textstate.font is None: if self.textstate.font is None:
if settings.STRICT: raise PDFInterpreterError('No font specified!')
raise PDFInterpreterError('No font specified!')
return
self.device.render_string(self.textstate.copy(), seq, self.ncs, self.graphicstate.copy()) self.device.render_string(self.textstate.copy(), seq, self.ncs, self.graphicstate.copy())
return return
@@ -540,9 +547,7 @@ class PDFPageInterpreter(object):
try: try:
xobj = PDFStream.validated_stream(self.xobjmap[xobjid]) xobj = PDFStream.validated_stream(self.xobjmap[xobjid])
except KeyError: except KeyError:
if settings.STRICT: raise PDFInterpreterError('Undefined xobject id: %r' % xobjid)
raise PDFInterpreterError('Undefined xobject id: %r' % xobjid)
return
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:
@@ -618,8 +623,7 @@ class PDFPageInterpreter(object):
log.debug('exec: %s', name) log.debug('exec: %s', name)
func() func()
else: else:
if settings.STRICT: raise PDFInterpreterError('Unknown operator: %r' % name)
raise PDFInterpreterError('Unknown operator: %r' % name)
else: else:
self.push(obj) self.push(obj)
return return
+1 -1
View File
@@ -12,7 +12,7 @@ from .exceptions import * # pylint: disable=W0614
## ##
class PDFObjRef(PDFObject): class PDFObjRef(PDFObject):
def __init__(self, doc, objid, _): def __init__(self, doc, objid, a):
if objid == 0: if objid == 0:
if settings.STRICT: if settings.STRICT:
raise PDFValueError('PDF object id cannot be 0.') raise PDFValueError('PDF object id cannot be 0.')
@@ -100,7 +100,6 @@ class PSBaseParser(object):
linebuf += self.buf[self.charpos:] linebuf += self.buf[self.charpos:]
self.charpos = len(self.buf) self.charpos = len(self.buf)
log.debug('nextline: %r, %r', linepos, linebuf) log.debug('nextline: %r, %r', linepos, linebuf)
return (linepos, linebuf) return (linepos, linebuf)
def revreadlines(self): def revreadlines(self):
+3
View File
@@ -60,6 +60,9 @@ class LTCharBlock(LTComponent):
def __repr__(self): def __repr__(self):
return f"""<{self.__class__.__name__} font="{self.fontname}" text="{self.get_text()}"/>""" return f"""<{self.__class__.__name__} font="{self.fontname}" text="{self.get_text()}"/>"""
def __iter__(self):
return iter(self.chars)
def get_text(self): def get_text(self):
return "".join([ c.get_text() for c in self.chars ]) return "".join([ c.get_text() for c in self.chars ])
+2 -2
View File
@@ -16,9 +16,9 @@ if not os.path.exists(OUTPUT_FOLDER):
# files = os.listdir(INPUT_FOLDER) # files = os.listdir(INPUT_FOLDER)
# files = ['fonts.pdf'] # files = ['fonts.pdf']
# files = ['bar-charts.pdf'] files = ['bar-charts.pdf']
# files = ['font-color.pdf'] # files = ['font-color.pdf']
files = ['tables.pdf'] # files = ['tables.pdf']
TOTAL = len(files) TOTAL = len(files)
for idx, file_name in enumerate(files): for idx, file_name in enumerate(files):
+1 -1
View File
@@ -10,7 +10,7 @@ import sys
with open('README.md') as readme_file: with open('README.md') as readme_file:
SETUP = { SETUP = {
"name": "pdfmajor", "name": "pdfmajor",
"version": "1.1.2", "version": "1.1.6",
"packages": find_packages(include=['pdfmajor*']), "packages": find_packages(include=['pdfmajor*']),
"install_requires": [ "install_requires": [
'pycryptodome', 'pycryptodome',