Add save PDF in memory

This commit is contained in:
Mauricio Baeza 2021-06-29 22:01:10 -05:00
parent 467280fca3
commit 94d8c43b1b
12 changed files with 249 additions and 9 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -142,6 +142,34 @@ With arguments.
doc = app.docs.open(path, args)
Save
^^^^
To PDF
^^^^^^
* Save in path
.. code-block:: python
doc = app.active
path = '/home/mau/test.pdf'
doc.to_pdf(path)
* Save in memory
.. code-block:: python
doc = app.active
pdf = doc.to_pdf()
Export
^^^^^^
Call dispatch
^^^^^^^^^^^^^

View File

@ -508,6 +508,47 @@ You can used the same way that `python csv`_
app.msgbox(data)
Zip, unzip
^^^^^^^^^^
For default, save in the same directory with the same name + `.zip`
* Zip file
.. code-block:: python
source = '/home/mau/logo.svg'
path_zip = app.paths.zip(source)
app.debug(path_zip)
* Zip directory
.. code-block:: python
source = '/home/mau/test'
path_zip = app.paths.zip(source)
app.debug(path_zip)
* Set target
.. code-block:: python
source = '/home/mau/logo.svg'
target = '/home/mau/myfile.zip'
path_zip = app.paths.zip(source, target)
app.debug(path_zip)
* Read content of zip
.. code-block:: python
path_zip = '/home/mau/test.zip'
names = app.paths.zip_content(path_zip)
for n in names:
app.debug(n)
Copy files
^^^^^^^^^^

View File

@ -142,6 +142,30 @@
</pre></div>
</div>
</section>
<section id="save">
<h2>Save<a class="headerlink" href="#save" title="Permalink to this headline"></a></h2>
</section>
<section id="to-pdf">
<h2>To PDF<a class="headerlink" href="#to-pdf" title="Permalink to this headline"></a></h2>
<ul class="simple">
<li><p>Save in path</p></li>
</ul>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">doc</span> <span class="o">=</span> <span class="n">app</span><span class="o">.</span><span class="n">active</span>
<span class="n">path</span> <span class="o">=</span> <span class="s1">&#39;/home/mau/test.pdf&#39;</span>
<span class="n">doc</span><span class="o">.</span><span class="n">to_pdf</span><span class="p">(</span><span class="n">path</span><span class="p">)</span>
</pre></div>
</div>
<ul class="simple">
<li><p>Save in memory</p></li>
</ul>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">doc</span> <span class="o">=</span> <span class="n">app</span><span class="o">.</span><span class="n">active</span>
<span class="n">pdf</span> <span class="o">=</span> <span class="n">doc</span><span class="o">.</span><span class="n">to_pdf</span><span class="p">()</span>
</pre></div>
</div>
</section>
<section id="export">
<h2>Export<a class="headerlink" href="#export" title="Permalink to this headline"></a></h2>
</section>
<section id="call-dispatch">
<h2>Call dispatch<a class="headerlink" href="#call-dispatch" title="Permalink to this headline"></a></h2>
<p>You can call any <a class="reference external" href="https://wiki.documentfoundation.org/Development/DispatchCommands">dispatch command</a> used only if property or method no exists in original object or in <cite>easymacro.py</cite></p>

View File

@ -110,6 +110,7 @@
<li class="toctree-l2"><a class="reference internal" href="paths.html#open-any-type-file">Open any type file</a></li>
<li class="toctree-l2"><a class="reference internal" href="paths.html#save-and-read-json">Save and read json</a></li>
<li class="toctree-l2"><a class="reference internal" href="paths.html#save-and-read-csv">Save and read csv</a></li>
<li class="toctree-l2"><a class="reference internal" href="paths.html#zip-unzip">Zip, unzip</a></li>
<li class="toctree-l2"><a class="reference internal" href="paths.html#copy-files">Copy files</a></li>
</ul>
</li>
@ -125,6 +126,9 @@
<li class="toctree-l2"><a class="reference internal" href="application.html#get-by-name">Get by name</a></li>
<li class="toctree-l2"><a class="reference internal" href="application.html#new">New</a></li>
<li class="toctree-l2"><a class="reference internal" href="application.html#open">Open</a></li>
<li class="toctree-l2"><a class="reference internal" href="application.html#save">Save</a></li>
<li class="toctree-l2"><a class="reference internal" href="application.html#to-pdf">To PDF</a></li>
<li class="toctree-l2"><a class="reference internal" href="application.html#export">Export</a></li>
<li class="toctree-l2"><a class="reference internal" href="application.html#call-dispatch">Call dispatch</a></li>
</ul>
</li>

