Mozilla PDF.js viewer for Trigger.io
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1"> | |
<!-- PDF.js --> | |
<link rel="stylesheet" href="bower_components/pdfjs-dist/web/pdf_viewer.css"> | |
<script src="bower_components/pdfjs-dist/web/compatibility.js"></script> | |
<script src="bower_components/pdfjs-dist/build/pdf.js"></script> | |
<script src="bower_components/pdfjs-dist/web/pdf_viewer.js"></script> | |
<script src="js/main.js"></script> | |
<script> | |
function test_pdfviewer() { | |
var url = "https://s3.amazonaws.com/trigger-blog-content/technote_pdf/whitepaper.pdf"; | |
forge.pdfviewer.open(url, function () { | |
forge.logging.log("Finished viewing PDF file"); | |
}, function (e) { | |
forge.logging.error("Couldn't open PDF file: " + JSON.stringify(e)); | |
}); | |
} | |
</script> | |
</head> | |
<body> | |
<center> | |
<h1>Trigger.io ❤ 's Mozilla PDF.js</h1> | |
<hr /> | |
<button onClick="test_pdfviewer();">Test PDF.js Viewer</button> | |
</center> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
forge.pdfviewer = { | |
open: function (url, success, error) { | |
// (optional) use the native viewer if we're on iOS | |
if (forge.is.ios()) { | |
forge.tools.getURL(url, function (url) { | |
forge.tabs.open(url, success, error); | |
}); | |
return; | |
} | |
// a simple modal container for our viewer | |
var container = document.createElement("div"); | |
container.style.position = "fixed"; | |
container.style.top = "50px"; | |
container.style.right = 0; | |
container.style.bottom = 0; | |
container.style.left = 0; | |
container.style.overflow = "auto"; | |
container.style.background = "white"; | |
// the parent div for the PDF viewer | |
var viewer = document.createElement("div"); | |
viewer.className = "pdfViewer"; | |
container.appendChild(viewer); | |
// instantiate a PDF.js pdfViewer | |
var pdfViewer = new PDFJS.PDFViewer({ | |
container: container | |
}); | |
container.addEventListener("pagesinit", function () { | |
pdfViewer.currentScaleValue = "page-width"; | |
}); | |
// fetch document if it lives on a remote server | |
if (url.indexOf("http") === 0) { | |
forge.file.cacheURL(url, function (file) { | |
forge.file.URL(file, function (url) { | |
if (forge.is.ios()) { | |
url = file.uri; // iOS XHR handles local URL's differently to Android | |
} | |
PDFJS.getDocument(url).then(function(document) { | |
pdfViewer.setDocument(document); | |
}); | |
}, error); | |
}, error); | |
} else { | |
PDFJS.getDocument(url).then(function(document) { | |
pdfViewer.setDocument(document); | |
}); | |
} | |
// a simple header with a close button | |
var header = document.createElement("div"); | |
header.style.position = "fixed"; | |
header.style.top = 0; | |
header.style.right = 0; | |
header.style.left = 0; | |
header.style.lineHeight = "50px"; | |
header.style.background = "#f8f8f8"; | |
var close = document.createElement("span"); | |
close.style.color = "#007AFF"; | |
close.style.marginLeft = "10px"; | |
close.innerHTML = "Close"; | |
close.onclick = function () { | |
pdfViewer.cleanup(); | |
pdfViewer = null; | |
document.body.removeChild(header); | |
document.body.removeChild(container); | |
success(); | |
}; | |
header.appendChild(close); | |
document.body.appendChild(container); | |
document.body.appendChild(header); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment