4 Commits
Author SHA1 Message Date
ari bab104885f - fixed caching error when running the interpreter multiple times on items with xobjects 2019-01-23 15:56:46 -05:00
ari 98f18c3e32 - corrected caching error
- added lorem ipsum pdf test docs
- introduced a new test for detecting if some unwanted caching is occuring
- muting "Unsupported" warning
2019-01-23 15:56:25 -05:00
ari ac2c462561 - improved html converter 2019-01-22 13:16:46 -05:00
ari 19712f1f0f cleaner html converter 2019-01-22 13:16:17 -05:00
13 changed files with 90 additions and 119 deletions

No files matched your search

+1 -1
View File
@@ -1 +1 @@
v1.3.4
v1.3.6
+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, 4)
__version__ = (1, 3, 6)
if __name__ == '__main__':
print(
+2 -2
View File
@@ -27,9 +27,9 @@ def convert_file(
):
if output_file is None:
output_file = os.path.join(
os.path.dirname(input_file.name),
os.path.dirname(input_file),
os.path.basename(
re.sub(r"\.\w+$", "."+out_type, input_file.name)
re.sub(r"\.\w+$", "."+out_type, input_file)
)
)
if re.search(re.escape(out_type) + r'$', output_file ) is None:
+38 -101
View File
@@ -5,6 +5,8 @@ from ..interpreter import LTImage, LTTextBlock, LTCharBlock, LTChar, LTCurve, LT
from ..interpreter.commands import LTItem
from ..interpreter.commands.state import CurvePath, PDFColor
INF = (1<<31) - 1
def convert_to_html(
input_file_path: str,
output_file_path: str,
@@ -79,33 +81,33 @@ def render_page(html: HTMLMaker, ltpage: PageInterpreter):
}):
html.write(item.get_text())
elif isinstance(item, LTTextBlock):
for char in item:
render(char)
with html.elm('span', {'class': 'text-block'}):
for char in item:
render(char)
elif isinstance(item, LTCharBlock):
with html.elm('span', {'class': 'char-block'}):
with html.elm('p', { 'class': 'text-block' }, {
'position': 'absolute',
'left': f'{item.x0}',
'bottom': f'{item.y0}',
'font-size': f'{item.size}px',
'font-family': item.fontname,
'font-weight': item.font.font_weight,
'color': get_color(item.color, default="black"),
'width': str(item.width) + "px",
'min-width': str(item.width) + "px",
'max-width': str(item.width) + "px",
'height': str(item.height) + "px",
'min-height': str(item.height) + "px",
'max-height': str(item.height) + "px",
'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)
with html.elm('span', { 'class': 'char-block' }, {
'position': 'absolute',
'left': f'{item.x0}',
'bottom': f'{item.y0}',
'font-size': f'{item.size}px',
'font-family': item.fontname,
'font-weight': item.font.font_weight,
'color': get_color(item.color, default="black"),
'width': str(item.width) + "px",
'min-width': str(item.width) + "px",
'max-width': str(item.width) + "px",
'height': str(item.height) + "px",
'min-height': str(item.height) + "px",
'max-height': str(item.height) + "px",
'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)
with html.elm('div', { "class": 'page', "id": f"page-{ltpage.page_num}" }, {
'width': f'{ltpage.width}px',
@@ -147,7 +149,7 @@ def render_curve(html: HTMLMaker, ltpage: PageInterpreter, item: LTCurve):
'right': f'{item.x1}px',
'width': f'{item.width}px',
'height': f'{item.height}px',
}) as svg:
}):
html.singleton('path', {
"d": path,
**path_css
@@ -156,11 +158,6 @@ def render_curve(html: HTMLMaker, ltpage: PageInterpreter, item: LTCurve):
})
def create_svg_path(item: LTCurve):
css = {
'position': 'absolute',
'left': f'{item.x0}px',
'bottom': f'{item.y0}px',
}
path = ""
svg_attr = {
'class': 'curve',
@@ -215,73 +212,13 @@ def create_svg_path(item: LTCurve):
return path, svg_attr
def __render_curve(html: HTMLMaker, ltpage: PageInterpreter, item: LTCurve):
css = {
'position': 'absolute',
'left': f'{item.x0}px',
'bottom': f'{item.y0}px',
'top': f'{item.y1}px',
'right': f'{item.x1}px',
'width': f'{item.width}px',
'height': f'{item.height}px',
}
path = ""
svg_attr = {
'class': 'curve',
'z-index': 2
}
path_css = {
# def render_textblock(html: HTMLMaker, ltpage: PageInterpreter, item: LTTextBlock):
# (x0, y0, x1, y1) = (INF, INF, -INF, -INF)
}
# Compute ViewBox
p_vals = { "x": [], "y": [] }
for p in item.paths:
for pt in p.points:
p_vals['x'].append(pt.x)
p_vals['y'].append(pt.y)
mx_x = max([*p_vals['x'], 1])
mn_x = min([*p_vals['x'], 0])
mx_y = max([*p_vals['y'], 1])
mn_y = min([*p_vals['y'], 0])
if mx_x == mn_x:
mn_x -= 1
if mx_y == mn_y:
mn_y -= 1
dx = 1/( mx_x - mn_x )
dy = (item.height/item.width)/( mx_y - mn_y )
ROUND_VAL = 5
svg_attr.update({
# "viewBox": f"{min(p_vals['x'])} {min(p_vals['y'])} {max(p_vals['x'])} {max(p_vals['y'])}",
"viewBox": f"{round(mn_x*dx, ROUND_VAL)} {round(mn_y*dy, ROUND_VAL)} {round(mx_x*dx , ROUND_VAL)} {round(mx_y*dy, ROUND_VAL)}",
})
# Compute shape
for p in item.paths:
if p.method == CurvePath.METHOD.MOVE_TO:
path += f'M{" ".join( f"{round(dx*pt.x, ROUND_VAL)} {round(dy*pt.y, ROUND_VAL)}" for pt in p.points )} '
elif p.method == CurvePath.METHOD.LINE_TO:
path += f'L{" ".join( f"{round(dx*pt.x, ROUND_VAL)} {round(dy*pt.y, ROUND_VAL)}" for pt in p.points )} '
elif p.method == CurvePath.METHOD.CLOSE_PATH:
path += 'Z'
elif p.method == CurvePath.METHOD.CURVE_BOTH_TO:
path += f'C{" ".join( f"{round(dx*pt.x, ROUND_VAL)} {round(dy*pt.y, ROUND_VAL)}" for pt in p.points )} '
stroke_col = get_color(item.stroke)
fill_col = get_color(item.fill)
if fill_col:
path_css['fill'] = fill_col
if stroke_col:
path_css['stroke'] = stroke_col
with html.elm('svg', svg_attr, css):
html.singleton('path', {
"d": path,
**path_css
}, {
**path_css
})
# for c_block in item:
# x0 = min(x0, c_block.x0)
# y0 = min(y0, c_block.y0)
# x1 = max(x1, c_block.x1)
# y1 = max(y1, c_block.y1)
# with html.elm('span')
+6 -6
View File
@@ -14,38 +14,38 @@ log = get_logger('commands')
# No Support
# @PDFCommands.add('W','W_a', 'sh', 'ET', 'BX', 'EX' ,'BI', 'ID', 'gs')
# def do_no_support(stack: PDFStateStack) -> PDFStateStack:
# # log.warning("Unsupported command")
# # log.debug("Unsupported command")
# return stack
# marked content operators
@PDFCommands.add('MP')
def do_MP(stack: PDFStateStack, tag) -> PDFStateStack:
log.warning(f"Unsupported command - do_tag {[tag]}")
log.debug(f"Unsupported command - do_tag {[tag]}")
# self.device.do_tag(tag)
return stack
@PDFCommands.add('DP')
def do_DP(stack: PDFStateStack, tag, props=None) -> PDFStateStack:
log.warning(f"Unsupported command - do_tag {[tag.name, type(tag), props]}")
log.debug(f"Unsupported command - do_tag {[tag.name, type(tag), props]}")
# self.device.do_tag(tag)
return stack
@PDFCommands.add('BMC')
def do_BMC(stack: PDFStateStack, tag) -> PDFStateStack:
log.warning(f"Unsupported command - begin_tag {[tag.name, type(tag)]}")
log.debug(f"Unsupported command - begin_tag {[tag.name, type(tag)]}")
# self.device.begin_tag(tag)
return stack
@PDFCommands.add('BDC')
def do_BDC(stack: PDFStateStack, tag, props=None) -> PDFStateStack:
log.warning(f"Unsupported command - begin_tag {[tag.name, type(tag), props]}")
log.debug(f"Unsupported command - begin_tag {[tag.name, type(tag), props]}")
# self.device.begin_tag(tag)
return stack
@PDFCommands.add('EMC')
def do_EMC(stack: PDFStateStack) -> PDFStateStack:
# self.device.end_tag(tag)
log.warning("Unsupported command - end_tag")
log.debug("Unsupported command - end_tag")
return stack
@PDFCommands.add('q')
@@ -90,9 +90,9 @@ class LTComponent(LTItem):
##
class LTContainer(LTComponent):
def __init__(self, bbox: Bbox, objs: List[LTItem] = []):
def __init__(self, bbox: Bbox, objs: List[LTItem] = None):
LTComponent.__init__(self, bbox)
self._objs: List[LTItem] = objs
self._objs: List[LTItem] = [] if objs is None else objs
return
def __iter__(self):
+1 -1
View File
@@ -10,7 +10,7 @@ import sys
with open('README.md') as readme_file:
SETUP = {
"name": "pdfmajor",
"version": "1.3.4",
"version": "1.3.6",
"packages": find_packages(include=['pdfmajor*']),
"install_requires": [
'pycryptodome',
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
+35
View File
@@ -0,0 +1,35 @@
import os
from tqdm import tqdm
# from pdfmajor.extractor import extract_items_from_pdf
from pdfmajor.interpreter import LTCharBlock, PDFInterpreter, logging
from pdfmajor.interpreter import PageInterpreter
from pdfmajor.interpreter.commands import LTContainer
CUR_PATH = os.path.dirname(os.path.dirname(__file__))
INPUT_FOLDER = os.path.join(
CUR_PATH, "tests/samples/pdf"
)
FILES = os.listdir(INPUT_FOLDER)
NUM_LOOPS = 10
def count_elms(file_path: str):
count = 0
for page in PDFInterpreter(file_path):
for item in page:
if isinstance(item, LTContainer):
count += len([*item])
else:
count += 1
return count
with tqdm(desc='Cache Test', total=len(FILES)*NUM_LOOPS) as waiter:
for file_name in FILES:
waiter.set_description(f"Cache Test - [{file_name}]")
file_path = os.path.join(INPUT_FOLDER, file_name)
last_count = count_elms(file_path)
for _ in range(NUM_LOOPS):
new_count = count_elms(file_path)
assert new_count == last_count
last_count = new_count
waiter.update()
+4 -5
View File
@@ -52,11 +52,10 @@ def main(args=None):
if len(parsed_args.files) == 1:
if parsed_args.output_file != "-":
func_args['output_file'] = open(parsed_args.output_file, "wb")
with open(parsed_args.files[0], 'rb') as input_file:
convert_file(
input_file,
**func_args
)
convert_file(
parsed_args.files[0],
**func_args
)
else:
print("Multiple files not supported yet.")
return 1