Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bab104885f | ||
|
|
98f18c3e32 | ||
|
|
ac2c462561 | ||
|
|
19712f1f0f | ||
|
|
ec02a7d38b | ||
|
|
0345ca01ba | ||
|
|
b36f6bc907 | ||
|
|
f9f9a59793 | ||
|
|
b7026793d7 | ||
|
|
addd59ed0f | ||
|
|
d43b617309 | ||
|
|
17b25c7d9b | ||
|
|
b7fab64801 |
No files matched your search
@@ -19,7 +19,7 @@ See documentation [here](https://asosnovsky.github.io/pdfmajor/).
|
|||||||
|
|
||||||
## Basic Example
|
## Basic Example
|
||||||
```py
|
```py
|
||||||
from pdfmajor.interpreter import PageInterpreter
|
from pdfmajor.interpreter import PDFInterpreter
|
||||||
|
|
||||||
for page in PDFInterpreter("/path/to/pdf.pdf"):
|
for page in PDFInterpreter("/path/to/pdf.pdf"):
|
||||||
print("page start", page.page_num)
|
print("page start", page.page_num)
|
||||||
|
|||||||
+1
-1
@@ -16,7 +16,7 @@ a interpreter of the pdf-standard commands
|
|||||||
|
|
||||||
### Example
|
### Example
|
||||||
```py
|
```py
|
||||||
from pdfmajor.interpreter import PageInterpreter
|
from pdfmajor.interpreter import PDFInterpreter
|
||||||
|
|
||||||
for page in PDFInterpreter("/path/to/pdf.pdf"):
|
for page in PDFInterpreter("/path/to/pdf.pdf"):
|
||||||
print("page start", page.page_num)
|
print("page start", page.page_num)
|
||||||
|
|||||||
+1
-1
@@ -19,7 +19,7 @@ See documentation [here](https://asosnovsky.github.io/pdfmajor/).
|
|||||||
|
|
||||||
## Basic Example
|
## Basic Example
|
||||||
```py
|
```py
|
||||||
from pdfmajor.interpreter import PageInterpreter
|
from pdfmajor.interpreter import PDFInterpreter
|
||||||
|
|
||||||
for page in PDFInterpreter("/path/to/pdf.pdf"):
|
for page in PDFInterpreter("/path/to/pdf.pdf"):
|
||||||
print("page start", page.page_num)
|
print("page start", page.page_num)
|
||||||
|
|||||||
+1
-1
@@ -121,7 +121,7 @@
|
|||||||
<h2 id="interpreter">interpreter</h2>
|
<h2 id="interpreter">interpreter</h2>
|
||||||
<p>a interpreter of the pdf-standard commands</p>
|
<p>a interpreter of the pdf-standard commands</p>
|
||||||
<h3 id="example">Example</h3>
|
<h3 id="example">Example</h3>
|
||||||
<pre><code class="py">from pdfmajor.interpreter import PageInterpreter
|
<pre><code class="py">from pdfmajor.interpreter import PDFInterpreter
|
||||||
|
|
||||||
for page in PDFInterpreter("/path/to/pdf.pdf"):
|
for page in PDFInterpreter("/path/to/pdf.pdf"):
|
||||||
print("page start", page.page_num)
|
print("page start", page.page_num)
|
||||||
|
|||||||
+2
-2
@@ -128,7 +128,7 @@
|
|||||||
<h2 id="documentation">Documentation</h2>
|
<h2 id="documentation">Documentation</h2>
|
||||||
<p>See documentation <a href="https://asosnovsky.github.io/pdfmajor/">here</a>.</p>
|
<p>See documentation <a href="https://asosnovsky.github.io/pdfmajor/">here</a>.</p>
|
||||||
<h2 id="basic-example">Basic Example</h2>
|
<h2 id="basic-example">Basic Example</h2>
|
||||||
<pre><code class="py">from pdfmajor.interpreter import PageInterpreter
|
<pre><code class="py">from pdfmajor.interpreter import PDFInterpreter
|
||||||
|
|
||||||
for page in PDFInterpreter("/path/to/pdf.pdf"):
|
for page in PDFInterpreter("/path/to/pdf.pdf"):
|
||||||
print("page start", page.page_num)
|
print("page start", page.page_num)
|
||||||
@@ -240,5 +240,5 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.</p>
|
|||||||
|
|
||||||
<!--
|
<!--
|
||||||
MkDocs version : 1.0.4
|
MkDocs version : 1.0.4
|
||||||
Build Date UTC : 2018-12-24 06:52:28
|
Build Date UTC : 2019-01-11 20:22:59
|
||||||
-->
|
-->
|
||||||
@@ -1 +1 @@
|
|||||||
{"config":{"lang":["en"],"prebuild_index":false,"separator":"[\\s\\-]+"},"docs":[{"location":"","text":"PDFMajor Latest Release PDF-Major is a fork of PDFMiner.six . It is meant to be a more light-weight implementation that makes fewer assumptions on the data. The ultimate goal of this project is to provide a simpler, faster and more functional library that both casual and low-level developers could build upon. Documentation See documentation here . Basic Example from pdfmajor.interpreter import PageInterpreter for page in PDFInterpreter(\"/path/to/pdf.pdf\"): print(\"page start\", page.page_num) for item in page: print(\" >\", item) print(\"page end\", page.page_num) Why Another Fork? PDFMiner was designed to run in python 2.7>, PDFMiner.six was written to bring cross-version support for python 2 and 3. However, after attempting to build and extend upon PDFMiner.six I found it rather difficult to extend and identify additional details regarding the items encoded in the pdf. This library will attempt to expose as much information as possible to end-users, without having them solely rely on to-xml or to-json exports. Features Functional extraction method based on generators Parse, analyze, and convert PDF documents. PDF-1.7 specification support. (well, almost) Font-Color extraction Shape fill and stroke color extraction CJK languages and vertical writing scripts support. Various font types (Type1, TrueType, Type3, and CID) support. Basic encryption (RC4) support. Note : We took out the layout-analysis process in this version (there is no more LTTextHorizontal or LTTextVertical). While the mathematics behind the grouping process was sound, the coupling of the layout-analysis process with the parsing and interpretation process produced unfriendly-code. This feature could be brought back by running your own implementation of it on the pdfmajor.interpreter.PageInterpreter class, but at the current time is not supported. How to Install Source Install Python 3.6.4 or newer. clone this repo git clone https://github.com/asosnovsky/pdfmajor * install repo python setup.py install Pypi Install Python 3.6.4 or newer. install repo pip install pdfmajor Terms and Conditions (This is so-called MIT/X License) Copyright (c) 2018-2019 Ariel Sosnovsky Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.","title":"Home"},{"location":"#pdfmajor","text":"Latest Release PDF-Major is a fork of PDFMiner.six . It is meant to be a more light-weight implementation that makes fewer assumptions on the data. The ultimate goal of this project is to provide a simpler, faster and more functional library that both casual and low-level developers could build upon.","title":"PDFMajor"},{"location":"#documentation","text":"See documentation here .","title":"Documentation"},{"location":"#basic-example","text":"from pdfmajor.interpreter import PageInterpreter for page in PDFInterpreter(\"/path/to/pdf.pdf\"): print(\"page start\", page.page_num) for item in page: print(\" >\", item) print(\"page end\", page.page_num)","title":"Basic Example"},{"location":"#why-another-fork","text":"PDFMiner was designed to run in python 2.7>, PDFMiner.six was written to bring cross-version support for python 2 and 3. However, after attempting to build and extend upon PDFMiner.six I found it rather difficult to extend and identify additional details regarding the items encoded in the pdf. This library will attempt to expose as much information as possible to end-users, without having them solely rely on to-xml or to-json exports.","title":"Why Another Fork?"},{"location":"#features","text":"Functional extraction method based on generators Parse, analyze, and convert PDF documents. PDF-1.7 specification support. (well, almost) Font-Color extraction Shape fill and stroke color extraction CJK languages and vertical writing scripts support. Various font types (Type1, TrueType, Type3, and CID) support. Basic encryption (RC4) support. Note : We took out the layout-analysis process in this version (there is no more LTTLine truncated
|
{"config":{"lang":["en"],"prebuild_index":false,"separator":"[\\s\\-]+"},"docs":[{"location":"","text":"PDFMajor Latest Release PDF-Major is a fork of PDFMiner.six . It is meant to be a more light-weight implementation that makes fewer assumptions on the data. The ultimate goal of this project is to provide a simpler, faster and more functional library that both casual and low-level developers could build upon. Documentation See documentation here . Basic Example from pdfmajor.interpreter import PDFInterpreter for page in PDFInterpreter(\"/path/to/pdf.pdf\"): print(\"page start\", page.page_num) for item in page: print(\" >\", item) print(\"page end\", page.page_num) Why Another Fork? PDFMiner was designed to run in python 2.7>, PDFMiner.six was written to bring cross-version support for python 2 and 3. However, after attempting to build and extend upon PDFMiner.six I found it rather difficult to extend and identify additional details regarding the items encoded in the pdf. This library will attempt to expose as much information as possible to end-users, without having them solely rely on to-xml or to-json exports. Features Functional extraction method based on generators Parse, analyze, and convert PDF documents. PDF-1.7 specification support. (well, almost) Font-Color extraction Shape fill and stroke color extraction CJK languages and vertical writing scripts support. Various font types (Type1, TrueType, Type3, and CID) support. Basic encryption (RC4) support. Note : We took out the layout-analysis process in this version (there is no more LTTextHorizontal or LTTextVertical). While the mathematics behind the grouping process was sound, the coupling of the layout-analysis process with the parsing and interpretation process produced unfriendly-code. This feature could be brought back by running your own implementation of it on the pdfmajor.interpreter.PageInterpreter class, but at the current time is not supported. How to Install Source Install Python 3.6.4 or newer. clone this repo git clone https://github.com/asosnovsky/pdfmajor * install repo python setup.py install Pypi Install Python 3.6.4 or newer. install repo pip install pdfmajor Terms and Conditions (This is so-called MIT/X License) Copyright (c) 2018-2019 Ariel Sosnovsky Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.","title":"Home"},{"location":"#pdfmajor","text":"Latest Release PDF-Major is a fork of PDFMiner.six . It is meant to be a more light-weight implementation that makes fewer assumptions on the data. The ultimate goal of this project is to provide a simpler, faster and more functional library that both casual and low-level developers could build upon.","title":"PDFMajor"},{"location":"#documentation","text":"See documentation here .","title":"Documentation"},{"location":"#basic-example","text":"from pdfmajor.interpreter import PDFInterpreter for page in PDFInterpreter(\"/path/to/pdf.pdf\"): print(\"page start\", page.page_num) for item in page: print(\" >\", item) print(\"page end\", page.page_num)","title":"Basic Example"},{"location":"#why-another-fork","text":"PDFMiner was designed to run in python 2.7>, PDFMiner.six was written to bring cross-version support for python 2 and 3. However, after attempting to build and extend upon PDFMiner.six I found it rather difficult to extend and identify additional details regarding the items encoded in the pdf. This library will attempt to expose as much information as possible to end-users, without having them solely rely on to-xml or to-json exports.","title":"Why Another Fork?"},{"location":"#features","text":"Functional extraction method based on generators Parse, analyze, and convert PDF documents. PDF-1.7 specification support. (well, almost) Font-Color extraction Shape fill and stroke color extraction CJK languages and vertical writing scripts support. Various font types (Type1, TrueType, Type3, and CID) support. Basic encryption (RC4) support. Note : We took out the layout-analysis process in this version (there is no more LTTexLine truncated
|
||||||
+2
-2
@@ -2,12 +2,12 @@
|
|||||||
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
|
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
|
||||||
<url>
|
<url>
|
||||||
<loc>None</loc>
|
<loc>None</loc>
|
||||||
<lastmod>2018-12-24</lastmod>
|
<lastmod>2019-01-11</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>None</loc>
|
<loc>None</loc>
|
||||||
<lastmod>2018-12-24</lastmod>
|
<lastmod>2019-01-11</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
</urlset>
|
</urlset>
|
||||||
Binary file not shown.
@@ -24,7 +24,7 @@ See documentation [here](https://asosnovsky.github.io/pdfmajor/).
|
|||||||
|
|
||||||
## Basic Example
|
## Basic Example
|
||||||
```py
|
```py
|
||||||
from pdfmajor.interpreter import PageInterpreter
|
from pdfmajor.interpreter import PDFInterpreter
|
||||||
|
|
||||||
for page in PDFInterpreter("/path/to/pdf.pdf"):
|
for page in PDFInterpreter("/path/to/pdf.pdf"):
|
||||||
print("page start", page.page_num)
|
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.
|
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
__version__ = (1, 3, 2)
|
__version__ = (1, 3, 6)
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
print(
|
print(
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ from .html import convert_to_html
|
|||||||
from .xml import convert_to_xml
|
from .xml import convert_to_xml
|
||||||
from .json import convert_to_json
|
from .json import convert_to_json
|
||||||
from .text import convert_to_text
|
from .text import convert_to_text
|
||||||
|
from .yaml import convert_to_yaml
|
||||||
from ..utils import logging
|
from ..utils import logging
|
||||||
|
|
||||||
def convert_file(
|
def convert_file(
|
||||||
@@ -26,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:
|
||||||
@@ -75,6 +76,20 @@ def convert_file(
|
|||||||
dont_export_images=dont_export_images,
|
dont_export_images=dont_export_images,
|
||||||
debug_level=debug_level,
|
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':
|
elif out_type == 'text':
|
||||||
return convert_to_text(
|
return convert_to_text(
|
||||||
input_file_path=input_file,
|
input_file_path=input_file,
|
||||||
@@ -89,4 +104,4 @@ def convert_file(
|
|||||||
dont_export_images=dont_export_images,
|
dont_export_images=dont_export_images,
|
||||||
debug_level=debug_level,
|
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'")
|
||||||
+127
-96
@@ -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,
|
||||||
@@ -43,19 +45,21 @@ def convert_to_html(
|
|||||||
render_page(html, page)
|
render_page(html, page)
|
||||||
|
|
||||||
def render_page(html: HTMLMaker, ltpage: PageInterpreter):
|
def render_page(html: HTMLMaker, ltpage: PageInterpreter):
|
||||||
def render(item: LTItem):
|
def render(item: LTItem, block = True):
|
||||||
if isinstance(item, LTCurve):
|
if isinstance(item, LTCurve):
|
||||||
render_curve(html, item)
|
render_curve(html, ltpage, item)
|
||||||
elif isinstance(item, LTXObject):
|
elif isinstance(item, LTXObject):
|
||||||
with html.elm('div', { "class": 'xobject' }, {
|
with html.elm('div', { "class": 'xobject' }, {
|
||||||
'position': 'absolute',
|
# 'position': 'absolute',
|
||||||
'left': f'{item.x0}px',
|
'left': f'{item.x0}px',
|
||||||
|
'right': f'{item.x1}px',
|
||||||
'bottom': f'{item.y0}px',
|
'bottom': f'{item.y0}px',
|
||||||
|
'top': f'{item.y1}px',
|
||||||
'width': f'{item.width}px',
|
'width': f'{item.width}px',
|
||||||
'height': f'{item.height}px',
|
'height': f'{item.height}px',
|
||||||
}):
|
}):
|
||||||
for child in item:
|
for child in item:
|
||||||
render(child)
|
render(child, False)
|
||||||
elif isinstance(item, LTImage):
|
elif isinstance(item, LTImage):
|
||||||
name = item.name
|
name = item.name
|
||||||
# if html.imagewriter is not None:
|
# if html.imagewriter is not None:
|
||||||
@@ -68,42 +72,42 @@ def render_page(html: HTMLMaker, ltpage: PageInterpreter):
|
|||||||
})
|
})
|
||||||
elif isinstance(item, LTChar):
|
elif isinstance(item, LTChar):
|
||||||
with html.elm('span', { 'class': 'char' }, {
|
with html.elm('span', { 'class': 'char' }, {
|
||||||
'width': item.width,
|
'width': str(item.width) + "px",
|
||||||
'min-width': item.width,
|
'min-width': str(item.width) + "px",
|
||||||
'max-width': item.width,
|
'max-width': str(item.width) + "px",
|
||||||
'height': item.height,
|
'height': str(item.height) + "px",
|
||||||
'min-height': item.height,
|
'min-height': str(item.height) + "px",
|
||||||
'max-height': item.height,
|
'max-height': str(item.height) + "px",
|
||||||
}):
|
}):
|
||||||
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': item.width,
|
'min-width': str(item.width) + "px",
|
||||||
'min-width': item.width,
|
'max-width': str(item.width) + "px",
|
||||||
'max-width': item.width,
|
'height': str(item.height) + "px",
|
||||||
'height': item.height,
|
'min-height': str(item.height) + "px",
|
||||||
'min-height': item.height,
|
'max-height': str(item.height) + "px",
|
||||||
'max-height': item.height,
|
'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',
|
||||||
@@ -125,69 +129,96 @@ def get_color(col: PDFColor, default: str = ""):
|
|||||||
else:
|
else:
|
||||||
return default
|
return default
|
||||||
|
|
||||||
def render_curve(html: HTMLMaker, item: LTCurve):
|
def render_curve(html: HTMLMaker, ltpage: PageInterpreter, item: LTCurve):
|
||||||
css = {
|
path, svg_attr = create_svg_path(item)
|
||||||
'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 = {
|
|
||||||
|
|
||||||
}
|
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
|
with html.elm('svg', svg_attr, css={
|
||||||
p_vals = { "x": [], "y": [] }
|
'border': '1px solid black',
|
||||||
|
'position': 'absolute',
|
||||||
for p in item.paths:
|
'left': f'{item.x0}px',
|
||||||
for pt in p.points:
|
'bottom': f'{ltpage.height - item.y0}px',
|
||||||
p_vals['x'].append(pt.x)
|
'top': f'{ltpage.height - item.y1}px',
|
||||||
p_vals['y'].append(pt.y)
|
'right': f'{item.x1}px',
|
||||||
|
'width': f'{item.width}px',
|
||||||
mx_x = max([*p_vals['x'], 1])
|
'height': f'{item.height}px',
|
||||||
mn_x = min([*p_vals['x'], 0])
|
}):
|
||||||
mx_y = max([*p_vals['y'], 1])
|
html.singleton('path', {
|
||||||
mn_y = min([*p_vals['y'], 0])
|
"d": path,
|
||||||
|
**path_css
|
||||||
if mx_x == mn_x:
|
}, {
|
||||||
mn_x -= 1
|
**path_css
|
||||||
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
|
def create_svg_path(item: LTCurve):
|
||||||
for p in item.paths:
|
path = ""
|
||||||
if p.method == CurvePath.METHOD.MOVE_TO:
|
svg_attr = {
|
||||||
path += f'M{" ".join( f"{round(dx*pt.x, ROUND_VAL)} {round(dy*pt.y, ROUND_VAL)}" for pt in p.points )} '
|
'class': 'curve',
|
||||||
if p.method == CurvePath.METHOD.LINE_TO:
|
'z-index': 2
|
||||||
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)
|
if item.height > 0:
|
||||||
fill_col = get_color(item.fill)
|
dy = 1.0/item.height
|
||||||
if fill_col:
|
dx = (item.width/item.height)/(item.x1-item.x0)
|
||||||
path_css['fill'] = fill_col
|
svg_attr.update({
|
||||||
if stroke_col:
|
"viewBox": "0 0 %s %s" % ( item.width/item.height, 1 )
|
||||||
path_css['stroke'] = stroke_col
|
})
|
||||||
|
else:
|
||||||
|
dx = 1.0/(item.x1-item.x0)
|
||||||
|
dy = 1.0
|
||||||
|
svg_attr.update({
|
||||||
|
"viewBox": "0 0 1 1"
|
||||||
|
})
|
||||||
|
|
||||||
with html.elm('svg', svg_attr, css):
|
if item.height > 1:
|
||||||
html.singleton('path', {
|
svg_attr.update({
|
||||||
"d": path,
|
'height': f'{item.height}px',
|
||||||
**path_css
|
})
|
||||||
}, {
|
else:
|
||||||
**path_css
|
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)
|
self.write(f"<{tag_name} {attrs} />", lineend=lineend, deep_space=deep_space)
|
||||||
yield
|
yield
|
||||||
|
|
||||||
def singleton(self, tag_name: str, attrs: dict = None, css: dict = None):
|
def singleton(self, tag_name: str, attrs: dict = None, css: dict = None, singleton: bool = True):
|
||||||
with self.elm(tag_name, attrs, css, singleton=True):
|
with self.elm(tag_name, attrs, css, singleton=singleton):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@@ -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
|
class CommandProcessorError(Exception): pass
|
||||||
|
|
||||||
def process_command_stream(streams: List[PDFStream], font_cache: dict = None, state: PDFStateStack = None):
|
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:
|
if font_cache is None:
|
||||||
font_cache = {}
|
font_cache = {}
|
||||||
parser = PDFContentParser(streams)
|
parser = PDFContentParser(streams)
|
||||||
@@ -41,14 +39,14 @@ def process_command_stream(streams: List[PDFStream], font_cache: dict = None, st
|
|||||||
else:
|
else:
|
||||||
func(state)
|
func(state)
|
||||||
else:
|
else:
|
||||||
log.warning('Unknown operator: %r (i.e. %r)' % (name, method))
|
log.debug('Unknown operator: %r (i.e. %r)' % (name, method))
|
||||||
else:
|
else:
|
||||||
state.argstack.append(obj)
|
state.argstack.append(obj)
|
||||||
|
|
||||||
for complete_item in state.complete_layout_items:
|
for complete_item in state.complete_layout_items:
|
||||||
if isinstance(complete_item, LTXObject):
|
if isinstance(complete_item, LTXObject):
|
||||||
xobj_state = prep_state(
|
xobj_state = prep_state(
|
||||||
state=PDFGraphicState(),
|
state=PDFStateStack(),
|
||||||
ctm=complete_item.t_matrix,
|
ctm=complete_item.t_matrix,
|
||||||
resources=complete_item.resources,
|
resources=complete_item.resources,
|
||||||
font_cache=font_cache
|
font_cache=font_cache
|
||||||
|
|||||||
@@ -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')
|
||||||
@@ -116,7 +116,7 @@ def do_i(stack: PDFStateStack, flatness) -> PDFStateStack:
|
|||||||
def do_m(stack: PDFStateStack, x: float, y: float) -> PDFStateStack:
|
def do_m(stack: PDFStateStack, x: float, y: float) -> PDFStateStack:
|
||||||
stack.curvestacks.append(CurvePath(
|
stack.curvestacks.append(CurvePath(
|
||||||
CurvePath.METHOD.MOVE_TO,
|
CurvePath.METHOD.MOVE_TO,
|
||||||
CurvePoint(x, y)
|
CurvePoint(stack.t_matrix, x, y)
|
||||||
))
|
))
|
||||||
return stack
|
return stack
|
||||||
|
|
||||||
@@ -125,7 +125,7 @@ def do_m(stack: PDFStateStack, x: float, y: float) -> PDFStateStack:
|
|||||||
def do_l(stack: PDFStateStack, x: float, y: float) -> PDFStateStack:
|
def do_l(stack: PDFStateStack, x: float, y: float) -> PDFStateStack:
|
||||||
stack.curvestacks.append(CurvePath(
|
stack.curvestacks.append(CurvePath(
|
||||||
CurvePath.METHOD.LINE_TO,
|
CurvePath.METHOD.LINE_TO,
|
||||||
CurvePoint(x, y)
|
CurvePoint(stack.t_matrix, x, y)
|
||||||
))
|
))
|
||||||
return stack
|
return stack
|
||||||
|
|
||||||
@@ -137,9 +137,9 @@ def do_c(stack: PDFStateStack,
|
|||||||
x3: float, y3: float ) -> PDFStateStack:
|
x3: float, y3: float ) -> PDFStateStack:
|
||||||
stack.curvestacks.append(CurvePath(
|
stack.curvestacks.append(CurvePath(
|
||||||
CurvePath.METHOD.CURVE_BOTH_TO,
|
CurvePath.METHOD.CURVE_BOTH_TO,
|
||||||
CurvePoint(x1, y1),
|
CurvePoint(stack.t_matrix, x1, y1),
|
||||||
CurvePoint(x2, y2),
|
CurvePoint(stack.t_matrix, x2, y2),
|
||||||
CurvePoint(x3, y3),
|
CurvePoint(stack.t_matrix, x3, y3),
|
||||||
))
|
))
|
||||||
return stack
|
return stack
|
||||||
|
|
||||||
@@ -150,8 +150,8 @@ def do_v(stack: PDFStateStack,
|
|||||||
x3: float, y3: float) -> PDFStateStack:
|
x3: float, y3: float) -> PDFStateStack:
|
||||||
stack.curvestacks.append(CurvePath(
|
stack.curvestacks.append(CurvePath(
|
||||||
CurvePath.METHOD.CURVE_NEXT_TO,
|
CurvePath.METHOD.CURVE_NEXT_TO,
|
||||||
CurvePoint(x2, y2),
|
CurvePoint(stack.t_matrix, x2, y2),
|
||||||
CurvePoint(x3, y3),
|
CurvePoint(stack.t_matrix, x3, y3),
|
||||||
))
|
))
|
||||||
return stack
|
return stack
|
||||||
|
|
||||||
@@ -162,8 +162,8 @@ def do_y(stack: PDFStateStack,
|
|||||||
x3: float, y3: float) -> PDFStateStack:
|
x3: float, y3: float) -> PDFStateStack:
|
||||||
stack.curvestacks.append(CurvePath(
|
stack.curvestacks.append(CurvePath(
|
||||||
CurvePath.METHOD.CURVE_FIRST_TO,
|
CurvePath.METHOD.CURVE_FIRST_TO,
|
||||||
CurvePoint(x1, y1),
|
CurvePoint(stack.t_matrix, x1, y1),
|
||||||
CurvePoint(x3, y3)
|
CurvePoint(stack.t_matrix, x3, y3)
|
||||||
))
|
))
|
||||||
return stack
|
return stack
|
||||||
|
|
||||||
@@ -177,10 +177,10 @@ def do_h(stack: PDFStateStack) -> PDFStateStack:
|
|||||||
@PDFCommands.add('re')
|
@PDFCommands.add('re')
|
||||||
def do_re(stack: PDFStateStack, x, y, w, h) -> PDFStateStack:
|
def do_re(stack: PDFStateStack, x, y, w, h) -> PDFStateStack:
|
||||||
stack.curvestacks.extend([
|
stack.curvestacks.extend([
|
||||||
CurvePath(CurvePath.METHOD.MOVE_TO, CurvePoint(x, y) ),
|
CurvePath(CurvePath.METHOD.MOVE_TO, CurvePoint(stack.t_matrix, x, y) ),
|
||||||
CurvePath(CurvePath.METHOD.LINE_TO, CurvePoint(x+w, y) ),
|
CurvePath(CurvePath.METHOD.LINE_TO, CurvePoint(stack.t_matrix, x+w, y) ),
|
||||||
CurvePath(CurvePath.METHOD.LINE_TO, CurvePoint(x+w, y+h) ),
|
CurvePath(CurvePath.METHOD.LINE_TO, CurvePoint(stack.t_matrix, x+w, y+h) ),
|
||||||
CurvePath(CurvePath.METHOD.LINE_TO, CurvePoint(x, y+h) ),
|
CurvePath(CurvePath.METHOD.LINE_TO, CurvePoint(stack.t_matrix, x, y+h) ),
|
||||||
CurvePath(CurvePath.METHOD.CLOSE_PATH)
|
CurvePath(CurvePath.METHOD.CLOSE_PATH)
|
||||||
])
|
])
|
||||||
return stack
|
return stack
|
||||||
@@ -476,7 +476,7 @@ def do__w(stack: PDFStateStack, wordspace, charspace, b) -> PDFStateStack:
|
|||||||
def do_EI(stack: PDFStateStack, obj) -> PDFStateStack:
|
def do_EI(stack: PDFStateStack, obj) -> PDFStateStack:
|
||||||
if 'W' in obj and 'H' in obj:
|
if 'W' in obj and 'H' in obj:
|
||||||
stack.complete_layout_items.append(make_image(
|
stack.complete_layout_items.append(make_image(
|
||||||
obj, stack.ctm
|
obj, stack.t_matrix
|
||||||
))
|
))
|
||||||
return stack
|
return stack
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,14 @@
|
|||||||
from enum import Enum
|
from enum import Enum
|
||||||
from typing import List
|
from typing import List
|
||||||
|
from pdfmajor.utils import apply_matrix_pt
|
||||||
|
|
||||||
class CurvePoint:
|
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.x = x
|
||||||
self.y = y
|
self.y = y
|
||||||
|
|
||||||
|
|||||||
@@ -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):
|
||||||
|
|||||||
@@ -13,6 +13,12 @@ class Bbox:
|
|||||||
self.x1 = x1
|
self.x1 = x1
|
||||||
self.y0 = y0
|
self.y0 = y0
|
||||||
self.y1 = y1
|
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
|
@property
|
||||||
def height(self) -> float:
|
def height(self) -> float:
|
||||||
|
|||||||
@@ -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.2",
|
"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.
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()
|
||||||
@@ -0,0 +1,90 @@
|
|||||||
|
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")
|
||||||
|
print(open(FILE_NAME, 'r').read())
|
||||||
|
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"})
|
||||||
|
print(open(FILE_NAME, 'r').read())
|
||||||
|
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)
|
||||||
|
|
||||||
|
print(open(FILE_NAME, 'r').read())
|
||||||
|
assert(open(FILE_NAME, 'r').read() == """""")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# Clean up
|
||||||
|
remove(FILE_NAME)
|
||||||
+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