Paid Request: Okular: Don’t lock file for overwrite

As an architect I make regular updates of the same PDF by overwriting the already opened file. Most PDF readers lock the file, so I have to close it first before I can overwrite, slowing down the process. The only reader I found so far that don’t lock the file (and is capable of loading huge vector drawings) is SumatraPDF. Unfortunate that app lacks a lot of other features and is not available on Linux.

Okular seems to have a feature to reload a changed files. But when I try to overwrite a file my drawing app saids the file is locked? I have only tried this on Windows 11.

Request: make Okular don’t lock the opened PDF file so we can overwrite it. So that Okular can reload the changed file. Right now this seems broken on Windows, and perhaps also on Linux.

I’m willing to donate 20,- euro to KDE or the person that fixes this.

Thanks in advance

I am not sure if this is related to the issue described above, but I use Okular with Kile (like many others) to compile LaTeX documents. Compiling the source code changes the PDF document which is already open in Okular and Okular seems to reload it automatically on my machine. So, it seems that Okular does permit modifying the PDF file while it is open in Okular. I’m on CachyOS KDE, updated 2 days ago. I can’t tell my Okular version right now, as I’m away from my machine.

Okular does not really lock the file but it might keep it open so that it doesn’t have to load everything into memory.

On Windows, when a file is opened, it can’t be deleted or written to.
There might be ways to work around this though.

No, on Linux a program has to specifically lock a file and that rarely happens.
Instead, most programs create a new (temporary) file and then “replace” the old file with the new one.

Thanks for your response! Great to hear it doesn’t lock the file on Linux. Unfortunate it works not that way on Windows. I asked Claude how it’s possible that SumatraPDF can open a pdf without ‘locking’ the response:

Based on what’s in the SumatraPDF repo and forum/discussion history, here’s the trick — and it’s less “trick” and more a deliberate design choice made in two parts.

1. It opens the file handle in shared mode (not exclusive)

Most PDF readers (Adobe Reader/Acrobat in particular) call Windows’ CreateFile() with a restrictive share mode, which is why Dropbox, LaTeX recompiles, or a second process can’t touch the file while it’s open — Adobe Reader DC reserves exclusive write-access when it opens a file under Windows.

SumatraPDF instead opens the handle with sharing flags that permit other processes to read, write, and even delete/rename the file concurrently (FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE in Win32 terms). Nothing on the OS side stops another process from overwriting the bytes on disk.

2. For smaller files, it reads the whole thing into memory and lets the handle go

This is the part that actually matters in practice, and it’s confirmed repeatedly by users and maintainers in the project’s own forum: SumatraPDF does not lock files under a size threshold (originally 10 MB, later raised to 32 MB in the 3.2+ pre-releases), so you can simply overwrite the file and it will refresh. A maintainer/community response frames this explicitly as intentional: it’s intentional and one of the reasons SumatraPDF is recommended in academic and publishing circles that PDFs up to 32 MB must not be locked, unlike Adobe products which need to be closed or killed to release the file.

Mechanically, this matches the classic technique used by editors/viewers designed for “hot reload” workflows (this is the same trick recommended as the standard workaround for Adobe’s locking, per the Adobe community thread): rather than keeping a live HANDLE/file mapping open into the PDF for the engine to stream from, the small file is slurped fully into a memory buffer at load time, the OS handle is closed (or never held with exclusive access to begin with), and rendering happens entirely from that in-memory copy. Since there’s no open handle asserting ownership of the file after the read completes, Windows has nothing to prevent LaTeX, Dropbox, git, or any other tool from truncating and rewriting the file on disk.

3. It watches the file for changes and reloads automatically

To make the “overwrite it externally” workflow actually useful (rather than just harmless), Sumatra pairs the non-locking read with a file watcher (src/utils/FileWatcher.cpp in the source tree), built on ReadDirectoryChangesW, that detects when the underlying file has changed on disk and reloads/re-renders it — which is exactly why LaTeX users can “compile → Sumatra auto-refreshes” without ever closing the viewer.

Why the size cutoff exists: for very large files, reading the whole thing into memory up front becomes expensive, so above the threshold Sumatra falls back to a more traditional streamed access pattern — and users on the forum confirm that above that size limit (e.g. 20 MB+ files in the versions tested), Windows does report the file as locked/in-use by SumatraPDF, just as with other readers. Even after closing individual tabs, some versions kept a lingering handle open on the folder due to caching/thumbnail-generation code paths, which the project treated as a bug and patched over several releases.

