Skip to content

Instantly share code, notes, and snippets.

@sprklinginfo
Last active March 28, 2024 01:16
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sprklinginfo/624c4c431a0f802556071ec459b6ca76 to your computer and use it in GitHub Desktop.
Save sprklinginfo/624c4c431a0f802556071ec459b6ca76 to your computer and use it in GitHub Desktop.
Online PDF Viewer for BookStack App

Setup

Download PDFJS prebuilt version from https://mozilla.github.io/pdf.js/getting_started/#download

Unzip the file to 'pdfjs' folder and move it to the BookStack app 'public' folder. Ensure both 'build' and 'web' folders and its files are there.
pdfjs-folder

Copy and paste the following code to Custom HTML Head Content field on Settings page:

<script src="//cdn.jsdelivr.net/npm/sweetalert2@10"></script>
<style>
  button[data-pdfurl]{
    background-color: lightgrey;
    border: 1px solid #000000;
    border-radius: 5px;
  }
</style>
<script type="text/javascript">
var createButton = function(text,details,id,callback) {
  var btnWrapper = document.createElement('div');
  btnWrapper.id = id;
  btnWrapper.className='mce-widget mce-btn';
  btnWrapper.tabindex=-1;
  btnWrapper.role='button';
  btnWrapper.ariaLabel=details;
  var btn = document.createElement('button');
  btn.id=id+'-button';
  btn.innerText = text;
  btn.role='presentation';
  btn.type='button';
  btn.tabindex='-1';
  btn.style.border = 'solid 1px';
  btn.style.padding = ' 4px 8px';
  btn.style.margin = ' 2px';
  btnWrapper.append(btn);
  btnWrapper.onclick = callback;
  var ar =document.querySelectorAll('.mce-btn.mce-last');
  var lastBtn = ar[ar.length-2];
  lastBtn.parentNode.append(btnWrapper);
};

window.addEventListener('load', function () {

// Insert pdf_viewer button  
var btn = document.querySelectorAll('#mceu_20')[0];

if (btn) {
  createButton('pdf_viewer', 'Insert a PDF', 'mceu_pdf_viewer', function(e) {
        // show dialog
        var editor = tinyMCE.editors[0];
        editor.windowManager.open({
            title: 'Insert PDF',
                body: [
                    {type: 'textbox', name: 'pdfurl', label: 'PDF URL', size: 'large'}
                ],
                onsubmit: function(e) {
                    // Insert content when the window form is submitted
                   editor.insertContent('<button data-pdfurl="' + e.data.pdfurl + '">Open PDF Viewer</button>');
                }
        });
    });
}

// Callback to open pdf viewer window
function openPDF(e){
  const url = e.target.dataset.pdfurl;
  let pdfViewer = document.createElement('div');
  pdfViewer.className='pdf-viewer';
  pdfViewer.innerHTML = `<iframe title="PDF viwer" src="/pdfjs/web/viewer.html?file=${url}" width="100%" height="500px"></iframe>`;
  Swal.fire({
  title: 'PDF Viewer',
  width: '80%',
  height: '500px',
  html: pdfViewer
})
 
}

const pdfViewers = document.querySelectorAll('button[data-pdfurl]');
for (const viewer of pdfViewers) {
  viewer.addEventListener('click', openPDF);
}

});
</script>

Usage

After the setup, a 'pdf_viewer' button appears in the toolbar of the editor. 
toolbar

Upload the pdf on the page in order to get the pdf url.

Click 'pdf_viewer' button to paste the url. A 'Open PDF Viewer' button will be inserted in the content area.
insert

when viewing the page, click 'Open PDF Viewer' button will open a popup window to view the pdf file. The viewer has a toolbar for search, download and zoom operations etc. Click 'Ok' button will close the popup window.

pdfviewer-btn
viewer-window

@Binhbnt
Copy link

Binhbnt commented Jul 18, 2022

Hi, I'm a newbie. Copy the 2 folders "build" and "web" to the "public" directory of the bookstack, where can this code be copied to run? I don't know where the "Custom HTML Head Content field on Settings page" is located in the bookstack. Please help me. Thank you

@Buantum
Copy link

Buantum commented Feb 8, 2023

Hi, I'm a newbie. Copy the 2 folders "build" and "web" to the "public" directory of the bookstack, where can this code be copied to run? I don't know where the "Custom HTML Head Content field on Settings page" is located in the bookstack. Please help me. Thank you

the custom HTML Head Content can be fonud in demo with this link https://demo.bookstackapp.com/settings/customization. it is on the button of such pages
image

@Buantum
Copy link

Buantum commented Feb 8, 2023

I cant find my 'public' folder. is it a folder named 'public' or just a folder open to all users? thank you!

@sprklinginfo
Copy link
Author

the 'public' folder inside the bookstack directory and the 'public' folder is also the document root for your website.

@weilin0304
Copy link

Hi,
Is this code still avalible on BookStack v23.01.1?
I had tried to put the pdfjs folder in /var/www/bookstack/public/ and paste the code into html header, and still not seeing the icon of pdf viewer? Is there anything I did wrong??
test1
test2
test3

@nabrurheuk
Copy link

same for me... no 'pdf_viewer' button after installation

@ddkhanh
Copy link

ddkhanh commented Mar 27, 2024

Based on this guideline, I have done some research to come up with this setup https://github.com/ddkhanh/bookstack-pdf-viewer/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment