7 Commits
Author SHA1 Message Date
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
11 changed files with 85 additions and 39 deletions

No files matched your search

+1 -1
View File
@@ -1 +1 @@
v1.1.3
v1.1.5
+2 -2
View File
@@ -4,12 +4,12 @@ WK_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cd $WK_DIR
cd ..
sh bin/clean.sh
./bin/clean.sh
python setup.py sdist bdist_wheel
git add .
git commit -m "Deployong new version - $(cat .version)"
git commit
git push
git tag $(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.
"""
__version__ = (1, 1, 3)
__version__ = (1, 1, 5)
if __name__ == '__main__':
print(
+4 -1
View File
@@ -26,6 +26,9 @@ def get_color(col: PDFGraphicStateColor):
return f"cmyk({','.join(map(str, color_val))})"
elif color_type == 'gray':
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"
class HTMLConverter(PDFConverter):
@@ -236,7 +239,7 @@ class HTMLConverter(PDFConverter):
'display': 'flex',
'flex-flow': 'row nowrap',
'justify-content': 'space-between',
'padding-left': item.font.leading
# 'padding-left': item.font.leading
}):
for char in item:
render(char)
+33 -17
View File
@@ -1,4 +1,6 @@
import logging
import re
import json
from typing import Dict
@@ -28,8 +30,17 @@ def get_color(col: PDFGraphicStateColor):
return f"cmyk({','.join(map(str, color_val))})"
elif color_type == 'gray':
return f'gray({color_val})'
elif color_type == "custom":
# Todo: convert these to more friendly color types
return f"custom({col.custom['type']})"
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):
def __init__(self,
rsrcmgr,
@@ -113,23 +124,28 @@ class XMLConverter(PDFConverter):
})
def place_text(self, char: LTChar):
tag_name = 'char'
if isinstance(char, LTCharBlock):
tag_name = 'char-block'
attr = {
'size': char.size,
"color": get_color(char.graphicstate.ncolor),
"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):
with self.place_elm_with_child('char', {
'width': char.width,
'height': char.height,
}, no_additional_char=True):
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):
attr = {
"x0": item.x0,
@@ -164,8 +180,8 @@ class XMLConverter(PDFConverter):
render(child)
elif isinstance(item, LTImage):
self.place_image(item)
elif isinstance(item, LTChar) or isinstance(item, LTCharBlock):
self.place_text(item)
elif isinstance(item, LTCharBlock):
self.render_char_block(item)
render(ltpage)
def close(self):
@@ -5,6 +5,7 @@ class PDFGraphicStateColor:
self.__rgb = None
self.__gray = None
self.__cmyk = None
self.__custom = None
def get_color(self):
if self.__rgb is not None:
@@ -13,6 +14,8 @@ class PDFGraphicStateColor:
return 'cmyk', self.cmyk
elif self.__gray is not None:
return 'gray', self.gray
elif self.__custom is not None:
return 'custom', self.custom
else: return None, None
@property
@@ -34,6 +37,11 @@ class PDFGraphicStateColor:
if self.__gray is not None: return float(self.__gray)
else: raise self.NotSet
@property
def custom(self):
if self.__custom is not None: return dict(self.__custom)
else: raise self.NotSet
@rgb.setter
def rgb(self, rgb):
self.__rgb = rgb
@@ -46,11 +54,16 @@ class PDFGraphicStateColor:
def gray(self, color):
self.__gray = color
@custom.setter
def custom(self, color):
self.__custom = color
def copy(self):
cp = self.__class__()
cp.gray = self.__gray
cp.rgb = self.__rgb
cp.cmyk = self.__cmyk
cp.custom = self.__custom
return cp
def __repr__(self):
@@ -58,4 +71,5 @@ class PDFGraphicStateColor:
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.__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 + '/>'
+25 -11
View File
@@ -1,6 +1,7 @@
import logging
import operator
from collections import OrderedDict
from typing import List
@@ -54,7 +55,7 @@ class PDFPageInterpreter(object):
self.resources = resources
self.fontmap = {}
self.xobjmap = {}
self.csmap = PREDEFINED_COLORSPACE.copy()
self.csmap:OrderedDict = PREDEFINED_COLORSPACE.copy()
if not resources:
return
@@ -351,20 +352,33 @@ class PDFPageInterpreter(object):
return
# setcolor
def do_SCN(self):
if self.scs:
n = self.scs.ncomponents
else:
raise PDFInterpreterError('No colorspace specified!')
self.pop(n)
return
def do_scn(self):
def __process_scn(self):
if self.ncs:
n = self.ncs.ncomponents
else:
raise PDFInterpreterError('No colorspace specified!')
self.pop(n)
components = self.pop(n)
obj_id = components[0]
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
def do_SC(self):
+1 -1
View File
@@ -12,7 +12,7 @@ from .exceptions import * # pylint: disable=W0614
##
class PDFObjRef(PDFObject):
def __init__(self, doc, objid, _):
def __init__(self, doc, objid, a):
if objid == 0:
if settings.STRICT:
raise PDFValueError('PDF object id cannot be 0.')
@@ -100,7 +100,6 @@ class PSBaseParser(object):
linebuf += self.buf[self.charpos:]
self.charpos = len(self.buf)
log.debug('nextline: %r, %r', linepos, linebuf)
return (linepos, linebuf)
def revreadlines(self):
+3 -3
View File
@@ -7,7 +7,7 @@ from pdfmajor.converters import convert_file
def rel_path(*arg: List[str]) -> str:
return os.path.join(os.path.dirname(__file__), *arg)
EXT = 'html'
EXT = 'xml'
INPUT_FOLDER = rel_path("./pdf")
OUTPUT_FOLDER = rel_path(f"./output/{EXT}")
@@ -16,9 +16,9 @@ if not os.path.exists(OUTPUT_FOLDER):
# files = os.listdir(INPUT_FOLDER)
# files = ['fonts.pdf']
# files = ['bar-charts.pdf']
files = ['bar-charts.pdf']
# files = ['font-color.pdf']
files = ['tables.pdf']
# files = ['tables.pdf']
TOTAL = len(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:
SETUP = {
"name": "pdfmajor",
"version": "1.1.3",
"version": "1.1.5",
"packages": find_packages(include=['pdfmajor*']),
"install_requires": [
'pycryptodome',