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
36 changes: 25 additions & 11 deletions attachment_preview/README.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
.. image:: https://odoo-community.org/readme-banner-image
:target: https://odoo-community.org/get-involved?utm_source=readme
:alt: Odoo Community Association

===================
Preview attachments
===================
Expand All @@ -17,7 +13,7 @@ Preview attachments
.. |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/license-AGPL--3-blue.png
.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
:alt: License: AGPL-3
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fknowledge-lightgray.png?logo=github
Expand All @@ -32,10 +28,14 @@ Preview attachments

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

This addon allows to preview attachments supported by
http://viewerjs.org.
This addon previews attachments and binary fields directly in Odoo's
backend.

Currently, that's most Libreoffice files and PDFs.
PDF files are rendered with Odoo's native PDF.js viewer. Office
documents — both ODF (ODT/ODS/ODP) and OOXML (DOCX/XLSX/PPTX), plus
legacy DOC/XLS/PPT — are converted to PDF on the server with LibreOffice
and then rendered through that same native viewer, so spreadsheets and
slide decks can be previewed without leaving Odoo.

|Screenshot of split form view|

Expand All @@ -49,10 +49,24 @@ Currently, that's most Libreoffice files and PDFs.
Installation
============

For filetype recognition, you'll get the best results by installing
``python-magic``:
Office-document preview (DOCX, XLSX, PPTX, ODT, ODS, ODP, and legacy
formats) requires **LibreOffice** to be installed on the Odoo server —
it is used in headless mode to convert documents to PDF. It is *not*
part of the standard Odoo image, so install it on the host/container
running Odoo:

::

sudo apt-get install libreoffice

Without LibreOffice the office-preview endpoint returns HTTP 503 and
only PDF files can be previewed. PDF preview needs no extra packages.

For best filetype recognition, also install ``python-magic``:

::

sudo apt-get install python-magic
sudo apt-get install python-magic

