PDF.js viewer in Joomla: links and iframes

WT PDF.js - Documentation

Each viewer includes its complete UI, local worker and resources.

Viewers for newer and older browsers

Each viewer includes its complete UI, local worker and resources. Choose a URL:

  • Newer browsers: /media/plg_system_wtpdfjs/viewer/modern/web/viewer.html.
  • Older browsers: /media/plg_system_wtpdfjs/viewer/legacy/web/viewer.html.

The parent page does not need useScript(): the viewer loads its own library in its window. The older-browser viewer still uses ES modules; this does not imply Internet Explorer support. Test your target browsers.

Pass the PDF in the file query parameter, the profile in config, and page/zoom after #. The example includes a link and an iframe; choose the appropriate UI. Uri::root(true) accounts for Joomla subdirectory installations. For older browsers, replace modern with legacy in the viewer URL.

PHP example

<?php
use Joomla\CMS\Uri\Uri;
$root = Uri::root(true);
$pdfUrl = $root . '/images/documents/catalog.pdf';
$viewerUrl = $root . '/media/plg_system_wtpdfjs/viewer/modern/web/viewer.html';
$query = http_build_query(
    ['file' => $pdfUrl, 'config' => 'catalog.json'],
    '', '&', PHP_QUERY_RFC3986
);
$src = $viewerUrl . '?' . $query . '#page=1&zoom=page-width';
$escapedSrc = htmlspecialchars($src, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
?>
<p><a href="<?php echo $escapedSrc; ?>" target="_blank" rel="noopener">PDF</a></p>
<iframe src="<?php echo $escapedSrc; ?>" title="PDF document"
    loading="lazy" style="width:100%;height:75vh;border:0" allowfullscreen>
</iframe>

WebTolk Joomla Extensions

113 Extensions
12 Categories
582 Versions released
893721 Downloads