2 Commits
Author SHA1 Message Date
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
6 changed files with 47 additions and 111 deletions

No files matched your search

+1 -1
View File
@@ -1 +1 @@
v1.3.4 v1.3.5
+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. SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
""" """
__version__ = (1, 3, 4) __version__ = (1, 3, 5)
if __name__ == '__main__': if __name__ == '__main__':
print( print(
+2 -2
View File
@@ -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
View File
@@ -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
})
+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.3.4", "version": "1.3.5",
"packages": find_packages(include=['pdfmajor*']), "packages": find_packages(include=['pdfmajor*']),
"install_requires": [ "install_requires": [
'pycryptodome', 'pycryptodome',
+4 -5
View File
@@ -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