|
<script src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.0.466/pdf.min.js"></script> |
|
<style> |
|
canvas[data-pdfurl] { |
|
background-color: lightgrey; |
|
width: 100%; |
|
} |
|
.page-content a { |
|
color: #39f; |
|
text-decoration: underline; |
|
} |
|
.pdf-wrapper { |
|
position: relative; |
|
height: 80vh; |
|
width: 100%; |
|
} |
|
.pdf-wrapper .download-link { |
|
position: absolute; |
|
top: -2em; |
|
right: 0; |
|
z-index: 50; |
|
} |
|
.pdf-wrapper .pdf-scroller { |
|
height: 100%; |
|
overflow: auto; |
|
} |
|
</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 = ' 3px 7px'; |
|
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 () { |
|
// ------------------- THIS SECTION ADDS A PDF BUTTON TO THE EDITOR TOOLBAR THAT ALLOWS YOU TO EMBED PDFS |
|
|
|
var btn = document.querySelectorAll('#mceu_20')[0]; |
|
|
|
|
|
if (btn) { |
|
createButton('pdf', 'Insert a PDF', 'mceu_pdf', function(e) { |
|
// show dialog |
|
var editor = tinyMCE.editors[0]; |
|
editor.windowManager.open({ |
|
title: 'Insert PDF', |
|
body: [ |
|
{type: 'textbox', name: 'pdfurl', label: 'PDF URL'} |
|
], |
|
onsubmit: function(e) { |
|
// Insert content when the window form is submitted |
|
editor.insertContent('<p> <canvas data-pdfurl="' + e.data.pdfurl + '"></canvas> </p>'); |
|
} |
|
}); |
|
}); |
|
} |
|
|
|
//-------------------- THE CODE BELOW SHALL BE ACTIVE IN VIEWING MODE TO EMBED PDFS |
|
var renderPdf=function(canvas) { |
|
var url = canvas.dataset.pdfurl; |
|
var pdf = null; |
|
// wrap canvas in div |
|
var wrapper = document.createElement('div'); |
|
wrapper.className='pdf-wrapper'; |
|
var scroller = document.createElement('div'); |
|
scroller.className='pdf-scroller'; |
|
wrapper.appendChild(scroller); |
|
canvas.parentNode.insertBefore(wrapper, canvas.nextSibling); |
|
scroller.insertBefore(canvas, null); |
|
|
|
var downloadLink = document.createElement('a'); |
|
downloadLink.href = url; |
|
downloadLink.className="download-link"; |
|
downloadLink.innerText = 'Download PDF now ↓'; |
|
wrapper.appendChild(downloadLink); |
|
|
|
var renderPage = function(page) { |
|
var scale = 1.5; |
|
var viewport = page.getViewport(scale); |
|
// Fetch canvas' 2d context |
|
var context = canvas.getContext('2d'); |
|
// Set dimensions to Canvas |
|
canvas.height = viewport.height; |
|
canvas.width = viewport.width; |
|
canvas.style.maxWidth='100%'; |
|
// Prepare object needed by render method |
|
var renderContext = { |
|
canvasContext: context, |
|
viewport: viewport |
|
}; |
|
// Render PDF page |
|
page.render(renderContext); |
|
if (currentPage < pdf.numPages) { |
|
currentPage++; |
|
var newCanvas = document.createElement('canvas'); |
|
scroller.insertBefore(newCanvas, canvas.nextSibling); |
|
scroller.insertBefore(document.createElement('hr'), canvas.nextSibling); |
|
canvas=newCanvas; |
|
pdf.getPage(currentPage).then(renderPage); |
|
} |
|
}; |
|
var currentPage = 1; |
|
pdfjsLib.getDocument(url) |
|
.then(function(pdfLocal) { |
|
pdf = pdfLocal; |
|
return pdf.getPage(1); |
|
}) |
|
.then(renderPage); |
|
} |
|
Array.prototype.forEach.call( |
|
document.querySelectorAll('canvas[data-pdfurl]'), |
|
renderPdf); |
|
}); |
|
</script> |
Thanks, this worked, but any idea why it can't be viewed on mobile whatsoever? Is the other solution above viewable on mobile at all?
Also, this only works for something like a PDF, but not a webpage? I tried to insert a URL into the above and it didn't work. TIA