View File

@ -484,6 +484,44 @@
</pre></div>
</div>
</section>
<section id="zip-unzip">
<h2>Zip, unzip<a class="headerlink" href="#zip-unzip" title="Permalink to this headline"></a></h2>
<p>For default, save in the same directory with the same name + <cite>.zip</cite></p>
<ul class="simple">
<li><p>Zip file</p></li>
</ul>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">source</span> <span class="o">=</span> <span class="s1">&#39;/home/mau/logo.svg&#39;</span>
<span class="n">path_zip</span> <span class="o">=</span> <span class="n">app</span><span class="o">.</span><span class="n">paths</span><span class="o">.</span><span class="n">zip</span><span class="p">(</span><span class="n">source</span><span class="p">)</span>
<span class="n">app</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="n">path_zip</span><span class="p">)</span>
</pre></div>
</div>
<ul class="simple">
<li><p>Zip directory</p></li>
</ul>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">source</span> <span class="o">=</span> <span class="s1">&#39;/home/mau/test&#39;</span>
<span class="n">path_zip</span> <span class="o">=</span> <span class="n">app</span><span class="o">.</span><span class="n">paths</span><span class="o">.</span><span class="n">zip</span><span class="p">(</span><span class="n">source</span><span class="p">)</span>
<span class="n">app</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="n">path_zip</span><span class="p">)</span>
</pre></div>
</div>
<ul class="simple">
<li><p>Set target</p></li>
</ul>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">source</span> <span class="o">=</span> <span class="s1">&#39;/home/mau/logo.svg&#39;</span>
<span class="n">target</span> <span class="o">=</span> <span class="s1">&#39;/home/mau/myfile.zip&#39;</span>
<span class="n">path_zip</span> <span class="o">=</span> <span class="n">app</span><span class="o">.</span><span class="n">paths</span><span class="o">.</span><span class="n">zip</span><span class="p">(</span><span class="n">source</span><span class="p">,</span> <span class="n">target</span><span class="p">)</span>
<span class="n">app</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="n">path_zip</span><span class="p">)</span>
</pre></div>
</div>
<ul class="simple">
<li><p>Read content of zip</p></li>
</ul>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">path_zip</span> <span class="o">=</span> <span class="s1">&#39;/home/mau/test.zip&#39;</span>
<span class="n">names</span> <span class="o">=</span> <span class="n">app</span><span class="o">.</span><span class="n">paths</span><span class="o">.</span><span class="n">zip_content</span><span class="p">(</span><span class="n">path_zip</span><span class="p">)</span>
<span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="n">names</span><span class="p">:</span>
<span class="n">app</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="n">n</span><span class="p">)</span>
</pre></div>
</div>
</section>
<section id="copy-files">
<h2>Copy files<a class="headerlink" href="#copy-files" title="Permalink to this headline"></a></h2>
<p>Copy with the same name</p>

File diff suppressed because one or more lines are too long

View File

@ -142,6 +142,34 @@ With arguments.
doc = app.docs.open(path, args)
Save
^^^^
To PDF
^^^^^^
* Save in path
.. code-block:: python
doc = app.active
path = '/home/mau/test.pdf'
doc.to_pdf(path)
* Save in memory
.. code-block:: python
doc = app.active
pdf = doc.to_pdf()
Export
^^^^^^
Call dispatch
^^^^^^^^^^^^^

View File

