Compare commits
15
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
14ba20be55 | ||
|
|
18938d91ea | ||
|
|
eb38854076 | ||
|
|
671ae5bc4a | ||
|
|
844408c6e3 | ||
|
|
bab104885f | ||
|
|
98f18c3e32 | ||
|
|
ac2c462561 | ||
|
|
19712f1f0f | ||
|
|
ec02a7d38b | ||
|
|
0345ca01ba | ||
|
|
b36f6bc907 | ||
|
|
f9f9a59793 | ||
|
|
b7026793d7 | ||
|
|
addd59ed0f |
No files matched your search
@@ -10,4 +10,5 @@ tests/*.txt
|
||||
.idea/
|
||||
.vscode
|
||||
tests/samples/output
|
||||
samples
|
||||
**__pycache**
|
||||
@@ -19,7 +19,7 @@ See documentation [here](https://asosnovsky.github.io/pdfmajor/).
|
||||
|
||||
## Basic Example
|
||||
```py
|
||||
from pdfmajor.interpreter import PageInterpreter
|
||||
from pdfmajor.interpreter import PDFInterpreter
|
||||
|
||||
for page in PDFInterpreter("/path/to/pdf.pdf"):
|
||||
print("page start", page.page_num)
|
||||
|
||||
+12
-7
@@ -6,13 +6,18 @@ cd ..
|
||||
|
||||
./bin/clean.sh
|
||||
|
||||
python setup.py sdist bdist_wheel
|
||||
|
||||
git add .
|
||||
git commit
|
||||
git push
|
||||
if ./bin/run_tests.sh; then
|
||||
python setup.py sdist bdist_wheel
|
||||
|
||||
git tag $(cat .version)
|
||||
git push -u origin $(cat .version)
|
||||
git add .
|
||||
git commit
|
||||
git push
|
||||
|
||||
twine upload dist/*
|
||||
git tag $(cat .version)
|
||||
git push -u origin $(cat .version)
|
||||
|
||||
twine upload dist/*
|
||||
else
|
||||
echo ">>> Stopped due to test failure!"
|
||||
fi
|
||||
Executable
+13
@@ -0,0 +1,13 @@
|
||||
#!/bin/bash
|
||||
# Get working dir (this will make sure that the script will work the same anywhere you run it)
|
||||
WK_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
||||
cd $WK_DIR
|
||||
cd ..
|
||||
|
||||
for filename in ./tests/test*.py; do
|
||||
if python $filename ; then
|
||||
echo woohoo
|
||||
else
|
||||
exit 1
|
||||
fi
|
||||
done
|
||||
@@ -24,7 +24,7 @@ See documentation [here](https://asosnovsky.github.io/pdfmajor/).
|
||||
|
||||
## Basic Example
|
||||
```py
|
||||
from pdfmajor.interpreter import PageInterpreter
|
||||
from pdfmajor.interpreter import PDFInterpreter
|
||||
|
||||
for page in PDFInterpreter("/path/to/pdf.pdf"):
|
||||
print("page start", page.page_num)
|
||||
@@ -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, 2c)
|
||||
__version__ = (1, 3, 6)
|
||||
|
||||
if __name__ == '__main__':
|
||||
print(
|
||||
|
||||
@@ -8,6 +8,7 @@ from .html import convert_to_html
|
||||
from .xml import convert_to_xml
|
||||
from .json import convert_to_json
|
||||
from .text import convert_to_text
|
||||
from .yaml import convert_to_yaml
|
||||
from ..utils import logging
|
||||
|
||||
def convert_file(
|
||||
@@ -26,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:
|
||||
@@ -75,6 +76,20 @@ def convert_file(
|
||||
dont_export_images=dont_export_images,
|
||||
debug_level=debug_level,
|
||||
)
|
||||
elif out_type == 'yaml' or out_type == 'yml':
|
||||
return convert_to_yaml(
|
||||
input_file_path=input_file,
|
||||
output_file_path=output_file,
|
||||
image_folder_path=image_folder_path,
|
||||
codec=codec,
|
||||
maxpages=maxpages,
|
||||
password=password,
|
||||
caching=caching,
|
||||
check_extractable=check_extractable,
|
||||
pagenos=pagenos,
|
||||
dont_export_images=dont_export_images,
|
||||
debug_level=debug_level,
|
||||
)
|
||||
elif out_type == 'text':
|
||||
return convert_to_text(
|
||||
input_file_path=input_file,
|
||||
@@ -89,4 +104,4 @@ def convert_file(
|
||||
dont_export_images=dont_export_images,
|
||||
debug_level=debug_level,
|
||||
)
|
||||
else: raise Exception("Please specify out_type as 'html' or 'xml' or 'json' or 'text'")
|
||||
else: raise Exception("Please specify out_type as 'html' or 'xml' or 'json' or 'text' or 'yaml'")
|
||||
+130
-99
@@ -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,
|
||||
@@ -43,19 +45,21 @@ def convert_to_html(
|
||||
render_page(html, page)
|
||||
|
||||
def render_page(html: HTMLMaker, ltpage: PageInterpreter):
|
||||
def render(item: LTItem):
|
||||
def render(item: LTItem, block = True):
|
||||
if isinstance(item, LTCurve):
|
||||
render_curve(html, item)
|
||||
render_curve(html, ltpage, item)
|
||||
elif isinstance(item, LTXObject):
|
||||
with html.elm('div', { "class": 'xobject' }, {
|
||||
'position': 'absolute',
|
||||
# 'position': 'absolute',
|
||||
'left': f'{item.x0}px',
|
||||
'right': f'{item.x1}px',
|
||||
'bottom': f'{item.y0}px',
|
||||
'top': f'{item.y1}px',
|
||||
'width': f'{item.width}px',
|
||||
'height': f'{item.height}px',
|
||||
}):
|
||||
for child in item:
|
||||
render(child)
|
||||
render(child, False)
|
||||
elif isinstance(item, LTImage):
|
||||
name = item.name
|
||||
# if html.imagewriter is not None:
|
||||
@@ -68,42 +72,42 @@ def render_page(html: HTMLMaker, ltpage: PageInterpreter):
|
||||
})
|
||||
elif isinstance(item, LTChar):
|
||||
with html.elm('span', { 'class': 'char' }, {
|
||||
'width': item.width,
|
||||
'min-width': item.width,
|
||||
'max-width': item.width,
|
||||
'height': item.height,
|
||||
'min-height': item.height,
|
||||
'max-height': item.height,
|
||||
'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",
|
||||
}):
|
||||
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': item.width,
|
||||
'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)
|
||||
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',
|
||||
@@ -125,69 +129,96 @@ def get_color(col: PDFColor, default: str = ""):
|
||||
else:
|
||||
return default
|
||||
|
||||
def render_curve(html: HTMLMaker, item: LTCurve):
|
||||
css = {
|
||||
'position': 'absolute',
|
||||
'left': f'{item.x0}px',
|
||||
'bottom': f'{item.y0}px',
|
||||
'width': f'{item.width}px',
|
||||
'height': f'{item.height}px',
|
||||
}
|
||||
path = ""
|
||||
svg_attr = {
|
||||
'class': 'curve',
|
||||
'z-index': 2
|
||||
}
|
||||
path_css = {
|
||||
def render_curve(html: HTMLMaker, ltpage: PageInterpreter, item: LTCurve):
|
||||
path, svg_attr = create_svg_path(item)
|
||||
|
||||
stroke_col = get_color(item.stroke)
|
||||
fill_col = get_color(item.fill)
|
||||
path_css = {}
|
||||
if fill_col:
|
||||
path_css['fill'] = fill_col
|
||||
if stroke_col:
|
||||
path_css['stroke'] = stroke_col
|
||||
|
||||
}
|
||||
|
||||
# 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)}",
|
||||
with html.elm('svg', svg_attr, css={
|
||||
'border': '1px solid black',
|
||||
'position': 'absolute',
|
||||
'left': f'{item.x0}px',
|
||||
'bottom': f'{ltpage.height - item.y0}px',
|
||||
'top': f'{ltpage.height - item.y1}px',
|
||||
'right': f'{item.x1}px',
|
||||
'width': f'{item.width}px',
|
||||
'height': f'{item.height}px',
|
||||
}):
|
||||
html.singleton('path', {
|
||||
"d": path,
|
||||
**path_css
|
||||
}, {
|
||||
**path_css
|
||||
})
|
||||
|
||||
# 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 )} '
|
||||
if 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 )} '
|
||||
if p.method == CurvePath.METHOD.CLOSE_PATH:
|
||||
path += 'Z'
|
||||
|
||||
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
|
||||
})
|
||||
def create_svg_path(item: LTCurve):
|
||||
path = ""
|
||||
svg_attr = {
|
||||
'class': 'curve',
|
||||
'z-index': 2
|
||||
}
|
||||
|
||||
if item.height > 0:
|
||||
dy = 1.0/item.height
|
||||
dx = (item.width/item.height)/max([(item.x1-item.x0),0.01])
|
||||
svg_attr.update({
|
||||
"viewBox": "0 0 %s %s" % ( item.width/item.height, 1 )
|
||||
})
|
||||
else:
|
||||
dx = 1.0/(item.x1-item.x0)
|
||||
dy = 1.0
|
||||
svg_attr.update({
|
||||
"viewBox": "0 0 1 1"
|
||||
})
|
||||
|
||||
if item.height > 1:
|
||||
svg_attr.update({
|
||||
'height': f'{item.height}px',
|
||||
})
|
||||
else:
|
||||
svg_attr.update({
|
||||
'height': '1px',
|
||||
})
|
||||
|
||||
if item.width > 1:
|
||||
svg_attr.update({
|
||||
'width': f'{item.width}px',
|
||||
})
|
||||
else:
|
||||
svg_attr.update({
|
||||
'width': '1px',
|
||||
})
|
||||
|
||||
def pX(val: float) -> float:
|
||||
return ( val-item.x0 )*dx
|
||||
def pY(val: float) -> float:
|
||||
return ( item.height- (val-item.y0) )*dy
|
||||
|
||||
for p in item.paths:
|
||||
if p.method == CurvePath.METHOD.MOVE_TO:
|
||||
path += f'M{" ".join( f"{pX(p.x)} {pY(p.y)}" for p in p.points )} '
|
||||
elif p.method == CurvePath.METHOD.LINE_TO:
|
||||
path += f'L{" ".join( f"{pX(p.x)} {pY(p.y)}" for p 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"{pX(pt.x)} {pY(pt.y)}" for pt in p.points )} '
|
||||
|
||||
return path, svg_attr
|
||||
|
||||
# def render_textblock(html: HTMLMaker, ltpage: PageInterpreter, item: LTTextBlock):
|
||||
# (x0, y0, x1, y1) = (INF, INF, -INF, -INF)
|
||||
|
||||
# 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')
|
||||
@@ -56,7 +56,6 @@ class HTMLMaker:
|
||||
self.write(f"<{tag_name} {attrs} />", lineend=lineend, deep_space=deep_space)
|
||||
yield
|
||||
|
||||
def singleton(self, tag_name: str, attrs: dict = None, css: dict = None):
|
||||
with self.elm(tag_name, attrs, css, singleton=True):
|
||||
def singleton(self, tag_name: str, attrs: dict = None, css: dict = None, singleton: bool = True):
|
||||
with self.elm(tag_name, attrs, css, singleton=singleton):
|
||||
pass
|
||||
|
||||
@@ -50,7 +50,8 @@ class JSONMakerObject:
|
||||
self.write_prop(key, val)
|
||||
|
||||
def string(self, key: str, val: str):
|
||||
self.write_prop(key, f'"{val}"')
|
||||
val_sant = str(val).replace('"', '\\"')
|
||||
self.write_prop(key, f'"{val_sant}"')
|
||||
|
||||
@contextmanager
|
||||
def object(self, key: str):
|
||||
@@ -109,7 +110,8 @@ class JSONMakerArray:
|
||||
self.write_prop(val)
|
||||
|
||||
def string(self, val: str):
|
||||
self.write_prop(f'"{val}"')
|
||||
val_sant = str(val).replace('"', '\\"')
|
||||
self.write_prop(f'"{val_sant}"')
|
||||
|
||||
@contextmanager
|
||||
def object(self):
|
||||
|
||||
@@ -0,0 +1,117 @@
|
||||
from json import dumps as json_dump
|
||||
from io import TextIOWrapper
|
||||
from contextlib import contextmanager
|
||||
|
||||
@contextmanager
|
||||
def YAMLMaker(file_path: str, codec: str = 'utf-8'):
|
||||
with open(file_path, 'wb') as outf:
|
||||
with YAMLMakerObject(outf, codec=codec) as obj:
|
||||
yield obj
|
||||
|
||||
class YAMLMakerWriter:
|
||||
class AccessError(Exception): pass
|
||||
def __init__(self, outfile: TextIOWrapper, codec: str = 'utf-8', levels_deep: int = -1):
|
||||
self.outfile: TextIOWrapper = outfile
|
||||
self.codec = codec
|
||||
self.levels_deep = levels_deep
|
||||
|
||||
def __enter__(self):
|
||||
if self.levels_deep > 0:
|
||||
self.__write_raw('')
|
||||
self.levels_deep += 1
|
||||
return self
|
||||
|
||||
def __exit__(self, exc_type, exc_val, exc_tb):
|
||||
self.levels_deep -= 1
|
||||
|
||||
def __write_raw(self, text):
|
||||
if self.outfile is not None:
|
||||
if self.codec:
|
||||
text = text.encode(self.codec)
|
||||
self.outfile.write(text)
|
||||
else:
|
||||
raise self.AccessError("Attempting to write to file without an open connection")
|
||||
|
||||
def _write(self, text: str, append_deep: bool = True, deep_space: str = ' ', prefix:str = ''):
|
||||
if not append_deep:
|
||||
deep_space = ""
|
||||
text = prefix + (deep_space*self.levels_deep) + text
|
||||
self.__write_raw(text)
|
||||
|
||||
class YAMLMakerObject(YAMLMakerWriter):
|
||||
def __init__(self, outfile: TextIOWrapper, codec: str = 'utf-8', levels_deep: int = -1, parent_is_array: bool = False):
|
||||
YAMLMakerWriter.__init__(self, outfile, codec, levels_deep)
|
||||
self.keys = set()
|
||||
self.parent_is_array = parent_is_array
|
||||
|
||||
def write(self, key: str, value: str):
|
||||
if key not in self.keys:
|
||||
self.keys.add(key)
|
||||
if len(self.keys) > 1:
|
||||
self._write(f'{key}: {value}', prefix='\n')
|
||||
else:
|
||||
self._write(f'{key}: {value}', prefix='',
|
||||
append_deep=not self.parent_is_array
|
||||
)
|
||||
|
||||
@contextmanager
|
||||
def object(self, key: str):
|
||||
if key not in self.keys:
|
||||
self.keys.add(key)
|
||||
if len(self.keys) > 1:
|
||||
self._write(f'{key}:\n', prefix='\n')
|
||||
else:
|
||||
self._write(f'{key}:\n', prefix='', append_deep=False)
|
||||
with YAMLMakerObject(self.outfile, codec=self.codec, levels_deep=self.levels_deep) as obj:
|
||||
yield obj
|
||||
|
||||
@contextmanager
|
||||
def array(self, key: str):
|
||||
if key not in self.keys:
|
||||
self.keys.add(key)
|
||||
if len(self.keys) > 1:
|
||||
self._write(f'{key}:\n', prefix='\n')
|
||||
else:
|
||||
self._write(f'{key}:\n', prefix='')
|
||||
with YAMLMakerArray(self.outfile, codec=self.codec, levels_deep=self.levels_deep) as obj:
|
||||
yield obj
|
||||
|
||||
def place_object(self, key: str, obj: dict):
|
||||
with self.object(key) as child:
|
||||
for key, val in obj.items():
|
||||
child.write(key, val)
|
||||
|
||||
def place_array(self, key: str, arr: list):
|
||||
with self.array(key) as child:
|
||||
for val in arr:
|
||||
child.write(val)
|
||||
|
||||
|
||||
class YAMLMakerArray(YAMLMakerWriter):
|
||||
def __init__(self, outfile: TextIOWrapper, codec: str = 'utf-8', levels_deep: int = -1):
|
||||
YAMLMakerWriter.__init__(self, outfile, codec, levels_deep)
|
||||
self.childrencount = 0
|
||||
|
||||
def write(self, value: str = ""):
|
||||
self.childrencount += 1
|
||||
if self.childrencount > 1:
|
||||
self._write(f'- {value}', prefix='\n')
|
||||
else:
|
||||
self._write(f'- {value}', prefix='')
|
||||
|
||||
@contextmanager
|
||||
def object(self):
|
||||
self.childrencount += 1
|
||||
if self.childrencount > 1:
|
||||
self._write(f'- ', prefix='\n')
|
||||
else:
|
||||
self._write(f'- ', prefix='')
|
||||
with YAMLMakerObject(self.outfile, codec=self.codec, levels_deep=self.levels_deep, parent_is_array=True) as obj:
|
||||
yield obj
|
||||
|
||||
def place_object(self, obj: dict):
|
||||
with self.object() as child:
|
||||
for key, val in obj.items():
|
||||
child.write(key, val)
|
||||
|
||||
|
||||
@@ -0,0 +1,144 @@
|
||||
from typing import List
|
||||
|
||||
from .writers.yaml import YAMLMaker, YAMLMakerObject, YAMLMakerArray
|
||||
from ..interpreter import PDFInterpreter, PageInterpreter, logging
|
||||
from ..interpreter import LTImage, LTTextBlock, LTCharBlock, LTCurve, LTXObject
|
||||
from ..interpreter.commands import LTItem
|
||||
from ..interpreter.commands import LTRect, LTLine
|
||||
from ..interpreter.commands.state import CurvePath, PDFColor
|
||||
from ..parser.PSStackParser import PSLiteral
|
||||
from ..utils import enc
|
||||
|
||||
def normalize_color_values(values: list):
|
||||
ret = []
|
||||
for v in values:
|
||||
if isinstance(v, PSLiteral):
|
||||
ret.append( enc(str(v.name)).decode() )
|
||||
else:
|
||||
ret.append( enc(str(v)).decode() )
|
||||
return ret
|
||||
|
||||
def convert_to_yaml(
|
||||
input_file_path: str,
|
||||
output_file_path: str,
|
||||
image_folder_path: str = None,
|
||||
dont_export_images: bool = False,
|
||||
codec: str = 'utf-8',
|
||||
maxpages: int = 0,
|
||||
password: str = None,
|
||||
caching: bool = True,
|
||||
check_extractable: bool = True,
|
||||
pagenos: List[int] = None,
|
||||
debug_level: int = logging.WARNING,
|
||||
):
|
||||
intepreter = PDFInterpreter(input_file_path,
|
||||
maxpages=maxpages,
|
||||
password=password,
|
||||
caching=caching,
|
||||
check_extractable=check_extractable,
|
||||
pagenos=pagenos,
|
||||
debug_level=debug_level
|
||||
)
|
||||
with YAMLMaker(output_file_path, codec=codec) as yaml:
|
||||
with yaml.array("pages") as arr:
|
||||
for page in intepreter:
|
||||
with arr.object() as page_obj:
|
||||
render_page(page_obj, page)
|
||||
|
||||
def render_page(yaml: YAMLMakerObject, ltpage: PageInterpreter):
|
||||
yaml.write("id", ltpage.page_num)
|
||||
yaml.write("width", ltpage.width)
|
||||
yaml.write("height", ltpage.height)
|
||||
with yaml.array("children") as chil_arr:
|
||||
for child in ltpage:
|
||||
with chil_arr.object() as chil_obj:
|
||||
render(chil_obj, child)
|
||||
|
||||
def render(yaml: YAMLMakerObject, item: LTItem):
|
||||
if isinstance(item, LTCurve):
|
||||
place_curve(yaml, item)
|
||||
elif isinstance(item, LTXObject):
|
||||
yaml.write("type", 'xobject')
|
||||
yaml.write("x0", item.x0)
|
||||
yaml.write("x1", item.x1)
|
||||
yaml.write("y0", item.y0)
|
||||
yaml.write("y1", item.y1)
|
||||
with yaml.array("children") as chil_arr:
|
||||
for child in item:
|
||||
with chil_arr.object() as chil_obj:
|
||||
render(chil_obj, child)
|
||||
elif isinstance(item, LTImage):
|
||||
# if yaml.imagewriter is not None:
|
||||
# name = yaml.imagewriter.export_image(item)
|
||||
yaml.write("type", 'img')
|
||||
yaml.write("src", enc(item.name, None))
|
||||
yaml.write("x0", item.x0)
|
||||
yaml.write("x1", item.x1)
|
||||
yaml.write("y0", item.y0)
|
||||
yaml.write("y1", item.y1)
|
||||
elif isinstance(item, LTTextBlock):
|
||||
place_text_block(yaml, item)
|
||||
|
||||
def place_text_block(yaml: YAMLMakerObject, text_block: LTTextBlock):
|
||||
yaml.write("type", 'text-block')
|
||||
yaml.write("x0", text_block.x0)
|
||||
yaml.write("x1", text_block.x1)
|
||||
yaml.write("y0", text_block.y0)
|
||||
yaml.write("y1", text_block.y1)
|
||||
with yaml.array("char_blocks") as cb_arr:
|
||||
for cb in text_block:
|
||||
with cb_arr.object() as cb_obj:
|
||||
place_char_block(cb_obj, cb)
|
||||
|
||||
def place_char_block(yaml: YAMLMakerObject, char_block: LTCharBlock):
|
||||
yaml.write("size", char_block.size)
|
||||
yaml.write("x0", char_block.x0)
|
||||
yaml.write("x1", char_block.x1)
|
||||
yaml.write("y0", char_block.y0)
|
||||
yaml.write("y1", char_block.y1)
|
||||
if char_block.color.color_space is not None:
|
||||
yaml.place_object("color", {
|
||||
"type": enc(char_block.color.color_space.name).decode(),
|
||||
"value": normalize_color_values(char_block.color.values)
|
||||
})
|
||||
for key, value in char_block.font.descriptor.items():
|
||||
if key != "Type" and "FontFile" not in key:
|
||||
yaml.write(key, value)
|
||||
yaml.write("text",
|
||||
"".join([
|
||||
char.get_text()
|
||||
for char in char_block
|
||||
])
|
||||
)
|
||||
|
||||
def place_curve(yaml: YAMLMakerObject, item: LTCurve):
|
||||
if isinstance(item, LTRect):
|
||||
yaml.write("type", 'rect')
|
||||
elif isinstance(item, LTLine):
|
||||
yaml.write("type", 'line')
|
||||
else:
|
||||
yaml.write("type", 'curve')
|
||||
with yaml.array('paths') as paths_arr:
|
||||
for path in item.paths:
|
||||
paths_arr.place_object({
|
||||
"type": path.method.name,
|
||||
"points": [
|
||||
{"x": point.x, "y": point.y}
|
||||
for point in path.points
|
||||
]
|
||||
})
|
||||
|
||||
yaml.write("x0", item.x0)
|
||||
yaml.write("x1", item.x1)
|
||||
yaml.write("y0", item.y0)
|
||||
yaml.write("y1", item.y1)
|
||||
if item.fill.color_space is not None:
|
||||
yaml.place_object("fill", {
|
||||
"type": enc(item.fill.color_space.name).decode(),
|
||||
"value": normalize_color_values(item.fill.values)
|
||||
})
|
||||
if item.stroke.color_space is not None:
|
||||
yaml.place_object("stroke", {
|
||||
"type": enc(item.stroke.color_space.name).decode(),
|
||||
"value": normalize_color_values(item.stroke.values)
|
||||
})
|
||||
@@ -20,8 +20,6 @@ log = get_logger('process_command_stream')
|
||||
class CommandProcessorError(Exception): pass
|
||||
|
||||
def process_command_stream(streams: List[PDFStream], font_cache: dict = None, state: PDFStateStack = None):
|
||||
if state is None:
|
||||
state = PDFStateStack()
|
||||
if font_cache is None:
|
||||
font_cache = {}
|
||||
parser = PDFContentParser(streams)
|
||||
@@ -41,14 +39,14 @@ def process_command_stream(streams: List[PDFStream], font_cache: dict = None, st
|
||||
else:
|
||||
func(state)
|
||||
else:
|
||||
log.warning('Unknown operator: %r (i.e. %r)' % (name, method))
|
||||
log.debug('Unknown operator: %r (i.e. %r)' % (name, method))
|
||||
else:
|
||||
state.argstack.append(obj)
|
||||
|
||||
for complete_item in state.complete_layout_items:
|
||||
if isinstance(complete_item, LTXObject):
|
||||
xobj_state = prep_state(
|
||||
state=PDFGraphicState(),
|
||||
state=PDFStateStack(),
|
||||
ctm=complete_item.t_matrix,
|
||||
resources=complete_item.resources,
|
||||
font_cache=font_cache
|
||||
|
||||
@@ -14,42 +14,42 @@ 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]}")
|
||||
# self.device.do_tag(tag)
|
||||
return stack
|
||||
# @PDFCommands.add('MP')
|
||||
# def do_MP(stack: PDFStateStack, tag) -> PDFStateStack:
|
||||
# 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]}")
|
||||
# self.device.do_tag(tag)
|
||||
return stack
|
||||
# @PDFCommands.add('DP')
|
||||
# def do_DP(stack: PDFStateStack, tag, props=None) -> PDFStateStack:
|
||||
# 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)]}")
|
||||
# self.device.begin_tag(tag)
|
||||
return stack
|
||||
# @PDFCommands.add('BMC')
|
||||
# def do_BMC(stack: PDFStateStack, tag) -> PDFStateStack:
|
||||
# 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]}")
|
||||
# self.device.begin_tag(tag)
|
||||
return stack
|
||||
# @PDFCommands.add('BDC')
|
||||
# def do_BDC(stack: PDFStateStack, tag, props=None) -> PDFStateStack:
|
||||
# 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")
|
||||
return stack
|
||||
# @PDFCommands.add('EMC')
|
||||
# def do_EMC(stack: PDFStateStack) -> PDFStateStack:
|
||||
# # self.device.end_tag(tag)
|
||||
# log.debug("Unsupported command - end_tag")
|
||||
# return stack
|
||||
|
||||
@PDFCommands.add('q')
|
||||
def do_q(stack: PDFStateStack) -> PDFStateStack:
|
||||
def save_state(stack: PDFStateStack) -> PDFStateStack:
|
||||
stack.gstack.append([
|
||||
stack.t_matrix,
|
||||
stack.text.copy(),
|
||||
@@ -58,136 +58,136 @@ def do_q(stack: PDFStateStack) -> PDFStateStack:
|
||||
return stack
|
||||
|
||||
@PDFCommands.add('Q')
|
||||
def do_Q(stack: PDFStateStack) -> PDFStateStack:
|
||||
def restore_state(stack: PDFStateStack) -> PDFStateStack:
|
||||
if len(stack.gstack) > 0:
|
||||
stack.t_matrix, stack.text, stack.graphicstate = stack.gstack.pop()
|
||||
return stack
|
||||
|
||||
# concat-matrix
|
||||
@PDFCommands.add('cm')
|
||||
def do_cm(stack: PDFStateStack, a1, b1, c1, d1, e1, f1) -> PDFStateStack:
|
||||
def concat_transition_matrix(stack: PDFStateStack, a1, b1, c1, d1, e1, f1) -> PDFStateStack:
|
||||
stack.t_matrix = mult_matrix((a1, b1, c1, d1, e1, f1), stack.t_matrix)
|
||||
return stack
|
||||
|
||||
# setlinewidth
|
||||
@PDFCommands.add('w')
|
||||
def do_w(stack: PDFStateStack, linewidth) -> PDFStateStack:
|
||||
def set_line_width(stack: PDFStateStack, linewidth) -> PDFStateStack:
|
||||
stack.graphics.linewidth = linewidth
|
||||
return stack
|
||||
|
||||
# setlinecap
|
||||
@PDFCommands.add('J')
|
||||
def do_J(stack: PDFStateStack, linecap) -> PDFStateStack:
|
||||
def set_linecap(stack: PDFStateStack, linecap) -> PDFStateStack:
|
||||
stack.graphics.linecap = linecap
|
||||
return stack
|
||||
|
||||
# setlinejoin
|
||||
@PDFCommands.add('j')
|
||||
def do_j(stack: PDFStateStack, linejoin) -> PDFStateStack:
|
||||
def set_linejoin(stack: PDFStateStack, linejoin) -> PDFStateStack:
|
||||
stack.graphics.linejoin = linejoin
|
||||
return stack
|
||||
|
||||
# setmiterlimit
|
||||
@PDFCommands.add('M')
|
||||
def do_M(stack: PDFStateStack, miterlimit) -> PDFStateStack:
|
||||
def set_miterlimit(stack: PDFStateStack, miterlimit) -> PDFStateStack:
|
||||
stack.graphics.miterlimit = miterlimit
|
||||
return stack
|
||||
|
||||
# setdash
|
||||
@PDFCommands.add('d')
|
||||
def do_d(stack: PDFStateStack, dash, phase) -> PDFStateStack:
|
||||
def set_dash(stack: PDFStateStack, dash, phase) -> PDFStateStack:
|
||||
stack.graphics.dash = (dash, phase)
|
||||
return stack
|
||||
|
||||
# setintent
|
||||
@PDFCommands.add('ri')
|
||||
def do_ri(stack: PDFStateStack, intent) -> PDFStateStack:
|
||||
def set_intent(stack: PDFStateStack, intent) -> PDFStateStack:
|
||||
stack.graphics.intent = intent
|
||||
return stack
|
||||
|
||||
# setflatness
|
||||
@PDFCommands.add('i')
|
||||
def do_i(stack: PDFStateStack, flatness) -> PDFStateStack:
|
||||
def set_flatness(stack: PDFStateStack, flatness) -> PDFStateStack:
|
||||
stack.graphics.flatness = flatness
|
||||
return stack
|
||||
|
||||
# moveto
|
||||
@PDFCommands.add('m')
|
||||
def do_m(stack: PDFStateStack, x: float, y: float) -> PDFStateStack:
|
||||
def curve_moveto(stack: PDFStateStack, x: float, y: float) -> PDFStateStack:
|
||||
stack.curvestacks.append(CurvePath(
|
||||
CurvePath.METHOD.MOVE_TO,
|
||||
CurvePoint(x, y)
|
||||
CurvePoint(stack.t_matrix, x, y)
|
||||
))
|
||||
return stack
|
||||
|
||||
# lineto
|
||||
@PDFCommands.add('l')
|
||||
def do_l(stack: PDFStateStack, x: float, y: float) -> PDFStateStack:
|
||||
def curve_lineto(stack: PDFStateStack, x: float, y: float) -> PDFStateStack:
|
||||
stack.curvestacks.append(CurvePath(
|
||||
CurvePath.METHOD.LINE_TO,
|
||||
CurvePoint(x, y)
|
||||
CurvePoint(stack.t_matrix, x, y)
|
||||
))
|
||||
return stack
|
||||
|
||||
# curveto
|
||||
@PDFCommands.add('c')
|
||||
def do_c(stack: PDFStateStack,
|
||||
def curve_curveto(stack: PDFStateStack,
|
||||
x1: float, y1: float,
|
||||
x2: float, y2: float,
|
||||
x3: float, y3: float ) -> PDFStateStack:
|
||||
stack.curvestacks.append(CurvePath(
|
||||
CurvePath.METHOD.CURVE_BOTH_TO,
|
||||
CurvePoint(x1, y1),
|
||||
CurvePoint(x2, y2),
|
||||
CurvePoint(x3, y3),
|
||||
CurvePoint(stack.t_matrix, x1, y1),
|
||||
CurvePoint(stack.t_matrix, x2, y2),
|
||||
CurvePoint(stack.t_matrix, x3, y3),
|
||||
))
|
||||
return stack
|
||||
|
||||
# urveto
|
||||
@PDFCommands.add('v')
|
||||
def do_v(stack: PDFStateStack,
|
||||
def curve_urveto(stack: PDFStateStack,
|
||||
x2: float, y2: float,
|
||||
x3: float, y3: float) -> PDFStateStack:
|
||||
stack.curvestacks.append(CurvePath(
|
||||
CurvePath.METHOD.CURVE_NEXT_TO,
|
||||
CurvePoint(x2, y2),
|
||||
CurvePoint(x3, y3),
|
||||
CurvePoint(stack.t_matrix, x2, y2),
|
||||
CurvePoint(stack.t_matrix, x3, y3),
|
||||
))
|
||||
return stack
|
||||
|
||||
# rveto
|
||||
@PDFCommands.add('y')
|
||||
def do_y(stack: PDFStateStack,
|
||||
def curve_rveto(stack: PDFStateStack,
|
||||
x1: float, y1: float,
|
||||
x3: float, y3: float) -> PDFStateStack:
|
||||
stack.curvestacks.append(CurvePath(
|
||||
CurvePath.METHOD.CURVE_FIRST_TO,
|
||||
CurvePoint(x1, y1),
|
||||
CurvePoint(x3, y3)
|
||||
CurvePoint(stack.t_matrix, x1, y1),
|
||||
CurvePoint(stack.t_matrix, x3, y3)
|
||||
))
|
||||
return stack
|
||||
|
||||
# closepath
|
||||
@PDFCommands.add('h')
|
||||
def do_h(stack: PDFStateStack) -> PDFStateStack:
|
||||
def curve_close(stack: PDFStateStack) -> PDFStateStack:
|
||||
stack.curvestacks.append(CurvePath(CurvePath.METHOD.CLOSE_PATH))
|
||||
return stack
|
||||
|
||||
# rectangle
|
||||
@PDFCommands.add('re')
|
||||
def do_re(stack: PDFStateStack, x, y, w, h) -> PDFStateStack:
|
||||
def curve_rect(stack: PDFStateStack, x, y, w, h) -> PDFStateStack:
|
||||
stack.curvestacks.extend([
|
||||
CurvePath(CurvePath.METHOD.MOVE_TO, CurvePoint(x, y) ),
|
||||
CurvePath(CurvePath.METHOD.LINE_TO, CurvePoint(x+w, y) ),
|
||||
CurvePath(CurvePath.METHOD.LINE_TO, CurvePoint(x+w, y+h) ),
|
||||
CurvePath(CurvePath.METHOD.LINE_TO, CurvePoint(x, y+h) ),
|
||||
CurvePath(CurvePath.METHOD.MOVE_TO, CurvePoint(stack.t_matrix, x, y) ),
|
||||
CurvePath(CurvePath.METHOD.LINE_TO, CurvePoint(stack.t_matrix, x+w, y) ),
|
||||
CurvePath(CurvePath.METHOD.LINE_TO, CurvePoint(stack.t_matrix, x+w, y+h) ),
|
||||
CurvePath(CurvePath.METHOD.LINE_TO, CurvePoint(stack.t_matrix, x, y+h) ),
|
||||
CurvePath(CurvePath.METHOD.CLOSE_PATH)
|
||||
])
|
||||
return stack
|
||||
|
||||
# stroke, fill
|
||||
@PDFCommands.add('S', 'f', 'F', 'B')
|
||||
def do_complete_path(stack: PDFStateStack) -> PDFStateStack:
|
||||
def curve_complete_path(stack: PDFStateStack) -> PDFStateStack:
|
||||
stack.complete_layout_items.append(make_curve(
|
||||
stack.t_matrix,
|
||||
stack.graphics.copy(),
|
||||
@@ -199,7 +199,7 @@ def do_complete_path(stack: PDFStateStack) -> PDFStateStack:
|
||||
|
||||
# sroke, fill-even-odd
|
||||
@PDFCommands.add('f_a', 'B_a')
|
||||
def do_complete_path_evenodd(stack: PDFStateStack) -> PDFStateStack:
|
||||
def curve_complete_path_evenodd(stack: PDFStateStack) -> PDFStateStack:
|
||||
stack.complete_layout_items.append(make_curve(
|
||||
stack.t_matrix,
|
||||
stack.graphics.copy(),
|
||||
@@ -211,7 +211,7 @@ def do_complete_path_evenodd(stack: PDFStateStack) -> PDFStateStack:
|
||||
|
||||
# close-and-stroke (do_h, do_S)
|
||||
@PDFCommands.add('s', 'b')
|
||||
def do_close_complete(stack: PDFStateStack) -> PDFStateStack:
|
||||
def curve_close_complete(stack: PDFStateStack) -> PDFStateStack:
|
||||
stack.curvestacks.append(CurvePath(
|
||||
CurvePath.METHOD.CLOSE_PATH
|
||||
))
|
||||
@@ -226,7 +226,7 @@ def do_close_complete(stack: PDFStateStack) -> PDFStateStack:
|
||||
|
||||
# close-fill-and-stroke-even-odd
|
||||
@PDFCommands.add('b_a')
|
||||
def do_close_complete_evenodd(stack: PDFStateStack) -> PDFStateStack:
|
||||
def curve_close_complete_evenodd(stack: PDFStateStack) -> PDFStateStack:
|
||||
stack.curvestacks.append(CurvePath(
|
||||
CurvePath.METHOD.CLOSE_PATH
|
||||
))
|
||||
@@ -241,14 +241,14 @@ def do_close_complete_evenodd(stack: PDFStateStack) -> PDFStateStack:
|
||||
|
||||
# close-only (empty path)
|
||||
@PDFCommands.add('n')
|
||||
def do_n(stack: PDFStateStack) -> PDFStateStack:
|
||||
def curve_close_only(stack: PDFStateStack) -> PDFStateStack:
|
||||
stack.curvestacks = []
|
||||
return stack
|
||||
|
||||
|
||||
# setcolorspace-stroking
|
||||
@PDFCommands.add('CS')
|
||||
def do_CS(stack: PDFStateStack, name: str) -> PDFStateStack:
|
||||
def set_stroke_colorspace(stack: PDFStateStack, name: str) -> PDFStateStack:
|
||||
try:
|
||||
stack.graphics.scolspace = stack.colorspace_map[literal_name(name)]
|
||||
return stack
|
||||
@@ -257,7 +257,7 @@ def do_CS(stack: PDFStateStack, name: str) -> PDFStateStack:
|
||||
|
||||
# setcolorspace-non-strokine
|
||||
@PDFCommands.add('cs')
|
||||
def do_cs(stack: PDFStateStack, name: str) -> PDFStateStack:
|
||||
def set_nonstroke_colorspace(stack: PDFStateStack, name: str) -> PDFStateStack:
|
||||
try:
|
||||
stack.graphics.ncolspace = stack.colorspace_map[literal_name(name)]
|
||||
return stack
|
||||
@@ -266,7 +266,7 @@ def do_cs(stack: PDFStateStack, name: str) -> PDFStateStack:
|
||||
|
||||
# setgray-stroking
|
||||
@PDFCommands.add('G')
|
||||
def do_G(stack: PDFStateStack, gray: float) -> PDFStateStack:
|
||||
def set_stroke_gray(stack: PDFStateStack, gray: float) -> PDFStateStack:
|
||||
stack.graphics.set_stroke_color(
|
||||
stack.colorspace_map['DeviceGray'],
|
||||
gray
|
||||
@@ -275,7 +275,7 @@ def do_G(stack: PDFStateStack, gray: float) -> PDFStateStack:
|
||||
|
||||
# setgray-non-stroking
|
||||
@PDFCommands.add('g')
|
||||
def do_g(stack: PDFStateStack, gray: float) -> PDFStateStack:
|
||||
def set_nonstroke_gray(stack: PDFStateStack, gray: float) -> PDFStateStack:
|
||||
stack.graphics.set_nostroke_color(
|
||||
stack.colorspace_map['DeviceGray'],
|
||||
gray
|
||||
@@ -285,7 +285,7 @@ def do_g(stack: PDFStateStack, gray: float) -> PDFStateStack:
|
||||
|
||||
# setrgb-stroking
|
||||
@PDFCommands.add('RG')
|
||||
def do_RG(stack: PDFStateStack, r, g, b) -> PDFStateStack:
|
||||
def set_stroke_rgb(stack: PDFStateStack, r, g, b) -> PDFStateStack:
|
||||
stack.graphics.set_stroke_color(
|
||||
stack.colorspace_map['DeviceRGB'],
|
||||
round(r*255),
|
||||
@@ -296,7 +296,7 @@ def do_RG(stack: PDFStateStack, r, g, b) -> PDFStateStack:
|
||||
|
||||
# setrgb-non-stroking
|
||||
@PDFCommands.add('rg')
|
||||
def do_rg(stack: PDFStateStack, r, g, b) -> PDFStateStack:
|
||||
def set_nonstroke_rgb(stack: PDFStateStack, r, g, b) -> PDFStateStack:
|
||||
stack.graphics.set_nostroke_color(
|
||||
stack.colorspace_map['DeviceRGB'],
|
||||
round(r*255),
|
||||
@@ -307,7 +307,7 @@ def do_rg(stack: PDFStateStack, r, g, b) -> PDFStateStack:
|
||||
|
||||
# setcmyk-stroking
|
||||
@PDFCommands.add('K')
|
||||
def do_K(stack: PDFStateStack, c, m, y, k) -> PDFStateStack:
|
||||
def set_stroke_cmyk(stack: PDFStateStack, c, m, y, k) -> PDFStateStack:
|
||||
stack.graphics.set_stroke_color(
|
||||
stack.colorspace_map['DeviceCMYK'],
|
||||
c, m, y, k
|
||||
@@ -316,7 +316,7 @@ def do_K(stack: PDFStateStack, c, m, y, k) -> PDFStateStack:
|
||||
|
||||
# setcmyk-non-stroking
|
||||
@PDFCommands.add('k')
|
||||
def do_k(stack: PDFStateStack, c, m, y, k) -> PDFStateStack:
|
||||
def set_nonstroke_cmyk(stack: PDFStateStack, c, m, y, k) -> PDFStateStack:
|
||||
stack.graphics.set_stroke_color(
|
||||
stack.colorspace_map['DeviceCMYK'],
|
||||
c, m, y, k
|
||||
@@ -324,7 +324,7 @@ def do_k(stack: PDFStateStack, c, m, y, k) -> PDFStateStack:
|
||||
return stack
|
||||
|
||||
@PDFCommands.add('SCN','SC')
|
||||
def do_SCN(stack: PDFStateStack) -> PDFStateStack:
|
||||
def set_stroke_colorspace_custom(stack: PDFStateStack) -> PDFStateStack:
|
||||
if stack.graphics.scolspace is None:
|
||||
raise PDFCommands.InvalidOperation('No colorspace specified!')
|
||||
components = stack.pop(stack.graphics.scolspace.ncomponents)
|
||||
@@ -335,7 +335,7 @@ def do_SCN(stack: PDFStateStack) -> PDFStateStack:
|
||||
return stack
|
||||
|
||||
@PDFCommands.add('scn','sc')
|
||||
def do_scn(stack: PDFStateStack) -> PDFStateStack:
|
||||
def set_nonstroke_colorspace_custom(stack: PDFStateStack) -> PDFStateStack:
|
||||
if stack.graphics.ncolspace is None:
|
||||
raise PDFCommands.InvalidOperation('No colorspace specified!')
|
||||
components = stack.pop(stack.graphics.ncolspace.ncomponents)
|
||||
@@ -347,7 +347,7 @@ def do_scn(stack: PDFStateStack) -> PDFStateStack:
|
||||
|
||||
# begin-text
|
||||
@PDFCommands.add('BT')
|
||||
def do_BT(stack: PDFStateStack) -> PDFStateStack:
|
||||
def begin_text(stack: PDFStateStack) -> PDFStateStack:
|
||||
stack.text.matrix = MATRIX_IDENTITY
|
||||
stack.text.linematrix = [0, 0]
|
||||
if stack.current_textblock is None:
|
||||
@@ -357,38 +357,38 @@ def do_BT(stack: PDFStateStack) -> PDFStateStack:
|
||||
return stack
|
||||
|
||||
@PDFCommands.add('ET')
|
||||
def do_ET(stack: PDFStateStack) -> PDFStateStack:
|
||||
def end_text(stack: PDFStateStack) -> PDFStateStack:
|
||||
stack.complete_layout_items.append(stack.current_textblock)
|
||||
stack.current_textblock = None
|
||||
return stack
|
||||
|
||||
# setcharspace
|
||||
@PDFCommands.add('Tc')
|
||||
def do_Tc(stack: PDFStateStack, charspace) -> PDFStateStack:
|
||||
def set_charspace(stack: PDFStateStack, charspace) -> PDFStateStack:
|
||||
stack.text.charspace = charspace
|
||||
return stack
|
||||
|
||||
# setwordspace
|
||||
@PDFCommands.add('Tw')
|
||||
def do_Tw(stack: PDFStateStack, wordspace) -> PDFStateStack:
|
||||
def set_wordspace(stack: PDFStateStack, wordspace) -> PDFStateStack:
|
||||
stack.text.wordspace = wordspace
|
||||
return stack
|
||||
|
||||
# textscale
|
||||
@PDFCommands.add('Tz')
|
||||
def do_Tz(stack: PDFStateStack, scaling) -> PDFStateStack:
|
||||
def set_textscale(stack: PDFStateStack, scaling) -> PDFStateStack:
|
||||
stack.text.scaling = scaling
|
||||
return stack
|
||||
|
||||
# setleading
|
||||
@PDFCommands.add('TL')
|
||||
def do_TL(stack: PDFStateStack, leading) -> PDFStateStack:
|
||||
def set_text_leading(stack: PDFStateStack, leading) -> PDFStateStack:
|
||||
stack.text.leading = -leading
|
||||
return stack
|
||||
|
||||
# selectfont
|
||||
@PDFCommands.add('Tf')
|
||||
def do_Tf(stack: PDFStateStack, fontid, fontsize) -> PDFStateStack:
|
||||
def set_font(stack: PDFStateStack, fontid, fontsize) -> PDFStateStack:
|
||||
stack.text.fontsize = fontsize
|
||||
try:
|
||||
stack.text.font = stack.fontmap[literal_name(fontid)]
|
||||
@@ -398,19 +398,19 @@ def do_Tf(stack: PDFStateStack, fontid, fontsize) -> PDFStateStack:
|
||||
|
||||
# setrendering
|
||||
@PDFCommands.add('Tr')
|
||||
def do_Tr(stack: PDFStateStack, render) -> PDFStateStack:
|
||||
def set_text_rendering(stack: PDFStateStack, render) -> PDFStateStack:
|
||||
stack.text.render = render
|
||||
return stack
|
||||
|
||||
# settextrise
|
||||
@PDFCommands.add('Ts')
|
||||
def do_Ts(stack: PDFStateStack, rise) -> PDFStateStack:
|
||||
def set_textrise(stack: PDFStateStack, rise) -> PDFStateStack:
|
||||
stack.text.rise = rise
|
||||
return stack
|
||||
|
||||
# text-move
|
||||
@PDFCommands.add('Td')
|
||||
def do_Td(stack: PDFStateStack, tx, ty) -> PDFStateStack:
|
||||
def move_text_x(stack: PDFStateStack, tx, ty) -> PDFStateStack:
|
||||
(a, b, c, d, e, f) = stack.text.matrix
|
||||
stack.text.matrix = (a, b, c, d, tx*a+ty*c+e, tx*b+ty*d+f)
|
||||
stack.text.linematrix = [0, 0]
|
||||
@@ -418,7 +418,7 @@ def do_Td(stack: PDFStateStack, tx, ty) -> PDFStateStack:
|
||||
|
||||
# text-move
|
||||
@PDFCommands.add('TD')
|
||||
def do_TD(stack: PDFStateStack, tx, ty) -> PDFStateStack:
|
||||
def move_text_y(stack: PDFStateStack, tx, ty) -> PDFStateStack:
|
||||
(a, b, c, d, e, f) = stack.text.matrix
|
||||
stack.text.matrix = (a, b, c, d, tx*a+ty*c+e, tx*b+ty*d+f)
|
||||
stack.text.leading = ty
|
||||
@@ -427,14 +427,14 @@ def do_TD(stack: PDFStateStack, tx, ty) -> PDFStateStack:
|
||||
|
||||
# textmatrix
|
||||
@PDFCommands.add('Tm')
|
||||
def do_Tm(stack: PDFStateStack, a, b, c, d, e, f) -> PDFStateStack:
|
||||
def set_text_matrix(stack: PDFStateStack, a, b, c, d, e, f) -> PDFStateStack:
|
||||
stack.text.matrix = (a, b, c, d, e, f)
|
||||
stack.text.linematrix = [0, 0]
|
||||
return stack
|
||||
|
||||
# nextline
|
||||
@PDFCommands.add('T_a')
|
||||
def do_T_a(stack: PDFStateStack) -> PDFStateStack:
|
||||
def nextline(stack: PDFStateStack) -> PDFStateStack:
|
||||
(a, b, c, d, e, f) = stack.text.matrix
|
||||
stack.text.matrix = (a, b, c, d, stack.text.leading*c+e, stack.text.leading*d+f)
|
||||
stack.text.linematrix = [0, 0]
|
||||
@@ -442,7 +442,7 @@ def do_T_a(stack: PDFStateStack) -> PDFStateStack:
|
||||
|
||||
# show-pos
|
||||
@PDFCommands.add('TJ')
|
||||
def do_TJ(stack: PDFStateStack, seq: bytearray) -> PDFStateStack:
|
||||
def add_char_blocks(stack: PDFStateStack, seq: bytearray) -> PDFStateStack:
|
||||
if stack.text.font is None:
|
||||
raise PDFCommands.InvalidOperation("No Font Specified")
|
||||
if stack.current_textblock is None:
|
||||
@@ -457,32 +457,32 @@ def do_TJ(stack: PDFStateStack, seq: bytearray) -> PDFStateStack:
|
||||
|
||||
# show
|
||||
@PDFCommands.add('Tj')
|
||||
def do_Tj(stack: PDFStateStack, b: bytes) -> PDFStateStack:
|
||||
return do_TJ(stack, [b])
|
||||
def add_char_block(stack: PDFStateStack, b: bytes) -> PDFStateStack:
|
||||
return add_char_blocks(stack, [b])
|
||||
|
||||
# quote
|
||||
@PDFCommands.add('_q')
|
||||
def do__q(stack: PDFStateStack, b: bytes) -> PDFStateStack:
|
||||
return do_TJ(do_T_a(stack), [b])
|
||||
def add_quoted_char_blocks(stack: PDFStateStack, b: bytes) -> PDFStateStack:
|
||||
return add_char_blocks(nextline(stack), [b])
|
||||
|
||||
# doublequote
|
||||
@PDFCommands.add('_w')
|
||||
def do__w(stack: PDFStateStack, wordspace, charspace, b) -> PDFStateStack:
|
||||
def add_dbquoted_char_blocks(stack: PDFStateStack, wordspace, charspace, b) -> PDFStateStack:
|
||||
stack.text.wordspace = wordspace
|
||||
stack.text.charspace = charspace
|
||||
return do_TJ(stack, [b])
|
||||
return add_char_blocks(stack, [b])
|
||||
|
||||
@PDFCommands.add('EI')
|
||||
def do_EI(stack: PDFStateStack, obj) -> PDFStateStack:
|
||||
def insert_image(stack: PDFStateStack, obj) -> PDFStateStack:
|
||||
if 'W' in obj and 'H' in obj:
|
||||
stack.complete_layout_items.append(make_image(
|
||||
obj, stack.ctm
|
||||
obj, stack.t_matrix
|
||||
))
|
||||
return stack
|
||||
|
||||
# invoke an XObject
|
||||
@PDFCommands.add('Do')
|
||||
def do_Do(stack: PDFStateStack, xobjid) -> PDFStateStack:
|
||||
def insert_xobject(stack: PDFStateStack, xobjid) -> PDFStateStack:
|
||||
xobjid = literal_name(xobjid)
|
||||
try:
|
||||
xobj = PDFStream.validated_stream(stack.xobjmap[xobjid])
|
||||
|
||||
@@ -1,8 +1,14 @@
|
||||
from enum import Enum
|
||||
from typing import List
|
||||
from pdfmajor.utils import apply_matrix_pt
|
||||
|
||||
class CurvePoint:
|
||||
def __init__(self, x: float, y: float):
|
||||
def __init__(self, ctm: tuple, x: float, y: float):
|
||||
# old = (x,y)
|
||||
# (x, y) = apply_matrix_pt(ctm, (x, y))
|
||||
# if old != (x,y):
|
||||
# print("CurvePoint", ctm, old)
|
||||
# print(" >", x, y)
|
||||
self.x = x
|
||||
self.y = y
|
||||
|
||||
|
||||
@@ -13,4 +13,6 @@ class PDFColor:
|
||||
)
|
||||
|
||||
def __repr__(self):
|
||||
return f"<PDFColor space={self.color_space} values={self.values}/>"
|
||||
return f"""<PDFColor space="{
|
||||
None if self.color_space is None else self.color_space.name
|
||||
}" values={self.values}/>"""
|
||||
@@ -24,6 +24,14 @@ class LTCurve(LTComponent):
|
||||
self.stroke = stroke
|
||||
self.fill = fill
|
||||
|
||||
def __repr__(self):
|
||||
return ('<%s [%s] fill=%s stroke=%s>' % (
|
||||
self.__class__.__name__,
|
||||
str(self.bbox),
|
||||
self.fill,
|
||||
self.stroke
|
||||
))
|
||||
|
||||
## LTLine
|
||||
##
|
||||
class LTLine(LTCurve):
|
||||
|
||||
@@ -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):
|
||||
|
||||
@@ -13,6 +13,12 @@ class Bbox:
|
||||
self.x1 = x1
|
||||
self.y0 = y0
|
||||
self.y1 = y1
|
||||
if (x0 is not None) and (x1 is not None):
|
||||
self.x0 = min([x0,x1])
|
||||
self.x1 = max([x0,x1])
|
||||
if (y0 is not None) and (y1 is not None):
|
||||
self.y0 = min([y0,y1])
|
||||
self.y1 = max([y0,y1])
|
||||
|
||||
@property
|
||||
def height(self) -> float:
|
||||
|
||||
@@ -10,7 +10,7 @@ import sys
|
||||
with open('README.md') as readme_file:
|
||||
SETUP = {
|
||||
"name": "pdfmajor",
|
||||
"version": "1.3.2c",
|
||||
"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.
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()
|
||||
+24
-26
@@ -22,30 +22,28 @@ if not os.path.exists(OUTPUT_FOLDER):
|
||||
files = os.listdir(INPUT_FOLDER)
|
||||
|
||||
TOTAL = len(files)*len(EXTS)
|
||||
waiter = tqdm(total=TOTAL)
|
||||
for idx, file_name in enumerate(files):
|
||||
file_name_no_ext = re.sub(r'\.pdf$',"", file_name)
|
||||
input_file_path = os.path.join(INPUT_FOLDER, file_name)
|
||||
with tqdm(total=TOTAL) as waiter:
|
||||
for idx, file_name in enumerate(files):
|
||||
file_name_no_ext = re.sub(r'\.pdf$',"", file_name)
|
||||
input_file_path = os.path.join(INPUT_FOLDER, file_name)
|
||||
|
||||
for ext in EXTS:
|
||||
if not os.path.exists(os.path.join(OUTPUT_FOLDER, ext)):
|
||||
os.makedirs(os.path.join(OUTPUT_FOLDER, ext))
|
||||
output_file_path = os.path.join(OUTPUT_FOLDER, ext, file_name_no_ext + "." + ext)
|
||||
if len(file_name) > len(file_name_no_ext):
|
||||
waiter.set_description(f"Processing {file_name}")
|
||||
convert_file(
|
||||
input_file=input_file_path,
|
||||
output_file=output_file_path,
|
||||
out_type=ext,
|
||||
debug_level=logging.WARN,
|
||||
)
|
||||
if ext == 'xml':
|
||||
waiter.set_description(f" > testing format {file_name}")
|
||||
ElementTree.parse(output_file_path)
|
||||
elif ext == 'json':
|
||||
waiter.set_description(f" > testing format {file_name}")
|
||||
json_load(open(output_file_path, 'r'))
|
||||
waiter.refresh()
|
||||
waiter.update()
|
||||
waiter.set_description("Done")
|
||||
waiter.close()
|
||||
for ext in EXTS:
|
||||
if not os.path.exists(os.path.join(OUTPUT_FOLDER, ext)):
|
||||
os.makedirs(os.path.join(OUTPUT_FOLDER, ext))
|
||||
output_file_path = os.path.join(OUTPUT_FOLDER, ext, file_name_no_ext + "." + ext)
|
||||
if len(file_name) > len(file_name_no_ext):
|
||||
waiter.set_description(f"Conversion test -- {file_name}")
|
||||
convert_file(
|
||||
input_file=input_file_path,
|
||||
output_file=output_file_path,
|
||||
out_type=ext,
|
||||
debug_level=logging.WARN,
|
||||
)
|
||||
if ext == 'xml':
|
||||
waiter.set_description(f" > testing format {file_name}")
|
||||
ElementTree.parse(output_file_path)
|
||||
elif ext == 'json':
|
||||
waiter.set_description(f" > testing format {file_name}")
|
||||
json_load(open(output_file_path, 'r'))
|
||||
waiter.update()
|
||||
waiter.set_description("Done")
|
||||
@@ -1,4 +1,5 @@
|
||||
import os
|
||||
from tqdm import tqdm
|
||||
|
||||
from pdfmajor.interpreter import LTImage, logging
|
||||
from pdfmajor.interpreter import PageInterpreter, PDFInterpreter
|
||||
@@ -18,16 +19,16 @@ OUTPUT_FOLDER = os.path.join(
|
||||
if not os.path.exists(OUTPUT_FOLDER):
|
||||
os.makedirs(OUTPUT_FOLDER)
|
||||
|
||||
for file_name in FILES:
|
||||
for file_name in tqdm(FILES, desc='Image Writer Test'):
|
||||
for page in PDFInterpreter(os.path.join(
|
||||
CUR_PATH,
|
||||
f"samples/pdf/{file_name}.pdf"
|
||||
f"tests/samples/pdf/{file_name}.pdf"
|
||||
), debug_level=logging.ERROR):
|
||||
assert isinstance(page, PageInterpreter)
|
||||
for item_idx, item in enumerate(page):
|
||||
assert isinstance(item, LTItem)
|
||||
if isinstance(item, LTImage):
|
||||
print(item.save_image(
|
||||
item.save_image(
|
||||
OUTPUT_FOLDER,
|
||||
file_name=f'{file_name}_p{page.page_num}i{item_idx}'
|
||||
))
|
||||
)
|
||||
+13
-12
@@ -1,21 +1,22 @@
|
||||
import os
|
||||
|
||||
# from pdfmajor.extractor import extract_items_from_pdf
|
||||
from tqdm import tqdm
|
||||
from pdfmajor.interpreter import LTCharBlock, PDFInterpreter, logging
|
||||
from pdfmajor.interpreter import PageInterpreter
|
||||
from pdfmajor.interpreter.commands import LTItem
|
||||
from pdfmajor.interpreter.commands import LTCharBlock, LTChar
|
||||
|
||||
CUR_PATH = os.path.dirname(os.path.dirname(__file__))
|
||||
FILE_NAME = os.path.join(
|
||||
CUR_PATH,
|
||||
"samples/pdf/tables.pdf"
|
||||
INPUT_FOLDER = os.path.join(
|
||||
CUR_PATH, "tests/samples/pdf"
|
||||
)
|
||||
FILES = os.listdir(INPUT_FOLDER)
|
||||
|
||||
for page in PDFInterpreter(FILE_NAME, debug_level=logging.INFO):
|
||||
assert isinstance(page, PageInterpreter)
|
||||
for item in page:
|
||||
assert isinstance(item, LTItem)
|
||||
if isinstance(item, LTCharBlock):
|
||||
for char in item:
|
||||
assert(char, LTChar)
|
||||
for file_name in tqdm(FILES, desc="Interpreter Test"):
|
||||
file_path = os.path.join(INPUT_FOLDER, file_name)
|
||||
for page in PDFInterpreter(file_path, debug_level=logging.ERROR):
|
||||
assert isinstance(page, PageInterpreter)
|
||||
for item in page:
|
||||
assert isinstance(item, LTItem)
|
||||
if isinstance(item, LTCharBlock):
|
||||
for char in item:
|
||||
assert isinstance(char, LTChar)
|
||||
@@ -80,7 +80,6 @@ with JSONMaker(FILE_NAME) as obj:
|
||||
item.number("x1", 4.2)
|
||||
item.number("y0", 231.12)
|
||||
item.number("y1", 331.12)
|
||||
print(open(FILE_NAME, 'r').read())
|
||||
assert(open(FILE_NAME, 'r').read() == """{
|
||||
"pages": [
|
||||
{
|
||||
|
||||
@@ -0,0 +1,101 @@
|
||||
from os import remove
|
||||
from tempfile import mktemp
|
||||
from pdfmajor.converters.writers.yaml import YAMLMaker
|
||||
|
||||
# Make temp file
|
||||
FILE_NAME = mktemp()
|
||||
|
||||
|
||||
with YAMLMaker(FILE_NAME) as yml:
|
||||
yml.write("name", "bob")
|
||||
yml.write("age", "20")
|
||||
with yml.object("wife") as wife:
|
||||
wife.write("name", "kory")
|
||||
wife.write("age", "21")
|
||||
with yml.array("children") as children:
|
||||
with children.object() as child:
|
||||
child.write("name", "Mike")
|
||||
with child.array("toys") as toys:
|
||||
toys.write("lion")
|
||||
toys.write("bear")
|
||||
with yml.object("computer") as computer:
|
||||
computer.write("type", "awesome")
|
||||
assert(open(FILE_NAME, 'r').read() == """name: bob
|
||||
age: 20
|
||||
wife:
|
||||
name: kory
|
||||
age: 21
|
||||
children:
|
||||
- name: Mike
|
||||
toys:
|
||||
- lion
|
||||
- bear
|
||||
computer:
|
||||
type: awesome""")
|
||||
|
||||
with YAMLMaker(FILE_NAME) as yml:
|
||||
yml.write("name", "bob")
|
||||
yml.write("age", "20")
|
||||
yml.place_object("wife", {
|
||||
"name": "kory",
|
||||
"age": 21
|
||||
})
|
||||
with yml.array("children") as children:
|
||||
with children.object() as child:
|
||||
child.write("name", "Mike")
|
||||
child.place_array("toys", [
|
||||
"lion", "bear"
|
||||
])
|
||||
yml.place_object("computer", {"type": "awesome"})
|
||||
assert(open(FILE_NAME, 'r').read() == """name: bob
|
||||
age: 20
|
||||
wife:
|
||||
name: kory
|
||||
age: 21
|
||||
children:
|
||||
- name: Mike
|
||||
toys:
|
||||
- lion
|
||||
- bear
|
||||
computer:
|
||||
type: awesome""")
|
||||
|
||||
with YAMLMaker(FILE_NAME) as yml:
|
||||
with yml.array('pages') as pages:
|
||||
with pages.object() as page:
|
||||
page.write("id", 0)
|
||||
page.write("width", 100)
|
||||
page.write("height", 200)
|
||||
with page.array("children") as children:
|
||||
with children.object() as curve:
|
||||
with curve.object("stroke") as fill:
|
||||
fill.write("type", "DeviceRGB")
|
||||
fill.write("value", ['1','2','3'])
|
||||
curve.write("x0", 0)
|
||||
curve.write("x1", 1)
|
||||
with curve.object("fill") as fill:
|
||||
fill.write("type", "DeviceRGB")
|
||||
fill.write("value", ['1','2','3'])
|
||||
curve.write("y0", 0)
|
||||
curve.write("y1", 1)
|
||||
|
||||
assert(open(FILE_NAME, 'r').read() == """pages:
|
||||
- id: 0
|
||||
width: 100
|
||||
height: 200
|
||||
children:
|
||||
- stroke:
|
||||
type: DeviceRGB
|
||||
value: ['1', '2', '3']
|
||||
x0: 0
|
||||
x1: 1
|
||||
fill:
|
||||
type: DeviceRGB
|
||||
value: ['1', '2', '3']
|
||||
y0: 0
|
||||
y1: 1""")
|
||||
|
||||
|
||||
|
||||
# Clean up
|
||||
remove(FILE_NAME)
|
||||
+4
-5
@@ -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
|
||||
|
||||
Reference in new issue
Block a user