DLM PDF.js System
The DLM PDF.js System is a pair of WordPress code snippets that work together to provide protected PDF viewing and unified file event counting for sites using the Download Monitor and PDF.js Viewer Shortcode plugins.
It solves three problems that Download Monitor does not handle natively:
- Protected PDF files managed by Download Monitor cannot be viewed inline — clicking a download link prompts a file download rather than opening a viewer.
- The PDF.js Viewer plugin cannot access Download Monitor’s protected file storage directly, making it impossible to use the two plugins together without a proxy layer.
- Download Monitor’s native download counter does not count PDF views (since PDFs bypass the normal download flow), and its counting features have not worked for me and some others as expressed in online support.
System Architecture
Click any component box to explore its detail diagram.
The [dlm_pdfjs] shortcode renders a plain HTML link. Clicking opens viewer.php in a new tab. PDF.js then requests the proxy URL, which verifies the download ID, resolves the file path from Download Monitor metadata, increments the counter, and streams the PDF. The real file path in dlm_uploads is never exposed to the visitor.
For ZIP files and any other non-PDF file type, the existing [download id=”x”] shortcode works unchanged. Download Monitor handles delivery natively and fires the dlm_downloading hook when done. The Proxy Counter snippet listens for this hook and increments the event counter automatically — no changes needed for any file type Download Monitor serves.
The live counter renders the current count server-side on first load so there is never a blank value while JavaScript initializes. A global PHP flag ensures the polling JavaScript is only loaded on pages that use a live shortcode — other pages get no extra overhead. The polling loop only updates the DOM when the value has actually changed, preventing unnecessary reflows.
The PDF.js Viewer Shortcode plugin provides the viewer.php endpoint the system relies on. viewer.php is used instead of viewer.html because newer versions of PDF.js bundle ES modules (.mjs files) which require correct MIME types — viewer.php handles this internally. No plugin configuration changes are needed; the system simply calls its viewer endpoint with the proxy URL as the file= parameter.
Download Monitor stores protected files in a non-public uploads directory, assigns a unique download ID to each file, and fires the dlm_downloading hook when a file is served. The Proxy Viewer snippet uses DLM metadata to resolve the real file path server-side. DLM’s native display counter is disabled since the Proxy Counter provides unified tracking across both PDFs and ZIPs.
Code Snippets executes the two PHP snippets within the WordPress environment. An important technical note: because Code Snippets runs snippets at an early load stage, WordPress transient functions are not yet available. This is why the counter uses a static PHP array combined with wp_cache (Redis) for duplicate-count prevention rather than the more common get_transient/set_transient pattern.
The Proxy Viewer snippet owns the complete PDF delivery pipeline. The [dlm_pdfjs] shortcode renders a link to viewer.php with the proxy URL as the file parameter. The proxy intercepts requests via template_redirect, runs security checks, resolves the file path through a 6-step metadata lookup, caches the result, increments the counter, and streams the PDF. The real file path in dlm_uploads is never exposed to the visitor.
The Proxy Counter is the shared counting engine for the entire system. It provides the core increment function called by both the PDF proxy and the ZIP download hook. The two-layer lock (static PHP array for within-request duplicates, Redis wp_cache for cross-request duplicates) prevents PDF.js byte-range requests from inflating counts. The admin panel under Downloads → DLM PDF.js Stats shows sortable stats with editable counts for restoring historical data.