@ -508,6 +508,47 @@ You can used the same way that `python csv`_
app.msgbox(data)
Zip, unzip
^^^^^^^^^^
For default, save in the same directory with the same name + `.zip`
* Zip file
.. code-block:: python
source = '/home/mau/logo.svg'
path_zip = app.paths.zip(source)
app.debug(path_zip)
* Zip directory
.. code-block:: python
source = '/home/mau/test'
path_zip = app.paths.zip(source)
app.debug(path_zip)
* Set target
.. code-block:: python
source = '/home/mau/logo.svg'
target = '/home/mau/myfile.zip'
path_zip = app.paths.zip(source, target)
app.debug(path_zip)
* Read content of zip
.. code-block:: python
path_zip = '/home/mau/test.zip'
names = app.paths.zip_content(path_zip)
for n in names:
app.debug(n)
Copy files
^^^^^^^^^^

View File

@ -101,6 +101,8 @@ from com.sun.star.awt.grid import XGridDataListener
from com.sun.star.awt.grid import XGridSelectionListener
from com.sun.star.script import ScriptEventDescriptor
from com.sun.star.io import IOException, XOutputStream
# ~ https://api.libreoffice.org/docs/idl/ref/namespacecom_1_1sun_1_1star_1_1awt_1_1FontUnderline.html
from com.sun.star.awt import FontUnderline
from com.sun.star.style.VerticalAlignment import TOP, MIDDLE, BOTTOM
@ -1201,21 +1203,31 @@ class LODocument(object):
"""
https://wiki.documentfoundation.org/Macros/Python_Guide/PDF_export_filter_data
"""
path_pdf = path
stream = None
path_pdf = 'private:stream'
if path:
path_pdf = _P.to_url(path)
filter_name = '{}_pdf_Export'.format(self.type)
filter_data = dict_to_property(args, True)
args = {
'FilterName': filter_name,
'FilterData': filter_data,
}
if not path:
stream = IOStream.output()
args['OutputStream'] = stream
opt = dict_to_property(args)
try:
self.obj.storeToURL(_P.to_url(path), opt)
self.obj.storeToURL(path_pdf, opt)
except Exception as e:
error(e)
path_pdf = ''
return _P.exists(path_pdf)
if not stream is None:
stream = stream.buffer
return stream
def export(self, path: str, ext: str='', args: dict={}):
if not ext:
@ -3965,7 +3977,6 @@ class LOBasic(LODocument):
class LODocs(object):
_desktop = None
# ~ 'private:stream'
def __init__(self):
self._desktop = get_desktop()
@ -6553,7 +6564,7 @@ class Paths(object):
return
@classmethod
def zip(cls, source, target='', pwd=''):
def zip(cls, source, target=''):
path_zip = target
if not isinstance(source, (tuple, list)):
path, _, name, _ = _P(source).info
@ -6572,7 +6583,7 @@ class Paths(object):
with zipfile.ZipFile(path_zip, 'w', compression=compression) as z:
for f in files:
z.write(f[0], f[1])
return
return path_zip
@classmethod
def zip_content(cls, path):
@ -6591,7 +6602,7 @@ class Paths(object):
if isinstance(members, str):
members = (members,)
z.extractall(path, members=members, pwd=pwd)
return True
return
@classmethod
def merge_zip(cls, target, zips):
@ -6642,6 +6653,27 @@ class Dates(object):
return d
class OutputStream(unohelper.Base, XOutputStream):
def __init__(self):
self._buffer = b''
self.closed = 0
@property
def buffer(self):
return self._buffer
def closeOutput(self):
self.closed = 1
def writeBytes(self, seq):
if seq.value:
self._buffer = seq.value
def flush(self):
pass
class IOStream(object):
@classmethod
@ -6655,6 +6687,10 @@ class IOStream(object):
stream.initialize((uno.ByteSequence(buffer.getvalue()),))
return stream
@classmethod
def output(cls):
return OutputStream()
@classmethod
def qr(cls, data, **kwargs):
import segno