So there’s no filesystem-level exploit involved — it’s simply: read fully into RAM, don’t hold an exclusive/locking handle open, and use directory-change notifications to reload when the file underneath changes. That’s also precisely why it became the de facto standard PDF viewer for LaTeX/Markdown/Pandoc workflows on Windows.

Since Okular already reloads the file a lot of code already works the way like SumatraPDF. I asked why windows won’t work the same, fragment of the answer:

On Windows, the CRT’s default open mode (whether via fopen/_wfopen or unflagged CreateFile) typically grants FILE_SHARE_READ | FILE_SHARE_WRITE but not FILE_SHARE_DELETE. Overwriting the file’s contents in place would likely work fine, but the far more common pattern for regenerating a PDF — write to a temp file, delete the original, rename the temp file over it (what pdflatex, Ghostscript, LibreOffice, etc. do for atomicity) — needs FILE_SHARE_DELETEon every handle still open on that path, or the delete/rename step fails outright with ERROR_SHARING_VIOLATION.

I can confirm that my drawing app first deletes the file before making the new file with the same name.

So it seems it is not allowed to delete a in Okular opened file on windows.

It seems that: poppler/goo/gfile.cc at pdf-structure-extractor · opensoft/poppler · GitHub needs FILE_SHARE_DELETE added.

FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE

Please don’t throw slopgrenades into discussions.

I tested a two-part repair for the Windows delete/recreate workflow described here.

The Poppler sharing flags were already proposed by hawkeye0386 in Poppler MR2366, currently closed and unmerged. My new KCoreAddons MR609, submitted as jayenichols, adds Windows wide stat calls and Unicode file/directory lifecycle tests.

With matched source-built libraries, the official Windows Okular automatically reopened the replacement PDF and dispatched its second page for rendering in 12 replacement cycles across four combinations: ASCII and Unicode filenames with immediate or 2-second replacement delay, with three consecutive cycles per combination without restarting the viewer. This validation covers PDF reparse and render dispatch; completed pixel rendering and a production installer have not been verified. The new KCoreAddons MR is awaiting upstream review.

Source patches, test harnesses and results are in the public GitHub repository Gaviate/okular-windows-reload-validation. I’m offering this contribution for the workflow requested here.

There is now actual pixel evidence beyond the earlier render-dispatch check.

Four patched cases (ASCII/Unicode paths, immediate/2-second recreation) show the packaged Okular Part/PageView automatically reopening the replacement synthetic PDF and painting blue first-page fixture pixels. Only after that automatic refresh does an intentional goToPage(2) call show green second-page pixels. Four matching clean controls retain the original red page, with deletion error32 and recreation error80.

The probe source, eight case receipts, 20 raw viewport PNGs and hashes are published in the existing Gaviate/okular-windows-reload-validation repository, commit fe69918f4a87b058e02fddfda2b420648e126914.

This proves observed fixture-colored regions in the offscreen viewer component, not whole-page final completion, visible desktop output or a production installer. The earlier 12 repeated cycles remain separate. The Poppler sharing flags remain credited to hawkeye0386 MR2366; the KCoreAddons wide-stat repair remains MR609.

An experimental two-DLL Windows kit and its matching corresponding-source archive are now published in Releases of the existing Gaviate/okular-windows-reload-validation repository, tag okular-8118-windows-experimental-20261003.

It targets ONLY a disposable copy of the official okular-master-8118-windows-cl-msvc2022-x86_64.7z bundle (Qt6.11.1). The kit README/manifest gives strict original archive and DLL hashes plus manual backup/replace/restore steps. Both archives must be kept together; no installer or extra dependency DLLs are included.

The new full-feature Poppler matches all 3168 original export names and resolves the shipped C++/Qt6 imports. This exact pair passed one Unicode-path, 2-second recreation case in the packaged offscreen Part/PageView: deletion/recreation succeeded and blue replacement page-one fixture pixels appeared automatically before navigation. Green second-page pixels appeared only after explicit navigation. Earlier paired controls and repeated-cycle evidence are separate. Arbitrary drawings, whole-page final completion, visible desktop output and Linux remain untested.

The Poppler sharing flags remain credited to hawkeye0386 / MR2366; the new Unicode watcher repair remains KCoreAddons MR609.

Could you confirm your current Windows Okular build and whether trying this exact bundle on copies of your drawings fits your workflow? Other bundle versions are not covered by this experimental kit.