<div class="container">
<div class="buttonHolder">
<button id="embedView01" title="https://documentcloud.adobe.com/view-sdk-demo/PDFs/Bodea Brochure.pdf" >Direct URL to PDF</button>
<button id="embedView02" title="not an actual url">Bad URL</button>
<button id="embedView03" title="https://documentcloud.adobe.com/link/review?uri=urn:aaid:scds:US:966b466d-7cea-4066-b2a3-195771984e9a">Not a PDF</button>
</div>
<div id="embeddedView"></div>
</div>
<script src="https://documentcloud.adobe.com/view-sdk/main.js"></script>
body {
width: 100%;
height: 100vh;
overflow-x: hidden;
margin: 0;
background-color: black;
overflow: hidden;
}
.container {
width: 100%;
height: 100%;
display: flex;
align-content: center;
justify-content: center;
}
.buttonHolder {
display: flex;
flex-wrap: nowrap;
flex-direction: column;
width: 225px;
padding: 20px;
}
button {
background-color: #4caf50;
border: none;
padding: 20px;
color: white;
text-align: center;
text-decoration: none;
font-size: 16px;
border-radius: 10px;
margin-bottom: 20px;
cursor: pointer;
display: block;
width: 100%;
}
#embeddedView {
width: 100%;
height: 100vh;
margin: 0 auto;
display: flex;
justify-content: center;
align-content: center;
flex-direction: column;
flex-grow: 1;
}
.error-message {
text-align: center;
color: red;
font-size: 24pt;
width: 500px;
margin: 0 auto;
}
const pPDFEmbedAPIWrapper = {
displayPDF: function (urlToPDF) {
fetch(urlToPDF)
.then(pPDFEmbedAPIWrapper.errorHandler)
.then((response) => response.blob())
.then((blob) => {
pPDFEmbedAPIWrapper.embedView(blob, urlToPDF);
})
.catch((error) => {
console.log(error);
});
},
embedView: function (blob, urlToPDF) {
if (window.adobeDCView) {
window.adobeDCView = null;
}
window.adobeDCView = new AdobeDC.View({
clientId: pPDFEmbedAPIWrapper.clientId,
divId: pPDFEmbedAPIWrapper.divId
});
window.adobeFilePreview = adobeDCView.previewFile(
{
content: { promise: blob.arrayBuffer() },
metaData: { fileName: urlToPDF.split("/").slice(-1)[0] }
},
pPDFEmbedAPIWrapper.viewerOptions
);
},
errorHandler: function (response) {
if (!response.ok) {
if (typeof badResponse === "function") {
badResponse(response); // modify the DOM here
}
throw Error("Fetch Response Status: " + response.status + " - " + response.statusText);
}
if (response.headers.get("Content-Type") != "application/pdf") {
if (typeof notPDF === "function") {
notPDF(response); // modify the DOM here
}
throw Error("The content at the requested URL is not a PDF file. Content-Type: " + response.headers.get("Content-Type"));
}
return response;
},
}
pPDFEmbedAPIWrapper.clientId = "e800d12fc12c4d60960778b2bc4370af"; // This clientId can be used for any CodePen example;
pPDFEmbedAPIWrapper.viewerOptions = {
embedMode: "SIZED_CONTAINER",
defaultViewMode: "FIT_PAGE",
showDownloadPDF: false,
showPrintPDF: false,
showLeftHandPanel: true,
showAnnotationTools: false
};
pPDFEmbedAPIWrapper.divId = "embeddedView";
document.addEventListener("adobe_dc_view_sdk.ready", (event) => {
document.getElementById("embedView01").addEventListener("click", function () {
pPDFEmbedAPIWrapper.displayPDF("https://documentcloud.adobe.com/view-sdk-demo/PDFs/Bodea Brochure.pdf")
});
document.getElementById("embedView02").addEventListener("click", function () {
pPDFEmbedAPIWrapper.displayPDF("not an actual url")
});
document.getElementById("embedView03").addEventListener("click", function () {
pPDFEmbedAPIWrapper.displayPDF("https://documentcloud.adobe.com/link/review?uri=urn:aaid:scds:US:966b466d-7cea-4066-b2a3-195771984e9a")
});
pPDFEmbedAPIWrapper.displayPDF("https://documentcloud.adobe.com/view-sdk-demo/PDFs/Bodea Brochure.pdf");
});
function badResponse(response) {
var messageElement = document.createElement("div");
messageElement.classList.add("error-message");
messageElement.appendChild(document.createTextNode("Fetch Response Status: " + response.status + " - " + response.statusText));
document.getElementById("embeddedView").clearChildren();
document.getElementById("embeddedView").appendChild(messageElement);
}
function notPDF(response) {
var messageElement = document.createElement("div");
messageElement.classList.add("error-message");
messageElement.appendChild(document.createTextNode("The MIME type of the requested content is " + response.headers.get("Content-Type") + ". The content must be MIME type application/pdf."));
document.getElementById("embeddedView").clearChildren();
document.getElementById("embeddedView").appendChild(messageElement);
}
// Helper functions
// Add arrayBuffer if necessary i.e. Safari
(function () {
if (Blob.arrayBuffer != "function") {
Blob.prototype.arrayBuffer = myArrayBuffer;
}
function myArrayBuffer() {
return new Promise((resolve) => {
let fileReader = new FileReader();
fileReader.onload = () => {
resolve(fileReader.result);
};
fileReader.readAsArrayBuffer(this);
});
}
})();
if (typeof Element.prototype.clearChildren === 'undefined') {
Object.defineProperty(Element.prototype, 'clearChildren', {
configurable: true,
enumerable: false,
value: function () {
while (this.firstChild) this.removeChild(this.lastChild);
}
});
}
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.