Skip to content

[17.0][ADD] attachment_preview_office: preview Office attachments via LibreOffice conversion - #635

Open
alan196 wants to merge 1 commit into
OCA:17.0from
Jarsa-dev:17.0-add-attachment_preview_office
Open

[17.0][ADD] attachment_preview_office: preview Office attachments via LibreOffice conversion#635
alan196 wants to merge 1 commit into
OCA:17.0from
Jarsa-dev:17.0-add-attachment_preview_office

Conversation

@alan196

@alan196 alan196 commented Aug 12, 2026

Copy link
Copy Markdown

Summary

New module that previews office documents (DOCX, XLSX, PPTX, DOC, XLS, PPT, ODT, ODS, ODP, ODG) in the browser — including chatter attachments — by converting them to PDF server-side with LibreOffice headless and rendering the result with Odoo's native PDF.js viewer.

Nothing leaves the server: no external viewer service (Microsoft Office Online, Google Docs) is involved, which makes it suitable for sensitive documents.

Context

  • attachment_preview was never migrated to 17.0 and its ViewerJS-based approach only covers PDF + ODF anyway.
  • [18.0][attachment_preview] Add Office format preview via LibreOffice conversion #603 adds Office-format preview to attachment_preview on 18.0 via LibreOffice conversion. This module brings the same conversion-endpoint logic to 17.0, but as a small standalone module: on 17.0 the native FileViewer already renders PDFs, so no ViewerJS or base module is needed.

How it works

  • HTTP endpoint GET /attachment_preview_office/to_pdf?model=&field=&id=&filename= converts the binary field content to PDF with libreoffice --headless --convert-to pdf (isolated UserInstallation profile per conversion, checksum-keyed disk cache under the filestore).
  • A JS patch on the mail Attachment model marks office extensions as previewable (isPdf) and routes defaultSource through the conversion endpoint wrapped in the native PDF.js viewer. The Download button still returns the original file.
  • Endpoint enforces: record read ACL, binary-field allow-list, extension allow-list, 50 MB size cap, 60 s conversion timeout. Returns 503 gracefully when LibreOffice is not installed.
  • No new Python dependencies (stdlib subprocess/tempfile only).

Test plan

  • 14 unit tests covering the conversion helper and every HTTP branch of the endpoint (LibreOffice is mocked, so tests pass on runners without it).
  • Manually validated on Odoo.sh (LibreOffice 7.3.7): xlsx/docx/pptx attachments preview correctly in the chatter.

@OCA-git-bot OCA-git-bot added series:17.0 mod:attachment_preview_office Module attachment_preview_office labels Aug 12, 2026
@alan196
alan196 force-pushed the 17.0-add-attachment_preview_office branch from 12b960e to 41623e2 Compare August 12, 2026 20:18
@alan196

alan196 commented Aug 12, 2026

Copy link
Copy Markdown
Author

Update: the preview button is now hidden when LibreOffice is not available on the server, instead of opening the viewer with an HTTP 503 error (the availability is exposed through session_info). Failed conversions now also log LibreOffice's stderr.

Note for reviewers: the runboat image (oca-ci) does not ship LibreOffice, so on runboat the preview button will simply not appear for office files — the feature itself cannot be demoed there. To test functionally, run locally with libreoffice in the PATH (apt-get install libreoffice), attach an .xlsx/.docx to any record with a chatter and click the attachment.

@alan196
alan196 force-pushed the 17.0-add-attachment_preview_office branch from 41623e2 to 20db219 Compare August 12, 2026 20:30
@alan196

alan196 commented Aug 12, 2026

Copy link
Copy Markdown
Author

Improved spreadsheet rendering: XLSX/XLS/ODS are now converted with LibreOffice's SinglePageSheets export option (calc_pdf_Export), so each sheet becomes one full-size PDF page instead of being cut into print-size pages — columns are no longer split and the layout matches the original sheet much more closely. The conversion cache key now includes the file extension since the export filter depends on it.

@alan196
alan196 force-pushed the 17.0-add-attachment_preview_office branch from 20db219 to 007cc34 Compare August 12, 2026 20:43
@alan196

alan196 commented Aug 12, 2026

Copy link
Copy Markdown
Author

Added a loading indicator for slow conversions: since the PDF.js iframe gives no feedback while the server converts a large document, the FileViewer now shows a spinner ("Generating preview… Large files can take several seconds.") and warms the conversion cache with a fetch before mounting the iframe, which then loads instantly from the cache. Includes Spanish translation.

@alan196

alan196 commented Aug 12, 2026

Copy link
Copy Markdown
Author

Compatibility fix for LibreOffice < 7.4: the CLI JSON filter-options syntax (pdf:calc_pdf_Export:{...}) is silently ignored by older LibreOffice (verified on 7.3.7 — spreadsheets came out paginated again). The SinglePageSheets option is now seeded into the isolated profile's registrymodifications.xcu instead, which works from LO 7.2 onward (verified on 7.3.7 and 25.8). The cache key gained a version component to invalidate PDFs converted before this fix.

@alan196
alan196 force-pushed the 17.0-add-attachment_preview_office branch from 007cc34 to bf5bf30 Compare August 12, 2026 21:09
…Office

Convert DOCX/XLSX/PPTX (and legacy/ODF) attachments to PDF server-side
with LibreOffice headless and render them with Odoo's native PDF.js
viewer, including chatter attachments. No external viewer service is
involved, so documents never leave the server. Converted PDFs are cached
on disk keyed by content checksum.

Conversion endpoint logic based on OCA#603 (18.0).
@alan196
alan196 force-pushed the 17.0-add-attachment_preview_office branch from bf5bf30 to b57e6e3 Compare August 13, 2026 00:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

mod:attachment_preview_office Module attachment_preview_office series:17.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants