Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bab104885f | ||
|
|
98f18c3e32 | ||
|
|
ac2c462561 | ||
|
|
19712f1f0f |
No files matched your search
@@ -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, 4)
|
__version__ = (1, 3, 6)
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
print(
|
print(
|
||||||
|
|||||||
@@ -27,9 +27,9 @@ def convert_file(
|
|||||||
):
|
):
|
||||||
if output_file is None:
|
if output_file is None:
|
||||||
output_file = os.path.join(
|
output_file = os.path.join(
|
||||||
os.path.dirname(input_file.name),
|
os.path.dirname(input_file),
|
||||||
os.path.basename(
|
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:
|
if re.search(re.escape(out_type) + r'$', output_file ) is None:
|
||||||
|
|||||||
+38
-101
@@ -5,6 +5,8 @@ from ..interpreter import LTImage, LTTextBlock, LTCharBlock, LTChar, LTCurve, LT
|
|||||||
from ..interpreter.commands import LTItem
|
from ..interpreter.commands import LTItem
|
||||||
from ..interpreter.commands.state import CurvePath, PDFColor
|
from ..interpreter.commands.state import CurvePath, PDFColor
|
||||||
|
|
||||||
|
INF = (1<<31) - 1
|
||||||
|
|
||||||
def convert_to_html(
|
def convert_to_html(
|
||||||
input_file_path: str,
|
input_file_path: str,
|
||||||
output_file_path: str,
|
output_file_path: str,
|
||||||
@@ -79,33 +81,33 @@ def render_page(html: HTMLMaker, ltpage: PageInterpreter):
|
|||||||
}):
|
}):
|
||||||
html.write(item.get_text())
|
html.write(item.get_text())
|
||||||
elif isinstance(item, LTTextBlock):
|
elif isinstance(item, LTTextBlock):
|
||||||
for char in item:
|
with html.elm('span', {'class': 'text-block'}):
|
||||||
render(char)
|
for char in item:
|
||||||
|
render(char)
|
||||||
elif isinstance(item, LTCharBlock):
|
elif isinstance(item, LTCharBlock):
|
||||||
with html.elm('span', {'class': 'char-block'}):
|
with html.elm('span', { 'class': 'char-block' }, {
|
||||||
with html.elm('p', { 'class': 'text-block' }, {
|
'position': 'absolute',
|
||||||
'position': 'absolute',
|
'left': f'{item.x0}',
|
||||||
'left': f'{item.x0}',
|
'bottom': f'{item.y0}',
|
||||||
'bottom': f'{item.y0}',
|
'font-size': f'{item.size}px',
|
||||||
'font-size': f'{item.size}px',
|
'font-family': item.fontname,
|
||||||
'font-family': item.fontname,
|
'font-weight': item.font.font_weight,
|
||||||
'font-weight': item.font.font_weight,
|
'color': get_color(item.color, default="black"),
|
||||||
'color': get_color(item.color, default="black"),
|
'width': str(item.width) + "px",
|
||||||
'width': str(item.width) + "px",
|
'min-width': str(item.width) + "px",
|
||||||
'min-width': str(item.width) + "px",
|
'max-width': str(item.width) + "px",
|
||||||
'max-width': str(item.width) + "px",
|
'height': str(item.height) + "px",
|
||||||
'height': str(item.height) + "px",
|
'min-height': str(item.height) + "px",
|
||||||
'min-height': str(item.height) + "px",
|
'max-height': str(item.height) + "px",
|
||||||
'max-height': str(item.height) + "px",
|
'transform': f'skew({item.font.italic_angle}deg, 0deg)',
|
||||||
'transform': f'skew({item.font.italic_angle}deg, 0deg)',
|
'text-align': 'center',
|
||||||
'text-align': 'center',
|
'display': 'flex',
|
||||||
'display': 'flex',
|
'flex-flow': 'row nowrap',
|
||||||
'flex-flow': 'row nowrap',
|
'justify-content': 'space-between',
|
||||||
'justify-content': 'space-between',
|
# 'padding-left': item.font.leading
|
||||||
# 'padding-left': item.font.leading
|
}):
|
||||||
}):
|
for char in item:
|
||||||
for char in item:
|
render(char)
|
||||||
render(char)
|
|
||||||
|
|
||||||
with html.elm('div', { "class": 'page', "id": f"page-{ltpage.page_num}" }, {
|
with html.elm('div', { "class": 'page', "id": f"page-{ltpage.page_num}" }, {
|
||||||
'width': f'{ltpage.width}px',
|
'width': f'{ltpage.width}px',
|
||||||
@@ -147,7 +149,7 @@ def render_curve(html: HTMLMaker, ltpage: PageInterpreter, item: LTCurve):
|
|||||||
'right': f'{item.x1}px',
|
'right': f'{item.x1}px',
|
||||||
'width': f'{item.width}px',
|
'width': f'{item.width}px',
|
||||||
'height': f'{item.height}px',
|
'height': f'{item.height}px',
|
||||||
}) as svg:
|
}):
|
||||||
html.singleton('path', {
|
html.singleton('path', {
|
||||||
"d": path,
|
"d": path,
|
||||||
**path_css
|
**path_css
|
||||||
@@ -156,11 +158,6 @@ def render_curve(html: HTMLMaker, ltpage: PageInterpreter, item: LTCurve):
|
|||||||
})
|
})
|
||||||
|
|
||||||
def create_svg_path(item: LTCurve):
|
def create_svg_path(item: LTCurve):
|
||||||
css = {
|
|
||||||
'position': 'absolute',
|
|
||||||
'left': f'{item.x0}px',
|
|
||||||
'bottom': f'{item.y0}px',
|
|
||||||
}
|
|
||||||
path = ""
|
path = ""
|
||||||
svg_attr = {
|
svg_attr = {
|
||||||
'class': 'curve',
|
'class': 'curve',
|
||||||
@@ -215,73 +212,13 @@ def create_svg_path(item: LTCurve):
|
|||||||
|
|
||||||
return path, svg_attr
|
return path, svg_attr
|
||||||
|
|
||||||
def __render_curve(html: HTMLMaker, ltpage: PageInterpreter, item: LTCurve):
|
# def render_textblock(html: HTMLMaker, ltpage: PageInterpreter, item: LTTextBlock):
|
||||||
css = {
|
# (x0, y0, x1, y1) = (INF, INF, -INF, -INF)
|
||||||
'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 = {
|
|
||||||
|
|
||||||
}
|
# for c_block in item:
|
||||||
|
# x0 = min(x0, c_block.x0)
|
||||||
# Compute ViewBox
|
# y0 = min(y0, c_block.y0)
|
||||||
p_vals = { "x": [], "y": [] }
|
# x1 = max(x1, c_block.x1)
|
||||||
|
# y1 = max(y1, c_block.y1)
|
||||||
for p in item.paths:
|
|
||||||
for pt in p.points:
|
# with html.elm('span')
|
||||||
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
|
|
||||||
})
|
|
||||||
@@ -14,38 +14,38 @@ log = get_logger('commands')
|
|||||||
# No Support
|
# No Support
|
||||||
# @PDFCommands.add('W','W_a', 'sh', 'ET', 'BX', 'EX' ,'BI', 'ID', 'gs')
|
# @PDFCommands.add('W','W_a', 'sh', 'ET', 'BX', 'EX' ,'BI', 'ID', 'gs')
|
||||||
# def do_no_support(stack: PDFStateStack) -> PDFStateStack:
|
# def do_no_support(stack: PDFStateStack) -> PDFStateStack:
|
||||||
# # log.warning("Unsupported command")
|
# # log.debug("Unsupported command")
|
||||||
# return stack
|
# return stack
|
||||||
|
|
||||||
# marked content operators
|
# marked content operators
|
||||||
@PDFCommands.add('MP')
|
@PDFCommands.add('MP')
|
||||||
def do_MP(stack: PDFStateStack, tag) -> PDFStateStack:
|
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)
|
# self.device.do_tag(tag)
|
||||||
return stack
|
return stack
|
||||||
|
|
||||||
@PDFCommands.add('DP')
|
@PDFCommands.add('DP')
|
||||||
def do_DP(stack: PDFStateStack, tag, props=None) -> PDFStateStack:
|
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)
|
# self.device.do_tag(tag)
|
||||||
return stack
|
return stack
|
||||||
|
|
||||||
@PDFCommands.add('BMC')
|
@PDFCommands.add('BMC')
|
||||||
def do_BMC(stack: PDFStateStack, tag) -> PDFStateStack:
|
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)
|
# self.device.begin_tag(tag)
|
||||||
return stack
|
return stack
|
||||||
|
|
||||||
@PDFCommands.add('BDC')
|
@PDFCommands.add('BDC')
|
||||||
def do_BDC(stack: PDFStateStack, tag, props=None) -> PDFStateStack:
|
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)
|
# self.device.begin_tag(tag)
|
||||||
return stack
|
return stack
|
||||||
|
|
||||||
@PDFCommands.add('EMC')
|
@PDFCommands.add('EMC')
|
||||||
def do_EMC(stack: PDFStateStack) -> PDFStateStack:
|
def do_EMC(stack: PDFStateStack) -> PDFStateStack:
|
||||||
# self.device.end_tag(tag)
|
# self.device.end_tag(tag)
|
||||||
log.warning("Unsupported command - end_tag")
|
log.debug("Unsupported command - end_tag")
|
||||||
return stack
|
return stack
|
||||||
|
|
||||||
@PDFCommands.add('q')
|
@PDFCommands.add('q')
|
||||||
|
|||||||
@@ -90,9 +90,9 @@ class LTComponent(LTItem):
|
|||||||
##
|
##
|
||||||
class LTContainer(LTComponent):
|
class LTContainer(LTComponent):
|
||||||
|
|
||||||
def __init__(self, bbox: Bbox, objs: List[LTItem] = []):
|
def __init__(self, bbox: Bbox, objs: List[LTItem] = None):
|
||||||
LTComponent.__init__(self, bbox)
|
LTComponent.__init__(self, bbox)
|
||||||
self._objs: List[LTItem] = objs
|
self._objs: List[LTItem] = [] if objs is None else objs
|
||||||
return
|
return
|
||||||
|
|
||||||
def __iter__(self):
|
def __iter__(self):
|
||||||
|
|||||||
@@ -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.3.4",
|
"version": "1.3.6",
|
||||||
"packages": find_packages(include=['pdfmajor*']),
|
"packages": find_packages(include=['pdfmajor*']),
|
||||||
"install_requires": [
|
"install_requires": [
|
||||||
'pycryptodome',
|
'pycryptodome',
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -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
@@ -52,11 +52,10 @@ def main(args=None):
|
|||||||
if len(parsed_args.files) == 1:
|
if len(parsed_args.files) == 1:
|
||||||
if parsed_args.output_file != "-":
|
if parsed_args.output_file != "-":
|
||||||
func_args['output_file'] = open(parsed_args.output_file, "wb")
|
func_args['output_file'] = open(parsed_args.output_file, "wb")
|
||||||
with open(parsed_args.files[0], 'rb') as input_file:
|
convert_file(
|
||||||
convert_file(
|
parsed_args.files[0],
|
||||||
input_file,
|
**func_args
|
||||||
**func_args
|
)
|
||||||
)
|
|
||||||
else:
|
else:
|
||||||
print("Multiple files not supported yet.")
|
print("Multiple files not supported yet.")
|
||||||
return 1
|
return 1
|
||||||
|
|||||||
Reference in new issue
Block a user