Newsletter
Join the Community
Subscribe to our newsletter for the latest news and updates
A Hugo shortcode that embeds PDF files in pages using Mozilla's PDF.js library.
Hugo Embed PDF Shortcode is a Hugo shortcode that renders a PDF file inline in any markdown page of a Hugo-based website. It takes a relative URL pointing to a local PDF file and outputs an embeddable PDF viewer built with Mozilla's PDF.js library, letting visitors read the document without leaving the page. The shortcode is developed by Anvith KS and distributed under the MIT license.
Hugo Embed PDF Shortcode is a shortcode for the Hugo static site generator that allows you to embed a PDF document directly into a markdown page. It uses Mozilla's PDF.js library to render the PDF in the visitor's browser, with no additional plugins. The shortcode accepts a few parameters to control pagination, page startup, and the loading spinner, and it can be installed either as a Git submodule or by copying the shortcode and PDF.js files into your Hugo project.
url parameter; e.g. {{< embed-pdf url="./path/to/file.pdf" >}}.hidePaginator (boolean, hides pagination controls), renderPageNum (integer, opens on a specified page number), and hideLoader (boolean, hides the loading spinner).static/js/pdf-js files into your website.embed-pdf.html file and updating the worker path.url parameter in any markdown file, and the PDF viewer will appear at that exact spot.renderPageNum="5" to open a long document on page 5 on first load, which is useful for multi-page reports.hidePaginator="true" to hide pagination controls for a one-page handout, and hideLoader="true" to suppress the spinner for small, quick-loading files.The shortcode outputs an HTML container plus script tags that load PDF.js from a CDN. When a visitor opens the page, PDF.js fetches the PDF file specified by the url parameter, renders it onto a canvas, and displays pagination controls. The worker script is configured via pdfjsLib.GlobalWorkerOptions.workerSrc to support rendering in a background thread. The page also documents how to switch from the CDN to a self-hosted copy of PDF.js.
Currently it supports local file embed. If you provide an absolute URL from a remote server, that server must be configured with the correct CORS headers, otherwise the browser will block the PDF request.
The current version loads [email protected] from a CDN (https://cdn.jsdelivr.net/npm/[email protected]/build/pdf.min.js). The README includes instructions to replace the CDN script with a local copy by editing the embed-pdf.html file.
Yes. Set hidePaginator="true" to remove pagination controls and hideLoader="true" to hide the loading spinner while the document loads.
Yes, the project is released under the MIT license, so it can be used, modified, and redistributed without cost.
The shortcode accepts four parameters: a required url that points to the PDF file, and three optional parameters — hidePaginator, renderPageNum, and hideLoader.
You can try a live example of the shortcode at the online demo: https://hugo-embed-pdf.netlify.app/.
