Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
beautifulsoup4
bokeh==3.1.1
mpld3==0.5.9
msoffice2pdf
plotly==5.13.1
6 changes: 6 additions & 0 deletions setup/web_preview_ms_office/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import setuptools

setuptools.setup(
setup_requires=['setuptools-odoo'],
odoo_addon=True,
)
109 changes: 109 additions & 0 deletions web_preview_ms_office/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
=================
Preview MS Office
=================

..
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:95dca921a936f35530178c093fe48f1326b5096651e1f9d6da4ab494dcb6b614
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
:target: https://odoo-community.org/page/development-status
:alt: Beta
.. |badge2| image:: https://img.shields.io/badge/licence-LGPL--3-blue.png
:target: http://www.gnu.org/licenses/lgpl-3.0-standalone.html
:alt: License: LGPL-3
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fweb-lightgray.png?logo=github
:target: https://github.com/OCA/web/tree/16.0/web_preview_ms_office
:alt: OCA/web
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
:target: https://translation.odoo-community.org/projects/web-16-0/web-16-0-web_preview_ms_office
:alt: Translate me on Weblate
.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png
:target: https://runboat.odoo-community.org/builds?repo=OCA/web&target_branch=16.0
:alt: Try me on Runboat

|badge1| |badge2| |badge3| |badge4| |badge5|

This module provides a preview of Microsoft Office documents in Odoo. It
allows users to view documents such as Word, Excel, and PowerPoint files
directly within the Odoo interface without needing to download them.

**Table of contents**

.. contents::
:local:

Installation
============

You need to install the python msoffice2pdf library:

::

pip install msoffice2pdf

Usage
=====

1. Install the module in your Odoo instance.

2. Add field in your model to include the ``attachment_ids`` field.

.. code:: python

attachment_ids = fields.Many2Many('ir.attachment', string='Attachments')

3. In your xml file, include the ``many2many_binary`` widget:

.. code:: xml

<field name="attachment_ids" widget="many2many_binary"/>

4. The MS Office file will be displayed in the form view of the model
where you added the ``attachment_ids`` field.

Bug Tracker
===========

Bugs are tracked on `GitHub Issues <https://github.com/OCA/web/issues>`_.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us to smash it by providing a detailed and welcomed
`feedback <https://github.com/OCA/web/issues/new?body=module:%20web_preview_ms_office%0Aversion:%2016.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.

Do not contact contributors directly about support or help with technical issues.

Credits
=======

Authors
-------

* Phan Hong Phuc

Contributors
------------

- `Trobz <https://trobz.com>`__

- Phan Hong Phuc phucph@trobz.com

Maintainers
-----------

This module is maintained by the OCA.

.. image:: https://odoo-community.org/logo.png
:alt: Odoo Community Association
:target: https://odoo-community.org

OCA, or the Odoo Community Association, is a nonprofit organization whose
mission is to support the collaborative development of Odoo features and
promote its widespread use.

This module is part of the `OCA/web <https://github.com/OCA/web/tree/16.0/web_preview_ms_office>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
2 changes: 2 additions & 0 deletions web_preview_ms_office/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import controllers
from . import models
24 changes: 24 additions & 0 deletions web_preview_ms_office/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "Preview MS Office",
"version": "16.0.1.0.0",
"summary": "Preview MS Office Documents in Odoo",
"category": "Tools",
"website": "https://github.com/OCA/web",
"maintainer": "Phan Hong Phuc",
"author": "Phan Hong Phuc, Odoo Community Association (OCA)",
"license": "LGPL-3",
"depends": ["web_preview_attachment_base"],
"external_dependencies": {"python": ["msoffice2pdf"]},
"assets": {
"web.assets_backend": [
"web_preview_ms_office/static/src/attachment/attachment_viewer_viewable.js",
"web_preview_ms_office/static/src/attachment/attachment_viewer.xml",
"web_preview_ms_office/static/src/attachment/attachment.js",
"web_preview_ms_office/static/src/many2many_binary/attachment_preview.js",
],
},
"installable": True,
"application": False,
"images": ["static/description/icon.png"],
"auto_install": False,
}
1 change: 1 addition & 0 deletions web_preview_ms_office/controllers/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import main
35 changes: 35 additions & 0 deletions web_preview_ms_office/controllers/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import base64

from odoo import http
from odoo.http import request


class OfficeConvertController(http.Controller):
@http.route("/preview-msoffice/<int:attachment_id>", auth="user")
def convert_pdf_attachment(self, attachment_id):
Attachment = request.env["ir.attachment"].sudo()
attachment = Attachment.browse(attachment_id)

headers = [
("Content-Type", "application/pdf"),
("Content-Disposition", 'inline; filename="converted_attachment.pdf"'),
]

if not attachment.exists() or not attachment.datas:
return request.make_response(
"Attachment not found or empty.",
headers=[("Content-Type", "text/plain")],
)
try:
attachment_bytes = base64.b64decode(attachment.datas)
pdf_data = request.env["converter.msoffice2pdf"].convert_msoffice2pdf(
binary_data=attachment_bytes, filename=attachment.name
)
return request.make_response(
pdf_data, headers=headers + [("Content-Length", str(len(pdf_data)))]
)
except Exception as e:
return request.make_response(
f"Error preview attachment: {str(e)}",
headers=[("Content-Type", "text/plain")],
)
1 change: 1 addition & 0 deletions web_preview_ms_office/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import convert_msoffice2pdf
40 changes: 40 additions & 0 deletions web_preview_ms_office/models/convert_msoffice2pdf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import logging
import os
import tempfile

from odoo import models

_logger = logging.getLogger(__name__)

try:
from msoffice2pdf import convert
except (OSError, ImportError) as err:
_logger.debug(err)


class ConverterOffice2PDF(models.AbstractModel):
_name = "converter.msoffice2pdf"

def convert_msoffice2pdf(self, binary_data, filename):
with tempfile.TemporaryDirectory() as tmpdir:
input_path = os.path.join(tmpdir, filename)
output = ""
with open(input_path, "wb") as f:
f.write(binary_data)
try:
output = convert(source=input_path, output_dir=tmpdir, soft=1)
with open(output, "rb") as f:
pdf_data = f.read()
return pdf_data

except Exception as e:
raise ValueError(
f"Error converting file {filename} to PDF: {str(e)}"
) from e

finally:
# Clean up the temporary files
if os.path.exists(input_path):
os.remove(input_path)
if os.path.exists(output):
os.remove(output)
3 changes: 3 additions & 0 deletions web_preview_ms_office/readme/CONTRIBUTORS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
* [Trobz](https://trobz.com)

* Phan Hong Phuc <phucph@trobz.com>
2 changes: 2 additions & 0 deletions web_preview_ms_office/readme/DESCRIPTION.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
This module provides a preview of Microsoft Office documents in Odoo.
It allows users to view documents such as Word, Excel, and PowerPoint files directly within the Odoo interface without needing to download them.
3 changes: 3 additions & 0 deletions web_preview_ms_office/readme/INSTALL.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
You need to install the python msoffice2pdf library:

pip install msoffice2pdf
13 changes: 13 additions & 0 deletions web_preview_ms_office/readme/USAGE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
1. Install the module in your Odoo instance.

2. Add field in your model to include the `attachment_ids` field.
```python
attachment_ids = fields.Many2Many('ir.attachment', string='Attachments')
```

3. In your xml file, include the `many2many_binary` widget:
```xml
<field name="attachment_ids" widget="many2many_binary"/>
```

4. The MS Office file will be displayed in the form view of the model where you added the `attachment_ids` field.
Binary file added web_preview_ms_office/static/description/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading