Skip to content

๐Ÿงฉ Embedding PDFs in MkDocs with PDF.js

This guide explains how to install and use PDF.js to display PDFs inline in your MkDocs site using a clean viewer โ€” without relying on browser rendering.


โœ… Folder Layout

Assume your docs/ folder has:

docs/
โ”œโ”€โ”€ assets/
โ”‚   โ”œโ”€โ”€ pdfs/             โ† put your PDFs here
โ”‚   โ””โ”€โ”€ pdfjs/            โ† we will install PDF.js here
โ”‚       โ”œโ”€โ”€ build/
โ”‚       โ””โ”€โ”€ web/
โ”‚           โ””โ”€โ”€ viewer.html

๐Ÿ“ฅ Step-by-Step Installation Instructions (WSL2 or Windows)

1. Download PDF.js

You will see:

pdfjs/
โ”œโ”€โ”€ build/
โ”œโ”€โ”€ web/


2. Copy Required Files into MkDocs

From the extracted folder, copy into your project:

# Inside your project root
mkdir -p docs/assets/pdfjs
cp -r ~/Downloads/pdfjs-*/build docs/assets/pdfjs/
cp -r ~/Downloads/pdfjs-*/web docs/assets/pdfjs/

Make sure the following file exists:

docs/assets/pdfjs/web/viewer.html


3. Place Your PDFs

Put your PDF files in:

docs/assets/pdfs/

For example:

docs/assets/pdfs/Aluminum_Etch_SOP.pdf


๐Ÿงช Test the Viewer in Browser

Start your MkDocs dev server:

mkdocs serve

Then test these URLs:

  • Viewer:
    http://127.0.0.1:8000/assets/pdfjs/web/viewer.html

  • Direct PDF:
    http://127.0.0.1:8000/assets/pdfs/Aluminum_Etch_SOP.pdf

  • Viewer with embedded PDF:
    http://127.0.0.1:8000/assets/pdfjs/web/viewer.html?file=/assets/pdfs/Aluminum_Etch_SOP.pdf


โœ… Embed in Markdown

Use this code block in any .md file:

<iframe src="/assets/pdfjs/web/viewer.html?file=/assets/pdfs/Aluminum_Etch_SOP.pdf"
        width="100%" height="800px" style="border: none;"></iframe>

โš™๏ธ Optional Viewer Tweaks

  • Hide sidebar:

    ?file=/...pdf#pagemode=none
    

  • Start on page 3:

    ?file=/...pdf#page=3
    

  • Zoom to 150%:

    ?file=/...pdf#zoom=150
    

Example:

<iframe src="/assets/pdfjs/web/viewer.html?file=/assets/pdfs/Aluminum_Etch_SOP.pdf#pagemode=none&zoom=100"
        width="100%" height="800px" style="border: none;"></iframe>


๐Ÿงผ GitHub Pages Notes

If you deploy using GitHub Pages, and your site URL is something like:

https://yourname.github.io/nanonotes/

Then you must prefix all paths with /nanonotes/, like:

<iframe src="/nanonotes/assets/pdfjs/web/viewer.html?file=/nanonotes/assets/pdfs/Aluminum_Etch_SOP.pdf"
        width="100%" height="800px" style="border: none;"></iframe>

Alternatively, in mkdocs.yml, set:

site_url: https://yourname.github.io/nanonotes

๐Ÿง  Troubleshooting

  • 404? โ†’ Check for exact filename, spaces, capitalization
  • Viewer loads but no PDF? โ†’ Wrong path in file=...
  • pdf.mjs not found? โ†’ You forgot to copy the build/ folder

End of guide.