Usage
=====
Expand Down
2 changes: 1 addition & 1 deletion attachment_preview/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2014 Therp BV (<http://therp.nl>)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from . import models
from . import controllers, models
2 changes: 1 addition & 1 deletion attachment_preview/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"author": "Therp BV," "Onestein," "Odoo Community Association (OCA)",
"website": "https://github.com/OCA/knowledge",
"license": "AGPL-3",
"summary": "Preview attachments supported by Viewer.js",
"summary": "Preview attachments (PDF + office) via the native PDF.js viewer",
"category": "Knowledge Management",
"depends": ["web", "mail"],
"data": [],
Expand Down
4 changes: 4 additions & 0 deletions attachment_preview/controllers/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Copyright 2026 Ledoweb
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from . import main
183 changes: 183 additions & 0 deletions attachment_preview/controllers/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
# Copyright 2026 Ledoweb
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
"""LibreOffice-based conversion endpoint for office-document preview.

Converts office documents (ODF *and* OOXML: ODT/ODS/ODP, DOCX/XLSX/PPTX, legacy
DOC/XLS/PPT, ODG) to PDF so they can be rendered by Odoo's native PDF.js viewer.
LibreOffice headless must be installed; if it is absent the endpoint returns
HTTP 503.
"""

import base64
import hashlib
import logging
import os
import subprocess
import tempfile

from odoo import http, tools
from odoo.http import request

_logger = logging.getLogger(__name__)

# Office formats LibreOffice can convert to PDF (ODF + OOXML + legacy binary).
OFFICE_EXTENSIONS = frozenset(
{"docx", "xlsx", "pptx", "doc", "xls", "ppt", "odt", "ods", "odp", "odg"}
)

# Reject documents larger than this before spawning a (slow) conversion.
MAX_CONTENT_BYTES = 50 * 1024 * 1024 # 50 MB
# Per-conversion wall-clock budget (seconds).
CONVERT_TIMEOUT = 60


class AttachmentPreviewOfficeController(http.Controller):
@http.route(
"/attachment_preview/office_to_pdf",
type="http",
auth="user",
methods=["GET"],
)
def office_to_pdf( # pylint: disable=redefined-builtin
self, model, field, id, filename="file", **kwargs
):
"""Convert a binary field's office document to PDF for preview.

Query params:
model – Odoo model name (e.g. 'ir.attachment')
field – binary field name (e.g. 'datas')
id – record id (integer)
filename – original filename (used to derive the extension)
"""
try:
record_id = int(id)
except (TypeError, ValueError):
return request.make_response("Bad request", status=400)

# Extension allow-list (on the claimed filename) before touching the DB.
ext = os.path.splitext(filename)[-1].lstrip(".").lower()
if ext not in OFFICE_EXTENSIONS:
return request.make_response(
"Extension not supported for conversion", status=415
)

record = request.env[model].browse(record_id).exists()
if not record:
return request.make_response("Not found", status=404)
# Record-level ACL: raises AccessError (-> 403) if the user can't read.
record.check_access("read")

# The field must exist on the model AND be a binary field — never read
# an arbitrary attribute supplied in the query string.
model_field = record._fields.get(field)
if model_field is None or model_field.type != "binary":
return request.make_response("Invalid field", status=400)

raw = record[field]
if not raw:
return request.make_response("No content", status=404)

content = base64.b64decode(raw)
if len(content) > MAX_CONTENT_BYTES:
return request.make_response("Document too large to preview", status=413)

pdf_bytes = self._get_pdf_cached(content, ext)
if pdf_bytes is None:
return request.make_response(
"LibreOffice not available — cannot convert document", status=503
)

return request.make_response(
pdf_bytes,
headers=[
("Content-Type", "application/pdf"),
(
"Content-Disposition",
f'inline; filename="{os.path.splitext(filename)[0]}.pdf"',
),
("Cache-Control", "private, max-age=3600"),
],
)

# -- conversion + cache ----------------------------------------------------

def _get_pdf_cached(self, content, ext):
"""Return converted PDF bytes, using a checksum-keyed disk cache.

The cache lives under the Odoo data_dir so it is shared across workers
on the same host; identical documents are converted only once. All cache
I/O is best-effort: a miss or any filesystem error falls back silently to
a fresh conversion.
"""
key = hashlib.sha256(content).hexdigest()
cache_path = self._cache_path(key)
if cache_path and os.path.exists(cache_path):
try:
with open(cache_path, "rb") as fh:
return fh.read()
except OSError:
_logger.warning("attachment_preview: cache read failed", exc_info=True)

pdf_bytes = self._libreoffice_to_pdf(content, ext)
if pdf_bytes and cache_path:
try:
# Atomic publish so a concurrent reader never sees a partial file.
tmp = f"{cache_path}.{os.getpid()}.tmp"
with open(tmp, "wb") as fh:
fh.write(pdf_bytes)
os.replace(tmp, cache_path)
except OSError:
_logger.warning("attachment_preview: cache write failed", exc_info=True)
return pdf_bytes

@staticmethod
def _cache_path(key):
"""Return the cache file path for ``key`` under the data_dir, or None."""
try:
base = os.path.join(
tools.config.filestore(request.env.cr.dbname),
"attachment_preview_cache",
)
os.makedirs(base, exist_ok=True)
return os.path.join(base, f"{key}.pdf")
except Exception: # pragma: no cover - never let caching break preview
return None

@staticmethod
def _libreoffice_to_pdf(content, ext):
"""Run LibreOffice headless conversion. Returns PDF bytes or None.

Each invocation uses an isolated ``UserInstallation`` profile so that
concurrent conversions do not collide on the shared LibreOffice profile
lock — the classic "another instance is already running" failure that
otherwise makes this endpoint flaky under any real load.
"""
try:
with tempfile.TemporaryDirectory() as tmpdir:
src = os.path.join(tmpdir, f"source.{ext}")
with open(src, "wb") as fh:
fh.write(content)
profile = os.path.join(tmpdir, "louser")
result = subprocess.run(
[
"libreoffice",
f"-env:UserInstallation=file://{profile}",
"--headless",
"--convert-to",
"pdf",
"--outdir",
tmpdir,
src,
],
timeout=CONVERT_TIMEOUT,
capture_output=True,
)
if result.returncode != 0:
return None
pdf_path = os.path.join(tmpdir, "source.pdf")
if not os.path.exists(pdf_path):
return None
with open(pdf_path, "rb") as fh:
return fh.read()
except (FileNotFoundError, subprocess.TimeoutExpired, OSError):
return None
9 changes: 6 additions & 3 deletions attachment_preview/readme/DESCRIPTION.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
This addon allows to preview attachments supported by
<http://viewerjs.org>.
This addon previews attachments and binary fields directly in Odoo's backend.

Currently, that's most Libreoffice files and PDFs.
PDF files are rendered with Odoo's native PDF.js viewer. Office documents — both
ODF (ODT/ODS/ODP) and OOXML (DOCX/XLSX/PPTX), plus legacy DOC/XLS/PPT — are
converted to PDF on the server with LibreOffice and then rendered through that
same native viewer, so spreadsheets and slide decks can be previewed without
leaving Odoo.

![Screenshot of split form view](/attachment_preview/static/description/screenshot-split.png)
15 changes: 12 additions & 3 deletions attachment_preview/readme/INSTALL.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
For filetype recognition, you'll get the best results by installing
`python-magic`:
Office-document preview (DOCX, XLSX, PPTX, ODT, ODS, ODP, and legacy formats)
requires **LibreOffice** to be installed on the Odoo server — it is used in
headless mode to convert documents to PDF. It is *not* part of the standard Odoo
image, so install it on the host/container running Odoo:

sudo apt-get install python-magic
sudo apt-get install libreoffice

Without LibreOffice the office-preview endpoint returns HTTP 503 and only PDF
files can be previewed. PDF preview needs no extra packages.

For best filetype recognition, also install `python-magic`:

sudo apt-get install python-magic
7 changes: 7 additions & 0 deletions attachment_preview/readme/newsfragments/603.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Add office-document preview (DOCX, XLSX, PPTX, legacy DOC/XLS/PPT, and ODF
formats) by converting them to PDF with LibreOffice headless and rendering the
result through Odoo's native PDF.js viewer. ViewerJS is no longer bundled. The
conversion endpoint validates the binary field, caps document size, caches
results by checksum, and runs each LibreOffice conversion with an isolated user
profile so concurrent previews do not collide. Returns HTTP 503 gracefully when
LibreOffice is not installed.
28 changes: 11 additions & 17 deletions attachment_preview/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="Docutils: https://docutils.sourceforge.io/" />
<title>README.rst</title>
<title>Preview attachments</title>
<style type="text/css">

/*
Expand Down Expand Up @@ -360,21 +360,16 @@
</style>
</head>
<body>
<div class="document">
<div class="document" id="preview-attachments">
<h1 class="title">Preview attachments</h1>


<a class="reference external image-reference" href="https://odoo-community.org/get-involved?utm_source=readme">
<img alt="Odoo Community Association" src="https://odoo-community.org/readme-banner-image" />
</a>
<div class="section" id="preview-attachments">
<h1>Preview attachments</h1>
<!-- !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:badc7aad1a4bee3f65173d08f56b91d13166d0c26644edf8475aa249016995e1
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
<p><a class="reference external image-reference" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external image-reference" href="http://www.gnu.org/licenses/agpl-3.0-standalone.html"><img alt="License: AGPL-3" src="https://img.shields.io/badge/license-AGPL--3-blue.png" /></a> <a class="reference external image-reference" href="https://github.com/OCA/knowledge/tree/18.0/attachment_preview"><img alt="OCA/knowledge" src="https://img.shields.io/badge/github-OCA%2Fknowledge-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/knowledge-18-0/knowledge-18-0-attachment_preview"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external image-reference" href="https://runboat.odoo-community.org/builds?repo=OCA/knowledge&amp;target_branch=18.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
<p><a class="reference external image-reference" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external image-reference" href="http://www.gnu.org/licenses/agpl-3.0-standalone.html"><img alt="License: AGPL-3" src="https://img.shields.io/badge/licence-AGPL--3-blue.png" /></a> <a class="reference external image-reference" href="https://github.com/OCA/knowledge/tree/18.0/attachment_preview"><img alt="OCA/knowledge" src="https://img.shields.io/badge/github-OCA%2Fknowledge-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/knowledge-18-0/knowledge-18-0-attachment_preview"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external image-reference" href="https://runboat.odoo-community.org/builds?repo=OCA/knowledge&amp;target_branch=18.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
<p>This addon allows to preview attachments supported by
<a class="reference external" href="http://viewerjs.org">http://viewerjs.org</a>.</p>
<p>Currently, that’s most Libreoffice files and PDFs.</p>
Expand All @@ -394,13 +389,13 @@ <h1>Preview attachments</h1>
</ul>
</div>
<div class="section" id="installation">
<h2><a class="toc-backref" href="#toc-entry-1">Installation</a></h2>
<h1><a class="toc-backref" href="#toc-entry-1">Installation</a></h1>
<p>For filetype recognition, you’ll get the best results by installing
<tt class="docutils literal"><span class="pre">python-magic</span></tt>:</p>
<p>sudo apt-get install python-magic</p>
</div>
<div class="section" id="usage">
<h2><a class="toc-backref" href="#toc-entry-2">Usage</a></h2>
<h1><a class="toc-backref" href="#toc-entry-2">Usage</a></h1>
<p>The module adds a little print preview icon right of download links for
attachments or binary fields. When a preview is opened from the
attachments menu it’s shown next to the form view. From this screen you
Expand All @@ -410,31 +405,31 @@ <h2><a class="toc-backref" href="#toc-entry-2">Usage</a></h2>
<p><img alt="Screenshot navigator" src="https://raw.githubusercontent.com/attachment_preview/static/description/screenshot-paginator.png" /></p>
</div>
<div class="section" id="bug-tracker">
<h2><a class="toc-backref" href="#toc-entry-3">Bug Tracker</a></h2>
<h1><a class="toc-backref" href="#toc-entry-3">Bug Tracker</a></h1>
<p>Bugs are tracked on <a class="reference external" href="https://github.com/OCA/knowledge/issues">GitHub Issues</a>.
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
<a class="reference external" href="https://github.com/OCA/knowledge/issues/new?body=module:%20attachment_preview%0Aversion:%2018.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**">feedback</a>.</p>
<p>Do not contact contributors directly about support or help with technical issues.</p>
</div>
<div class="section" id="credits">
<h2><a class="toc-backref" href="#toc-entry-4">Credits</a></h2>
<h1><a class="toc-backref" href="#toc-entry-4">Credits</a></h1>
<div class="section" id="authors">
<h3><a class="toc-backref" href="#toc-entry-5">Authors</a></h3>
<h2><a class="toc-backref" href="#toc-entry-5">Authors</a></h2>
<ul class="simple">
<li>Therp BV</li>
<li>Onestein</li>
</ul>
</div>
<div class="section" id="contributors">
<h3><a class="toc-backref" href="#toc-entry-6">Contributors</a></h3>
<h2><a class="toc-backref" href="#toc-entry-6">Contributors</a></h2>
<ul class="simple">
<li>Holger Brunn &lt;<a class="reference external" href="mailto:mail&#64;hunki-enterprises.com">mail&#64;hunki-enterprises.com</a>&gt;</li>
<li>Dennis Sluijk &lt;<a class="reference external" href="mailto:d.sluijk&#64;onestein.nl">d.sluijk&#64;onestein.nl</a>&gt;</li>
</ul>
</div>
<div class="section" id="maintainers">
<h3><a class="toc-backref" href="#toc-entry-7">Maintainers</a></h3>
<h2><a class="toc-backref" href="#toc-entry-7">Maintainers</a></h2>
<p>This module is maintained by the OCA.</p>
<a class="reference external image-reference" href="https://odoo-community.org">
<img alt="Odoo Community Association" src="https://odoo-community.org/logo.png" />
Expand All @@ -447,6 +442,5 @@ <h3><a class="toc-backref" href="#toc-entry-7">Maintainers</a></h3>
</div>
</div>
</div>
</div>
</body>
</html>
Loading
Loading