6 Commits
Author SHA1 Message Date
ari 609bcda76a - returned image writer
- added .save_image method to LTImage
2019-01-04 15:43:30 -05:00
ari ef230847ac - image extraction 2019-01-04 15:43:07 -05:00
ari d1d1b23f2f updated docs 2018-12-24 01:52:52 -05:00
ari abfbe98897 - correct usage of ET BT tags
- introduced LTTextBlocks
2018-12-24 01:49:51 -05:00
ari e7701ce2ac - correct usage of the BT ET tags 2018-12-24 01:49:32 -05:00
ari c239f79b2c added settings to setup.py 2018-12-22 23:37:12 -05:00
29 changed files with 462 additions and 398 deletions

No files matched your search

+1 -1
View File
@@ -1 +1 @@
v1.3.0
v1.3.2
+2 -1
View File
@@ -58,7 +58,8 @@ All layout items extend the `LTItem` class. There are two kinds of layout items:
All of these classes extend the LTContainer class.
- LTXObject: a layout item containing other additional layout items
- LTCharBlock: a layout item containing LTChars
- LTCharBlock: a layout item containing LTChars, this corresponds to whenever a `TJ` or `Tj` operators is issued within a text object.
- LTTextBlock: a layout item containing LTCharBlocks, note that this directly corresponds to the `BT` and `ET` operators pair in the pdf standard
#### Layout Components
All of these classes extend the LTComponent class.
+71 -65
View File
@@ -62,14 +62,14 @@
<ul>
<li><a class="toctree-l3" href="#parser">parser</a></li>
<li><a class="toctree-l3" href="#interpreter">interpreter</a></li>
<li><a class="toctree-l3" href="#converters">converters</a></li>
<li><a class="toctree-l3" href="#imagewriter">imagewriter</a></li>
<li><a class="toctree-l3" href="#layouts">layouts</a></li>
</ul>
@@ -111,37 +111,82 @@
<h1 id="api">API</h1>
<p>The library is constructed from 4 base modules:</p>
<ul>
<li><strong>interpreter</strong>: contains low-level classes for extracting fundamental data-structures from the documents</li>
<li><strong>converters</strong>: contains high-level classes for conversion of the fundamental pdf structures to other formats</li>
<li><strong>parser</strong>: contains low-level classes for extracting fundamental data-structures from the documents</li>
<li><strong>interpreter</strong>: a interpreter of the pdf-standard commands</li>
<li><strong>converters</strong>: contains high-level functions for conversion of the fundamental pdf structures to other formats</li>
<li><strong>imagewriter</strong>: contains a simple implementation for converting PDF Image Streams to png/bmp/img formats</li>
<li><strong>layouts</strong>: contains an abstraction layer for the data-structures found in the pdf</li>
</ul>
<h2 id="parser">parser</h2>
<p>WIP</p>
<h2 id="interpreter">interpreter</h2>
<pre><code>WIP
</code></pre>
<h2 id="converters">converters</h2>
<p>Contains high-level classes for conversion of the fundamental pdf structures to other formats. This library includes 4 high-level conversion classes:</p>
<ul>
<li>HTMLConverter</li>
<li>JSONConverter</li>
<li>XMLConverter</li>
<li>TextConverter</li>
</ul>
<p>These classes all extend the <a href="#converters.PDFConverter">PDFConverter</a>. To use them simply call the static method <a href="#converterspdfconverterparse_fileinput_file-output_file-image_folder_path-codec-maxpages-password-caching-check_extractable-pagenos">parse_file</a>.</p>
<p>a interpreter of the pdf-standard commands</p>
<h3 id="example">Example</h3>
<pre><code class="py">from pdfmajor.converters import HTMLConverter
<pre><code class="py">from pdfmajor.interpreter import PageInterpreter
input_file = open('path/to/input/file.pdf', 'rb')
output_file = open('path/to/output/file.html', 'wb')
HTMLConverter.parse_file(
input_file,
output_file
)
input_file.close()
output_file.close()
for page in PDFInterpreter(&quot;/path/to/pdf.pdf&quot;):
print(&quot;page start&quot;, page.page_num)
for item in page:
print(&quot; &gt;&quot;, item)
print(&quot;page end&quot;, page.page_num)
</code></pre>
<h3 id="convertersconvert_fileinput_file-output_file-image_folder_path-codec-maxpages-password-caching-check_extractable-pagenos-out_type">converters.convert_file(input_file, output_file, image_folder_path, codec, maxpages, password, caching, check_extractable, pagenos, out_type)</h3>
<h3 id="interpreterpdfinterpreter">interpreter.PDFInterpreter</h3>
<p>This generator-function yields individual pages which contain their respected items.</p>
<h4 id="arguments">Arguments</h4>
<ul>
<li><code>input_file_path</code>: <a href="#">str</a></li>
<li><code>preload</code>: <a href="#">bool</a> defaults to False</li>
<li><code>maxpages</code>: <a href="#">int</a> defaults to 0 </li>
<li><code>password</code>: <a href="#">str</a> defaults to None </li>
<li><code>caching</code>: <a href="#">bool</a> defaults to True </li>
<li><code>check_extractable</code>: <a href="#">bool</a> defaults to True</li>
<li><code>pagenos</code>: <a href="#">List[int]</a> defaults to None</li>
<li><code>debug_level</code>: <a href="#https://docs.python.org/3/library/logging.html#levels">logging.levels</a> defaults logging.WARNING</li>
</ul>
<h4 id="yield-value">Yield Value</h4>
<p>This function returns a generator that yields <a href="#interpreterpageinterpreter">PDFInterpreter</a>.</p>
<h3 id="interpreterpageinterpreter">interpreter.PageInterpreter</h3>
<p>This generator-function-class yields individual <a href="#layout-items">layout items</a>.</p>
<h3 id="layout-items">Layout Items</h3>
<p>All layout items extend the <code>LTItem</code> class. There are two kinds of layout items:</p>
<ul>
<li>LTComponent: extends the base <code>LTItem</code> class, this class will have additional values such as boundary boxes, height and width</li>
<li>LTContainer: extends the <code>LTComponent</code> class, this class is used to contain elements of the pdf that would have child elements. Iterating on this element will output its child elements.</li>
</ul>
<h4 id="layout-containers">Layout Containers</h4>
<p>All of these classes extend the LTContainer class.</p>
<ul>
<li>LTXObject: a layout item containing other additional layout items</li>
<li>LTCharBlock: a layout item containing LTChars, this corresponds to whenever a <code>TJ</code> or <code>Tj</code> operators is issued within a text object.</li>
<li>LTTextBlock: a layout item containing LTCharBlocks, note that this directly corresponds to the <code>BT</code> and <code>ET</code> operators pair in the pdf standard</li>
</ul>
<h4 id="layout-components">Layout Components</h4>
<p>All of these classes extend the LTComponent class.</p>
<ul>
<li>LTChar: an individual character</li>
<li>LTCurves: represents a collection of svg-paths (available under <code>self.paths</code>)</li>
<li>LTImage: a component containing information regarding an image</li>
</ul>
<h2 id="converters">converters</h2>
<p>Contains high-level functions for conversion of the fundamental pdf structures to other formats. This library includes 4 high-level conversion cases:</p>
<ul>
<li>HTML</li>
<li>JSON</li>
<li>XML</li>
<li>Text</li>
</ul>
<p>These formats are all generated using the <a href="#PDFInterpreter">PageInterpreter</a>. To use them simply call the static method <a href="#converterspdfconverter">parse_file</a>.</p>
<h3 id="example_1">Example</h3>
<pre><code class="py">from pdfmajor.converters import convert_file
convert_file(
&quot;path/to/input/file.pdf&quot;,
&quot;path/to/output/file.html&quot;,
out_type=&quot;html&quot;
)
</code></pre>
<h3 id="convertersconvert_file">converters.convert_file</h3>
<p>A high-level abstraction for the conversion classes.</p>
<ul>
<li><code>input_file</code>: <a href="https://docs.python.org/3/library/io.html#io.TextIOWrapper">TextIOWrapper</a> </li>
@@ -155,46 +200,7 @@ output_file.close()
<li><code>pagenos</code>: <a href="#">List[int]</a> defaults to None</li>
<li><code>out_type</code>: <a href="#">str</a> defaults to 'html'</li>
</ul>
<h3 id="example-convertersconvert_file">Example (converters.convert_file)</h3>
<pre><code class="py">from pdfmajor.converters import convert_file
input_file = open('path/to/input/file.pdf', 'rb')
output_file = open('path/to/output/file.html', 'wb')
convert_file(
input_file,
output_file
)
input_file.close()
output_file.close()
</code></pre>
<h3 id="converterspdfconverter">converters.PDFConverter</h3>
<p>Extends <code>layouts.PDFLayoutAnalyzer</code>.</p>
<h4 id="converterspdfconverter9595init9595rsrcmgr-outfp-imagewriter-codec-pageno">converters.PDFConverter.__init__(rsrcmgr, outfp, imagewriter, codec, pageno)</h4>
<ul>
<li><code>rsrcmgr</code>: <a href="#interpreter.PDFResourceManager">PDFResourceManager</a></li>
<li><code>outfp</code>: <a href="https://docs.python.org/3/library/io.html#io.TextIOWrapper">TextIOWrapper</a></li>
<li><code>imagewriter</code>: <a href="#imagewriter.ImageWriter">ImageWriter</a></li>
<li><code>codec</code>: <a href="#">str</a> defaults to 'utf-8'</li>
<li><code>pageno</code>: <a href="#">int</a> defaults to 1</li>
</ul>
<p>initialization function for the class</p>
<h4 id="converterspdfconverterparse_fileinput_file-output_file-image_folder_path-codec-maxpages-password-caching-check_extractable-pagenos">converters.PDFConverter.parse_file(input_file, output_file, image_folder_path, codec, maxpages, password, caching, check_extractable, pagenos)</h4>
<p>returns the output file <a href="https://docs.python.org/3/library/io.html#io.TextIOWrapper">TextIOWrapper</a>.</p>
<ul>
<li><code>input_file</code>: <a href="https://docs.python.org/3/library/io.html#io.TextIOWrapper">TextIOWrapper</a> </li>
<li><code>output_file</code>: <a href="https://docs.python.org/3/library/io.html#io.TextIOWrapper">TextIOWrapper</a> </li>
<li><code>image_folder_path</code>: <a href="#">str</a> defaults to None</li>
<li><code>codec</code>: <a href="#">str</a> defaults to 'utf-8'</li>
<li><code>maxpages</code>: <a href="#">int</a> defaults to 0 </li>
<li><code>password</code>: <a href="#">str</a> defaults to None </li>
<li><code>caching</code>: <a href="#">bool</a> defaults to True </li>
<li><code>check_extractable</code>: <a href="#">bool</a> defaults to True</li>
<li><code>pagenos</code>: <a href="#">List[int]</a> defaults to None</li>
</ul>
<h2 id="imagewriter">imagewriter</h2>
<p>WIP </p>
<h2 id="layouts">layouts</h2>
<p>WIP </p>
</div>
+20 -7
View File
@@ -59,6 +59,8 @@
<li><a class="toctree-l3" href="#documentation">Documentation</a></li>
<li><a class="toctree-l3" href="#basic-example">Basic Example</a></li>
<li><a class="toctree-l3" href="#why-another-fork">Why Another Fork?</a></li>
<li><a class="toctree-l3" href="#features">Features</a></li>
@@ -122,23 +124,34 @@
</tr>
</table>
<p>PDF-Major is a fork of <code>PDFMiner.six</code>. It is meant to be a more light-weight implementation that makes fewer assumptions on the data. Additionally, this version intends to be more up to date with recent versions of python and make liberal use of the typing capabilities of python for improved future maintainability.</p>
<p>PDF-Major is a fork of <code>PDFMiner.six</code>. 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.</p>
<h2 id="documentation">Documentation</h2>
<p>See documentation <a href="https://asosnovsky.github.io/pdfmajor/">here</a>.</p>
<h2 id="basic-example">Basic Example</h2>
<pre><code class="py">from pdfmajor.interpreter import PageInterpreter
for page in PDFInterpreter(&quot;/path/to/pdf.pdf&quot;):
print(&quot;page start&quot;, page.page_num)
for item in page:
print(&quot; &gt;&quot;, item)
print(&quot;page end&quot;, page.page_num)
</code></pre>
<h2 id="why-another-fork">Why Another Fork?</h2>
<p><code>PDFMiner</code> was designed to run in python 2.7&gt;, <code>PDFMiner.six</code> was written to bring cross-version support for python 2 and 3. This versions intends to make use of the latest improvement in the python language while remaining fairly simple to develop with. </p>
<p><code>PDFMiner</code> was designed to run in python 2.7&gt;, <code>PDFMiner.six</code> was written to bring cross-version support for python 2 and 3. However, after attempting to build and extend upon <code>PDFMiner.six</code> 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.</p>
<h2 id="features">Features</h2>
<ul>
<li>Written entirely in Python.</li>
<li>Functional extraction method based on generators</li>
<li>Parse, analyze, and convert PDF documents.</li>
<li>PDF-1.7 specification support. (well, almost)</li>
<li>Font-Color extraction</li>
<li>Shape fill and stroke color extraction</li>
<li>CJK languages and vertical writing scripts support.</li>
<li>Various font types (Type1, TrueType, Type3, and CID) support.</li>
<li>Basic encryption (RC4) support.</li>
<li>Font-Color extraction</li>
<li>Shape fill and stroke color extraction</li>
</ul>
<p><strong>Note</strong>: 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 extending the <code>pdfmajor.layouts.PDFLayoutAnalyzer</code> class, but at the current time is not supported.</p>
<p><strong>Note</strong>: 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 <code>pdfmajor.interpreter.PageInterpreter</code> class, but at the current time is not supported.</p>
<h2 id="how-to-install">How to Install</h2>
<h3 id="source">Source</h3>
<ul>
@@ -227,5 +240,5 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.</p>
<!--
MkDocs version : 1.0.4
Build Date UTC : 2018-11-04 21:27:38
Build Date UTC : 2018-12-24 06:52:28
-->
+1 -1
View File
@@ -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. Additionally, this version intends to be more up to date with recent versions of python and make liberal use of the typing capabilities of python for improved future maintainability. Documentation See documentation here . 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. This versions intends to make use of the latest improvement in the python language while remaining fairly simple to develop with. Features Written entirely in Python. Parse, analyze, and convert PDF documents. PDF-1.7 specification support. (well, almost) CJK languages and vertical writing scripts support. Various font types (Type1, TrueType, Type3, and CID) support. Basic encryption (RC4) support. Font-Color extraction Shape fill and stroke color extraction 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 extending the pdfmajor.layouts.PDFLayoutAnalyzer 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. Additionally, this version intends to be more up to date with recent versions of python and make liberal use of the typing capabilities of python for improved future maintainability.","title":"PDFMajor"},{"location":"#documentation","text":"See documentation here .","title":"Documentation"},{"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. This versions intends to make use of the latest improvement in the python language while remaining fairly simple to develop with.","title":"Why Another Fork?"},{"location":"#features","text":"Written entirely in Python. Parse, analyze, and convert PDF documents. PDF-1.7 specification support. (well, almost) CJK languages and vertical writing scripts support. Various font types (Type1, TrueType, Type3, and CID) support. Basic encryption (RC4) support. Font-Color extraction Shape fill and stroke color extraction 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 extending the pdfmajor.layouts.PDFLayoutAnalyzer class, but at the current time is not supported.","title":"Features"},{"location":"#how-to-install","text":"","title":"How to Install"},{"location":"#source","text":"Install Python 3.6.4 or newer. clone this repo git clone https://github.com/asosnovsky/pdfmajor * install repo python setup.py install","title":"Source"},{"location":"#pypi","text":"Install Python 3.6.4 or newer. install repo pip install pdfmajor","title":"Pypi"},{"location":"#terms-and-conditions","text":"(This is so-called MIT/X License) Copyright (c) 2018-2019 Ariel Sosnovsky Permission is hereby granted, free of chargeLine 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 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
+2 -2
View File
@@ -2,12 +2,12 @@
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>None</loc>
<lastmod>2018-11-04</lastmod>
<lastmod>2018-12-24</lastmod>
<changefreq>daily</changefreq>
</url>
<url>
<loc>None</loc>
<lastmod>2018-11-04</lastmod>
<lastmod>2018-12-24</lastmod>
<changefreq>daily</changefreq>
</url>
</urlset>
Binary file not shown.
+60
View File
@@ -0,0 +1,60 @@
import struct
from io import BytesIO
from ..parser.PDFStream import LITERALS_DCT_DECODE, PDFStream
def align32(x):
return ((x+3)//4)*4
## BMPWriter
##
class BMPWriter(object):
@classmethod
def write_all(cls, fp, stream: PDFStream, bits: int, width: float, height: float, step: float):
bmp = cls(fp, 1, width, height)
data = stream.get_data()
i = 0
for y in range(height):
bmp.write_line(y, data[i:i+step])
i += step
def __init__(self, fp, bits, width, height):
self.fp = fp
self.bits = bits
self.width = width
self.height = height
if bits == 1:
ncols = 2
elif bits == 8:
ncols = 256
elif bits == 24:
ncols = 0
else:
raise ValueError(bits)
self.linesize = align32((self.width*self.bits+7)//8)
self.datasize = self.linesize * self.height
headersize = 14+40+ncols*4
info = struct.pack('<IiiHHIIIIII', 40, self.width, self.height, 1, self.bits, 0, self.datasize, 0, 0, ncols, 0)
assert len(info) == 40, str(len(info))
header = struct.pack('<ccIHHI', b'B', b'M', headersize+self.datasize, 0, 0, headersize)
assert len(header) == 14, str(len(header))
self.fp.write(header)
self.fp.write(info)
if ncols == 2:
# B&W color table
for i in (0, 255):
self.fp.write(struct.pack('BBBx', i, i, i))
elif ncols == 256:
# grayscale color table
for i in range(256):
self.fp.write(struct.pack('BBBx', i, i, i))
self.pos0 = self.fp.tell()
self.pos1 = self.pos0 + self.datasize
return
def write_line(self, y, data):
self.fp.seek(self.pos1 - (y+1)*self.linesize)
self.fp.write(data)
return
+1 -1
View File
@@ -97,7 +97,7 @@ OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
__version__ = (1, 3, 0)
__version__ = (1, 3, 2)
if __name__ == '__main__':
print(
+8 -4
View File
@@ -1,7 +1,7 @@
from typing import List
from .writers.html import HTMLMaker
from ..interpreter import PDFInterpreter, PageInterpreter, logging
from ..interpreter import LTImage, LTCharBlock, LTChar, LTCurve, LTXObject
from ..interpreter import LTImage, LTTextBlock, LTCharBlock, LTChar, LTCurve, LTXObject
from ..interpreter.commands import LTItem
from ..interpreter.commands.state import CurvePath, PDFColor
@@ -76,8 +76,12 @@ def render_page(html: HTMLMaker, ltpage: PageInterpreter):
'max-height': item.height,
}):
html.write(item.get_text())
elif isinstance(item, LTTextBlock):
for char in item:
render(char)
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',
'left': f'{item.x0}',
'bottom': f'{item.y0}',
@@ -98,8 +102,8 @@ def render_page(html: HTMLMaker, ltpage: PageInterpreter):
'justify-content': 'space-between',
# 'padding-left': item.font.leading
}):
for char in item:
render(char)
for char in item:
render(char)
with html.elm('div', { "class": 'page', "id": f"page-{ltpage.page_num}" }, {
'width': f'{ltpage.width}px',
+14 -4
View File
@@ -2,7 +2,7 @@ from typing import List
from .writers.json import JSONMaker, JSONMakerObject, JSONMakerArray
from ..interpreter import PDFInterpreter, PageInterpreter, logging
from ..interpreter import LTImage, LTCharBlock, LTChar, LTCurve, LTXObject
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
@@ -76,11 +76,21 @@ def render(json: JSONMakerObject, item: LTItem):
json.number("x1", item.x1)
json.number("y0", item.y0)
json.number("y1", item.y1)
elif isinstance(item, LTCharBlock):
place_char_block(json, item)
elif isinstance(item, LTTextBlock):
place_text_block(json, item)
def place_text_block(json: JSONMakerObject, text_block: LTTextBlock):
json.string("type", 'text-block')
json.number("x0", text_block.x0)
json.number("x1", text_block.x1)
json.number("y0", text_block.y0)
json.number("y1", text_block.y1)
with json.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(json: JSONMakerObject, char_block: LTCharBlock):
json.string("type", 'char-block')
json.number("size", char_block.size)
json.number("x0", char_block.x0)
json.number("x1", char_block.x1)
+5 -4
View File
@@ -1,7 +1,7 @@
from typing import List
from ..interpreter import PDFInterpreter, PageInterpreter, logging
from ..interpreter import LTCharBlock, LTXObject
from ..interpreter import LTTextBlock, LTXObject
def convert_to_text(
@@ -28,9 +28,10 @@ def convert_to_text(
with open(output_file_path, 'wb') as outfp:
def process_container(container: LTXObject):
for item in container:
if isinstance(item, LTCharBlock):
for char in item:
outfp.write(char.get_text().encode(codec))
if isinstance(item, LTTextBlock):
for text in item:
for char in text:
outfp.write(char.get_text().encode(codec))
elif isinstance(item, LTXObject):
process_container(item)
for page in intepreter:
+14 -3
View File
@@ -4,7 +4,7 @@ from xml.sax import saxutils
from .writers.xml import XMLMaker
from ..interpreter import PDFInterpreter, PageInterpreter, logging
from ..interpreter import LTImage, LTCharBlock, LTChar, LTCurve, LTXObject
from ..interpreter import LTImage, LTTextBlock, LTCharBlock, LTChar, LTCurve, LTXObject
from ..interpreter.commands import LTItem
from ..interpreter.commands import LTRect, LTLine
from ..interpreter.commands.state import CurvePath, PDFColor
@@ -86,8 +86,8 @@ def render_page(xml: XMLMaker, ltpage: PageInterpreter):
"y0": item.y0,
"y1": item.y1,
})
elif isinstance(item, LTCharBlock):
place_char_block(xml, item)
elif isinstance(item, LTTextBlock):
place_text_block(xml, item)
with xml.elm('page', {
"id": ltpage.page_num,
"width": ltpage.width,
@@ -121,6 +121,17 @@ def place_char_block(xml: XMLMaker, char_block: LTCharBlock):
for char in char_block:
place_char(xml, char)
def place_text_block(xml: XMLMaker, text_block: LTCharBlock):
attr = {
"x0": text_block.x0,
"x1": text_block.x1,
"y0": text_block.y0,
"y1": text_block.y1,
}
with xml.elm("text-block", attr):
for char_block in text_block:
place_char_block(xml, char_block)
def place_curve(xml: XMLMaker, item: LTCurve):
attr = {
"x0": item.x0,
-127
View File
@@ -1,127 +0,0 @@
import struct
import os
import os.path
from io import BytesIO
from ..interpreter.PDFStream import LITERALS_DCT_DECODE
from ..interpreter.PDFColorSpace import LITERAL_DEVICE_GRAY
from ..interpreter.PDFColorSpace import LITERAL_DEVICE_RGB
from ..interpreter.PDFColorSpace import LITERAL_DEVICE_CMYK
from ..layouts import LTImage
def align32(x):
return ((x+3)//4)*4
## BMPWriter
##
class BMPWriter(object):
def __init__(self, fp, bits, width, height):
self.fp = fp
self.bits = bits
self.width = width
self.height = height
if bits == 1:
ncols = 2
elif bits == 8:
ncols = 256
elif bits == 24:
ncols = 0
else:
raise ValueError(bits)
self.linesize = align32((self.width*self.bits+7)//8)
self.datasize = self.linesize * self.height
headersize = 14+40+ncols*4
info = struct.pack('<IiiHHIIIIII', 40, self.width, self.height, 1, self.bits, 0, self.datasize, 0, 0, ncols, 0)
assert len(info) == 40, str(len(info))
header = struct.pack('<ccIHHI', b'B', b'M', headersize+self.datasize, 0, 0, headersize)
assert len(header) == 14, str(len(header))
self.fp.write(header)
self.fp.write(info)
if ncols == 2:
# B&W color table
for i in (0, 255):
self.fp.write(struct.pack('BBBx', i, i, i))
elif ncols == 256:
# grayscale color table
for i in range(256):
self.fp.write(struct.pack('BBBx', i, i, i))
self.pos0 = self.fp.tell()
self.pos1 = self.pos0 + self.datasize
return
def write_line(self, y, data):
self.fp.seek(self.pos1 - (y+1)*self.linesize)
self.fp.write(data)
return
## ImageWriter
##
class ImageWriter(object):
def __init__(self, outdir: str = None):
if outdir is None:
self.inactive = True
else:
self.inactive = False
self.outdir = outdir
if not os.path.exists(self.outdir):
os.makedirs(self.outdir)
def export_image(self, image: LTImage) -> str:
if self.inactive:
return "N/A"
stream = image.stream
filters = stream.get_filters()
(width, height) = image.srcsize
if len(filters) == 1 and filters[0][0] in LITERALS_DCT_DECODE:
ext = '.jpg'
elif (image.bits == 1 or
image.bits == 8 and image.colorspace in (LITERAL_DEVICE_RGB, LITERAL_DEVICE_GRAY)):
ext = '.%dx%d.bmp' % (width, height)
else:
ext = '.%d.%dx%d.img' % (image.bits, width, height)
name = image.name+ext
path = os.path.join(self.outdir, name)
fp=open(path, 'wb')
if ext == '.jpg':
raw_data = stream.get_rawdata()
if LITERAL_DEVICE_CMYK in image.colorspace:
from PIL import Image
from PIL import ImageChops
ifp = BytesIO(raw_data)
i = Image.open(ifp)
i = ImageChops.invert(i)
i = i.convert('RGB')
i.save(fp, 'JPEG')
else:
fp.write(raw_data)
elif image.bits == 1:
bmp = BMPWriter(fp, 1, width, height)
data = stream.get_data()
i = 0
width = (width+7)//8
for y in range(height):
bmp.write_line(y, data[i:i+width])
i += width
elif image.bits == 8 and image.colorspace is LITERAL_DEVICE_RGB:
bmp = BMPWriter(fp, 24, width, height)
data = stream.get_data()
i = 0
width = width*3
for y in range(height):
bmp.write_line(y, data[i:i+width])
i += width
elif image.bits == 8 and image.colorspace is LITERAL_DEVICE_GRAY:
bmp = BMPWriter(fp, 8, width, height)
data = stream.get_data()
i = 0
for y in range(height):
bmp.write_line(y, data[i:i+width])
i += width
else:
fp.write(stream.get_data())
fp.close()
return name
+17 -53
View File
@@ -1,13 +1,8 @@
from pdfmajor.parser.PDFPage import PDFPage
from pdfmajor.parser.PDFStream import list_value
from pdfmajor.parser.PDFContentParser import PDFContentParser
from pdfmajor.parser.PSStackParser import PSKeyword
from pdfmajor.parser.PSStackParser import keyword_name
from .commands import PDFStateStack, PDFCommands
from .commands import LTXObject
from .utils import init_resources
from .XObjectInterpreter import XObjectInterpreter
from .commands import PDFStateStack
from .commands import process_command_stream, prep_state
class PageInterpreter:
class PDFInterpreterError(Exception): pass
@@ -31,52 +26,21 @@ class PageInterpreter:
self.width = x1-x0
# Init State
self.state: PDFStateStack = PDFStateStack()
self.state.t_matrix = ctm
self.state.resources = page.resources
# set some global states.
self.state.graphics.ncolspace = self.state.graphics.scolspace = None
if self.state.colorspace_map:
col_space = next(iter(self.state.colorspace_map.values()))
self.state.graphics.ncolspace = self.state.graphics.scolspace = col_space
# Init Resources
init_resources(self.state, self.font_cache)
self.state: PDFStateStack = prep_state(
PDFStateStack(),
ctm=ctm,
resources=page.resources,
font_cache=self.font_cache
)
def __iter__(self):
parser = PDFContentParser(list_value(self.page.contents))
history = []
for obj in parser:
history.append(obj)
if isinstance(obj, PSKeyword):
name = keyword_name(obj)
method = name.replace('*', '_a').replace('"', '_w').replace("'", '_q')
if method in PDFCommands.commands.keys():
func = PDFCommands.commands.get(method)
nargs = func.__code__.co_argcount-1
if nargs:
args = self.state.pop(nargs)
if len(args) == nargs:
func(self.state, *args)
else:
raise self.PDFInterpreterError(f"Invalid Number of Args provided {method}")
else:
# log.debug('exec: %s', name)
func(self.state)
else:
raise self.PDFInterpreterError('Unknown operator: %r' % name)
else:
self.state.argstack.append(obj)
for complete_item in self.state.complete_layout_items:
if isinstance(complete_item, LTXObject):
interpeter = XObjectInterpreter(
streams=[complete_item.stream],
resources=complete_item.resources,
ctm=complete_item.t_matrix
)
for item in interpeter:
complete_item.add(item)
yield complete_item
self.state.complete_layout_items = []
for item in process_command_stream(
streams=list_value(self.page.contents),
font_cache=self.font_cache,
state=self.state
):
yield item
def __repr__(self) -> str:
return f"<Page:{self.page_num}/>"
return f"<Page:{self.page_num} width={self.width} height={self.height}/>"
@@ -1,70 +0,0 @@
from pdfmajor.utils import MATRIX_IDENTITY
from pdfmajor.parser.PSStackParser import literal_name
from pdfmajor.parser.PDFParser import PDFStream
from pdfmajor.parser.PSStackParser import PSEOF
from pdfmajor.parser.PSStackParser import PSKeyword
from pdfmajor.parser.PSStackParser import keyword_name
from pdfmajor.parser.PSStackParser import PSStackParser
from pdfmajor.parser.constants import LITERAL_PDF, LITERAL_TEXT, LITERAL_FONT
from pdfmajor.parser.PDFStream import resolve1, list_value, dict_value
from pdfmajor.parser.PDFStream.PDFObjRef import PDFObjRef
from pdfmajor.parser.PDFContentParser import PDFContentParser
from .commands.state import PDFStateStack, PDFColorSpace, PREDEFINED_COLORSPACE
from .commands import PDFCommands, LTXObject
from .utils import init_resources
class XObjectInterpreter:
class PDFInterpreterError(Exception): pass
def __init__(self, streams, resources, ctm):
self.streams = streams
self.font_cache = {}
self.state = PDFStateStack()
self.state.t_matrix = ctm
self.state.resources = resources
# set some global states.
self.state.graphics.ncolspace = self.state.graphics.scolspace = None
if self.state.colorspace_map:
col_space = next(iter(self.state.colorspace_map.values()))
self.state.graphics.ncolspace = self.state.graphics.scolspace = col_space
init_resources(self.state, self.font_cache)
def __iter__(self):
parser = PDFContentParser(self.streams)
for obj in parser:
if isinstance(obj, PSKeyword):
name = keyword_name(obj)
method = name.replace('*', '_a').replace('"', '_w').replace("'", '_q')
if method in PDFCommands.commands.keys():
func = PDFCommands.commands.get(method)
nargs = func.__code__.co_argcount-1
if nargs:
args = self.state.pop(nargs)
if len(args) == nargs:
func(self.state, *args)
else:
raise self.PDFInterpreterError(f"Invalid Number of Args provided {method}")
else:
# log.debug('exec: %s', name)
func(self.state)
else:
raise self.PDFInterpreterError('Unknown operator: %r' % name)
else:
self.state.argstack.append(obj)
for complete_item in self.state.complete_layout_items:
if isinstance(complete_item, LTXObject):
interpeter = self.__class__(
streams=[complete_item.stream],
resources=complete_item.resources,
ctm=complete_item.t_matrix
)
for item in interpeter:
complete_item.add(item)
yield complete_item
self.state.complete_layout_items = []
+1 -1
View File
@@ -6,7 +6,7 @@ from ..parser.PDFPage import PDFPage
from ..utils import set_log_level, get_logger, logging
from .commands.state import PDFStateStack, PDFColorSpace, PREDEFINED_COLORSPACE
from .commands import PDFCommands
from .commands import LTCharBlock, LTChar
from .commands import LTTextBlock, LTCharBlock, LTChar
from .commands import LTCurve, LTLine, LTHorizontalLine, LTVerticalLine, LTRect
from .commands import LTImage
from .commands import LTXObject
+68 -2
View File
@@ -1,9 +1,75 @@
from typing import List
from pdfmajor.parser.PDFContentParser import PDFContentParser, PDFStream
from pdfmajor.parser.PSStackParser import PSKeyword, keyword_name
from pdfmajor.utils import get_logger
from .commands import PDFCommands
from .state import PDFStateStack, PDFGraphicState, PDFTextState
from .state.PDFGraphicState import PDFColor, PDFColorSpace, PREDEFINED_COLORSPACE
from .state import PDFFont, get_font
from .state import LTCharBlock, LTChar
from .state import LTTextBlock, LTCharBlock, LTChar
from .state import LTCurve, LTLine, LTHorizontalLine, LTVerticalLine, LTRect
from .state import LTImage
from .state import LTXObject
from .state import LTItem, LTComponent, LTContainer
from .state import LTItem, LTComponent, LTContainer
from .utils import init_resources
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)
for obj in parser:
if isinstance(obj, PSKeyword):
name = keyword_name(obj)
method = name.replace('*', '_a').replace('"', '_w').replace("'", '_q')
if method in PDFCommands.commands.keys():
func = PDFCommands.commands.get(method)
nargs = func.__code__.co_argcount-1
if nargs:
args = state.pop(nargs)
if len(args) == nargs:
func(state, *args)
else:
raise CommandProcessorError(f"Invalid Number of Args provided {method}")
else:
func(state)
else:
log.warning('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(),
ctm=complete_item.t_matrix,
resources=complete_item.resources,
font_cache=font_cache
)
for item in process_command_stream(
[complete_item.stream],
font_cache=font_cache,
state=xobj_state
):
complete_item.add(item)
yield complete_item
state.complete_layout_items = []
def prep_state(state: PDFStateStack, ctm: tuple, resources: dict, font_cache: dict) -> PDFStateStack:
state.t_matrix = ctm
state.resources = resources
# set some global states.
state.graphics.ncolspace = state.graphics.scolspace = None
if state.colorspace_map:
col_space = next(iter(state.colorspace_map.values()))
state.graphics.ncolspace = state.graphics.scolspace = col_space
init_resources(state, font_cache)
return state
+41 -11
View File
@@ -1,33 +1,51 @@
from pdfmajor.utils import mult_matrix, MATRIX_IDENTITY, apply_matrix_pt
from pdfmajor.utils import mult_matrix, MATRIX_IDENTITY, apply_matrix_pt, get_logger
from pdfmajor.parser.PSStackParser import literal_name
from pdfmajor.parser.PDFStream import PDFStream, list_value, dict_value
from pdfmajor.parser.constants import LITERAL_FORM, LITERAL_IMAGE
from .PDFCommands import PDFCommands
from .state import PDFStateStack, PDFGraphicState
from .state import PDFStateStack, PDFGraphicState, LTTextBlock
from .state.Curves import CurveMethod, CurvePath, CurvePoint
# from .state.PDFItem import PDFImage, PDFShape, PDFText, PDFXObject
from .state import make_char_block, make_curve, make_image, make_xobject
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:
return stack
# @PDFCommands.add('W','W_a', 'sh', 'ET', 'BX', 'EX' ,'BI', 'ID', 'gs')
# def do_no_support(stack: PDFStateStack) -> PDFStateStack:
# # log.warning("Unsupported command")
# return stack
# marked content operators
@PDFCommands.add('MP', 'DP')
def do_MP(stack: PDFStateStack) -> PDFStateStack:
@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('BMC', 'BDC')
def do_BMC(stack: PDFStateStack) -> PDFStateStack:
@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('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('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('EMC')
def do_EMC(stack: PDFStateStack) -> PDFStateStack:
# self.device.end_tag(tag)
log.warning("Unsupported command - end_tag")
return stack
@PDFCommands.add('q')
@@ -332,6 +350,16 @@ def do_scn(stack: PDFStateStack) -> PDFStateStack:
def do_BT(stack: PDFStateStack) -> PDFStateStack:
stack.text.matrix = MATRIX_IDENTITY
stack.text.linematrix = [0, 0]
if stack.current_textblock is None:
stack.current_textblock = LTTextBlock()
else:
raise PDFCommands.InvalidOperation("Last TextBlock did not clear")
return stack
@PDFCommands.add('ET')
def do_ET(stack: PDFStateStack) -> PDFStateStack:
stack.complete_layout_items.append(stack.current_textblock)
stack.current_textblock = None
return stack
# setcharspace
@@ -417,12 +445,14 @@ def do_T_a(stack: PDFStateStack) -> PDFStateStack:
def do_TJ(stack: PDFStateStack, seq: bytearray) -> PDFStateStack:
if stack.text.font is None:
raise PDFCommands.InvalidOperation("No Font Specified")
stack.complete_layout_items.append(make_char_block(
if stack.current_textblock is None:
raise PDFCommands.InvalidOperation("No TextBlock initilized")
stack.current_textblock.add_char_block(
seq,
stack.t_matrix,
stack.text.copy(),
stack.graphics.ncolor.copy()
))
)
return stack
# show
@@ -10,7 +10,7 @@ from .PDFTextState import PDFTextState, PDFFont, get_font
from .Curves import CurvePath
from .layout import LTItem, LTComponent, LTContainer
from .layout import LTCharBlock, LTChar
from .layout import LTTextBlock, LTCharBlock, LTChar
from .layout import LTCurve, LTLine, LTHorizontalLine, LTVerticalLine, LTRect
from .layout import LTXObject
from .layout import LTImage
@@ -30,6 +30,7 @@ class PDFStateStack:
self.fontmap = {}
self.xobjmap = {}
self.resources = {}
self.current_textblock: LTTextBlock = None
def pop(self, n: int) -> bytearray:
if n == 0:
@@ -1,7 +1,19 @@
import os
from io import BytesIO
import PIL as pillow
from pdfmajor.BMPWriter import BMPWriter
from pdfmajor.utils import Bbox
from pdfmajor.parser.PDFStream import PDFStream
from pdfmajor.parser.PDFStream import PDFStream, LITERALS_DCT_DECODE
from pdfmajor.parser.constants import LIT
from ._base import LTComponent
LITERAL_DEVICE_GRAY = LIT('DeviceGray')
LITERAL_DEVICE_RGB = LIT('DeviceRGB')
LITERAL_DEVICE_CMYK = LIT('DeviceCMYK')
## LTImage
##
class LTImage(LTComponent):
@@ -22,4 +34,46 @@ class LTImage(LTComponent):
def __repr__(self):
return ('<%s(%s) %s %r>' %
(self.__class__.__name__, self.name,
str(self.bbox), self.srcsize))
str(self.bbox), self.srcsize))
def save_image(self, outdir: str, file_name: str = None):
stream = self.stream
filters = stream.get_filters()
width = self.bbox.width
height = self.bbox.height
bits = self.bits
if len(filters) == 1 and filters[0][0] in LITERALS_DCT_DECODE:
ext = '.jpg'
elif (bits == 1 or
bits == 8 and self.colorspace in (LITERAL_DEVICE_RGB, LITERAL_DEVICE_GRAY)):
ext = '.w%dh%d.bmp' % (width, height)
else:
ext = '.b%dw%dh%d.img' % (bits, width, height)
if file_name is None:
file_name = self.name + ext
else:
file_name += ext
path = os.path.join(outdir, file_name)
with open(path, 'wb') as fp:
if ext == '.jpg':
raw_data = stream.get_rawdata()
if LITERAL_DEVICE_CMYK in self.colorspace:
ifp = BytesIO(raw_data)
i = pillow.Image.open(ifp)
i = pillow.ImageChops.invert(i)
i = i.convert('RGB')
i.save(fp, 'JPEG')
else:
fp.write(raw_data)
elif bits == 1:
BMPWriter.write_all(fp, stream, 1, width, height, (width+7)//8)
elif bits == 8 and self.colorspace is LITERAL_DEVICE_RGB:
BMPWriter.write_all(fp, stream, 24, width, height, width*3)
elif bits == 8 and self.colorspace is LITERAL_DEVICE_GRAY:
BMPWriter.write_all(fp, stream, 8, width, height, width)
else:
fp.write(stream.get_data())
return path
@@ -0,0 +1,40 @@
from typing import List
from pdfmajor.utils import apply_matrix_norm, apply_matrix_pt, matrix2str, Bbox, Point
from ..PDFGraphicState import PDFColor
from ..PDFTextState import PDFFont
from ..PDFTextState import PDFTextState
from ._base import LTContainer
from .LTCharBlock import LTCharBlock
from .utils.textdecoder import decode_text_seq
## LTTextBlock
##
class LTTextBlock(LTContainer):
def __init__(self, x0=None, y0=None, x1=None, y1=None):
LTContainer.__init__(self,
Bbox(x0, y0, x1, y1),
[]
)
def add_char_block(self, seq: bytearray, ctm: tuple, textstate: PDFTextState, color: PDFColor):
(char_meta_datas, textstate, color) = decode_text_seq(seq, ctm, textstate, color)
char_block = LTCharBlock(
chars=char_meta_datas,
color=color,
textstate=textstate
)
if None in [self.bbox.x0, self.bbox.x1, self.bbox.y0, self.bbox.y1]:
self.bbox.x0 = char_block.x0
self.bbox.x1 = char_block.x1
self.bbox.y0 = char_block.y0
self.bbox.y1 = char_block.y1
else:
self.bbox.x0 = min([self.bbox.x0, char_block.x0])
self.bbox.x1 = max([self.bbox.x1, char_block.x1])
self.bbox.y0 = min([self.bbox.y0, char_block.y0])
self.bbox.y1 = max([self.bbox.y1, char_block.y1])
self.add(char_block)
@@ -6,6 +6,7 @@ from ..PDFGraphicState import PDFGraphicState, PDFColor
from ..PDFTextState import PDFTextState
from ._base import LTItem, LTComponent, LTContainer
from .LTTextBlock import LTTextBlock
from .LTCharBlock import LTCharBlock, LTChar
from .LTCurves import LTCurve, LTLine, LTHorizontalLine, LTRect, LTVerticalLine
from .LTImage import LTImage
@@ -4,8 +4,8 @@ from pdfmajor.parser.PDFStream.PDFObjRef import PDFObjRef
from pdfmajor.parser.PSStackParser import literal_name
from pdfmajor.parser.constants import LITERAL_PDF, LITERAL_TEXT
from .commands import PDFColorSpace, PREDEFINED_COLORSPACE
from .commands import PDFStateStack, get_font
from .state import PDFColorSpace, PREDEFINED_COLORSPACE
from .state import PDFStateStack, get_font
def get_colorspace(spec):
if isinstance(spec, list):
-35
View File
@@ -1,35 +0,0 @@
import collections
from .PSStackParser import LIT
## PDFColorSpace
##
LITERAL_DEVICE_GRAY = LIT('DeviceGray')
LITERAL_DEVICE_RGB = LIT('DeviceRGB')
LITERAL_DEVICE_CMYK = LIT('DeviceCMYK')
class PDFColorSpace(object):
def __init__(self, name: str, ncomponents: int):
self.name: str = name
self.ncomponents: int = ncomponents
return
def __repr__(self):
return '<PDFColorSpace: %s, ncomponents=%d>' % (self.name, self.ncomponents)
PREDEFINED_COLORSPACE = collections.OrderedDict()
for (name, n) in [
('DeviceGray', 1), # default value first
('CalRGB', 3),
('CalGray', 1),
('Lab', 3),
('DeviceRGB', 3),
('DeviceCMYK', 4),
('Separation', 1),
('Indexed', 1),
('Pattern', 1),
]:
PREDEFINED_COLORSPACE[name]=PDFColorSpace(name, n)
+2 -1
View File
@@ -10,7 +10,7 @@ import sys
with open('README.md') as readme_file:
SETUP = {
"name": "pdfmajor",
"version": "1.3.0",
"version": "1.3.2",
"packages": find_packages(include=['pdfmajor*']),
"install_requires": [
'pycryptodome',
@@ -18,6 +18,7 @@ with open('README.md') as readme_file:
],
"description": 'PDF parser',
"long_description": readme_file.read(),
'long_description_content_type': 'text/markdown',
"license": 'MIT/X',
"author": 'Ariel Sosnovsky + Yusuke Shinyama + Philippe Guglielmetti',
"author_email": 'ariel@sosnovsky.ca',
Binary file not shown.
Binary file not shown.
+33
View File
@@ -0,0 +1,33 @@
import os
from pdfmajor.interpreter import LTImage, logging
from pdfmajor.interpreter import PageInterpreter, PDFInterpreter
from pdfmajor.interpreter.commands import LTItem
from pdfmajor.interpreter.commands import LTCharBlock, LTChar
CUR_PATH = os.path.dirname(os.path.dirname(__file__))
FILES = [
"jpg",
"looseless"
]
OUTPUT_FOLDER = os.path.join(
CUR_PATH,
'samples/output/images'
)
if not os.path.exists(OUTPUT_FOLDER):
os.makedirs(OUTPUT_FOLDER)
for file_name in FILES:
for page in PDFInterpreter(os.path.join(
CUR_PATH,
f"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(
OUTPUT_FOLDER,
file_name=f'{file_name}_p{page.page_num}i{item_idx}'
))