How do I display all pages in a PDF (stored in base64) using PDF.js?
I am using the following script to display a PDF using PDF.js, but it does not show anything on the canvas. Can you tell me where I am going wrong with this? I tried looking for documentation online, but could not find any help.
@Model.B is the base64 string.
<script>
var pdfData = atob("@Model.B");
var pdfjsLib = window['pdfjs-dist/build/pdf'];
pdfjsLib.GlobalWorkerOptions.workerSrc = '//mozilla.github.io/pdf.js/build/pdf.worker.js';
var loadingTask = pdfjsLib.getDocument({ data: pdfData });
loadingTask.getDocument(pdfData).promise.then(function (pdf) {
thePdf = pdf;
viewer = document.getElementById('pdfData');
for (page = 1; page <= pdf.numPages; page++) {
canvas = document.createElement("canvas");
canvas.className = 'pdf-page-canvas';
viewer.appendChild(canvas);
renderPage(page, canvas);
}
});
function renderPage(pageNumber, canvas) {
thePdf.getPage(pageNumber).then(function (page) {
viewport = page.getViewport(scale);
canvas.height = viewport.height;
canvas.width = viewport.width;
page.render({ canvasContext: canvas.getContext('2d'), viewport: viewport });
});
}
</script>
The following is the div. I used the embed tag to see if the PDF data was correct or not.
<div class="form-inline" style="margin-left:130px; margin-top:20px; display:none" id="pdfData" role="group" aria-label="Basic example">
<embed src="data:application/pdf;base64,@Model.B" style="height: 750px; width: 1000px;" alt="pdf" type="application/pdf"/>
@ViewBag.Error
<div class="form-group row" style="margin-top: 20px; margin-left: 25px">
Accept Policy
<div class="col-sm-6 col-sm-offset-2 col-xs-6 col-xs-offset-0" , style="text-align:justify; margin-top: 20px;">
@Html.CheckBoxFor(model => model.C)
@Html.ValidationMessageFor(model => model.C, "", new { @class = "form-text", style = "color:rgb(184, 10, 10); font-size:12px;" })
</div>
<div class="form-inline" role="group" aria-label="Basic example">
<button type="submit" class="btn btn-default btn-sm" id="submit_Agreement" name="submit_Agreement" title="Agree to Policy" onclick="">
Submit
</button>
</div>
</div>
</div>
javascript c# html asp.net-mvc pdf.js
|
show 1 more comment
I am using the following script to display a PDF using PDF.js, but it does not show anything on the canvas. Can you tell me where I am going wrong with this? I tried looking for documentation online, but could not find any help.
@Model.B is the base64 string.
<script>
var pdfData = atob("@Model.B");
var pdfjsLib = window['pdfjs-dist/build/pdf'];
pdfjsLib.GlobalWorkerOptions.workerSrc = '//mozilla.github.io/pdf.js/build/pdf.worker.js';
var loadingTask = pdfjsLib.getDocument({ data: pdfData });
loadingTask.getDocument(pdfData).promise.then(function (pdf) {
thePdf = pdf;
viewer = document.getElementById('pdfData');
for (page = 1; page <= pdf.numPages; page++) {
canvas = document.createElement("canvas");
canvas.className = 'pdf-page-canvas';
viewer.appendChild(canvas);
renderPage(page, canvas);
}
});
function renderPage(pageNumber, canvas) {
thePdf.getPage(pageNumber).then(function (page) {
viewport = page.getViewport(scale);
canvas.height = viewport.height;
canvas.width = viewport.width;
page.render({ canvasContext: canvas.getContext('2d'), viewport: viewport });
});
}
</script>
The following is the div. I used the embed tag to see if the PDF data was correct or not.
<div class="form-inline" style="margin-left:130px; margin-top:20px; display:none" id="pdfData" role="group" aria-label="Basic example">
<embed src="data:application/pdf;base64,@Model.B" style="height: 750px; width: 1000px;" alt="pdf" type="application/pdf"/>
@ViewBag.Error
<div class="form-group row" style="margin-top: 20px; margin-left: 25px">
Accept Policy
<div class="col-sm-6 col-sm-offset-2 col-xs-6 col-xs-offset-0" , style="text-align:justify; margin-top: 20px;">
@Html.CheckBoxFor(model => model.C)
@Html.ValidationMessageFor(model => model.C, "", new { @class = "form-text", style = "color:rgb(184, 10, 10); font-size:12px;" })
</div>
<div class="form-inline" role="group" aria-label="Basic example">
<button type="submit" class="btn btn-default btn-sm" id="submit_Agreement" name="submit_Agreement" title="Agree to Policy" onclick="">
Submit
</button>
</div>
</div>
</div>
javascript c# html asp.net-mvc pdf.js
1
Any errors (on development console)? Can you inspect the page and determine if canvas elements are actually created?
– ymz
Oct 29 '18 at 12:07
No, the canvas elements are not created. Also, when I debug it, the code never goes to the var pdfjsLib declaration line in the script.
– Jitesh G
Oct 29 '18 at 12:10
Seems like the first line of code is throwing an error. By the way - are you sure you are not missing a razor block? JS does not now what the value behind@Model.B
– ymz
Oct 29 '18 at 12:13
I tried the same code earlier without the for loop to display the first page, and it worked perfectly fine. I was referring to the example shown here: mozilla.github.io/pdf.js/examples
– Jitesh G
Oct 29 '18 at 12:19
I think it is better for you do replace the loop with aconsole.log(pdf)
statement, inspect the values from your debugger / dev console and proceed from there
– ymz
Oct 29 '18 at 12:23
|
show 1 more comment
I am using the following script to display a PDF using PDF.js, but it does not show anything on the canvas. Can you tell me where I am going wrong with this? I tried looking for documentation online, but could not find any help.
@Model.B is the base64 string.
<script>
var pdfData = atob("@Model.B");
var pdfjsLib = window['pdfjs-dist/build/pdf'];
pdfjsLib.GlobalWorkerOptions.workerSrc = '//mozilla.github.io/pdf.js/build/pdf.worker.js';
var loadingTask = pdfjsLib.getDocument({ data: pdfData });
loadingTask.getDocument(pdfData).promise.then(function (pdf) {
thePdf = pdf;
viewer = document.getElementById('pdfData');
for (page = 1; page <= pdf.numPages; page++) {
canvas = document.createElement("canvas");
canvas.className = 'pdf-page-canvas';
viewer.appendChild(canvas);
renderPage(page, canvas);
}
});
function renderPage(pageNumber, canvas) {
thePdf.getPage(pageNumber).then(function (page) {
viewport = page.getViewport(scale);
canvas.height = viewport.height;
canvas.width = viewport.width;
page.render({ canvasContext: canvas.getContext('2d'), viewport: viewport });
});
}
</script>
The following is the div. I used the embed tag to see if the PDF data was correct or not.
<div class="form-inline" style="margin-left:130px; margin-top:20px; display:none" id="pdfData" role="group" aria-label="Basic example">
<embed src="data:application/pdf;base64,@Model.B" style="height: 750px; width: 1000px;" alt="pdf" type="application/pdf"/>
@ViewBag.Error
<div class="form-group row" style="margin-top: 20px; margin-left: 25px">
Accept Policy
<div class="col-sm-6 col-sm-offset-2 col-xs-6 col-xs-offset-0" , style="text-align:justify; margin-top: 20px;">
@Html.CheckBoxFor(model => model.C)
@Html.ValidationMessageFor(model => model.C, "", new { @class = "form-text", style = "color:rgb(184, 10, 10); font-size:12px;" })
</div>
<div class="form-inline" role="group" aria-label="Basic example">
<button type="submit" class="btn btn-default btn-sm" id="submit_Agreement" name="submit_Agreement" title="Agree to Policy" onclick="">
Submit
</button>
</div>
</div>
</div>
javascript c# html asp.net-mvc pdf.js
I am using the following script to display a PDF using PDF.js, but it does not show anything on the canvas. Can you tell me where I am going wrong with this? I tried looking for documentation online, but could not find any help.
@Model.B is the base64 string.
<script>
var pdfData = atob("@Model.B");
var pdfjsLib = window['pdfjs-dist/build/pdf'];
pdfjsLib.GlobalWorkerOptions.workerSrc = '//mozilla.github.io/pdf.js/build/pdf.worker.js';
var loadingTask = pdfjsLib.getDocument({ data: pdfData });
loadingTask.getDocument(pdfData).promise.then(function (pdf) {
thePdf = pdf;
viewer = document.getElementById('pdfData');
for (page = 1; page <= pdf.numPages; page++) {
canvas = document.createElement("canvas");
canvas.className = 'pdf-page-canvas';
viewer.appendChild(canvas);
renderPage(page, canvas);
}
});
function renderPage(pageNumber, canvas) {
thePdf.getPage(pageNumber).then(function (page) {
viewport = page.getViewport(scale);
canvas.height = viewport.height;
canvas.width = viewport.width;
page.render({ canvasContext: canvas.getContext('2d'), viewport: viewport });
});
}
</script>
The following is the div. I used the embed tag to see if the PDF data was correct or not.
<div class="form-inline" style="margin-left:130px; margin-top:20px; display:none" id="pdfData" role="group" aria-label="Basic example">
<embed src="data:application/pdf;base64,@Model.B" style="height: 750px; width: 1000px;" alt="pdf" type="application/pdf"/>
@ViewBag.Error
<div class="form-group row" style="margin-top: 20px; margin-left: 25px">
Accept Policy
<div class="col-sm-6 col-sm-offset-2 col-xs-6 col-xs-offset-0" , style="text-align:justify; margin-top: 20px;">
@Html.CheckBoxFor(model => model.C)
@Html.ValidationMessageFor(model => model.C, "", new { @class = "form-text", style = "color:rgb(184, 10, 10); font-size:12px;" })
</div>
<div class="form-inline" role="group" aria-label="Basic example">
<button type="submit" class="btn btn-default btn-sm" id="submit_Agreement" name="submit_Agreement" title="Agree to Policy" onclick="">
Submit
</button>
</div>
</div>
</div>
javascript c# html asp.net-mvc pdf.js
javascript c# html asp.net-mvc pdf.js
edited Oct 29 '18 at 12:18
Jitesh G
asked Oct 29 '18 at 12:03
Jitesh GJitesh G
114
114
1
Any errors (on development console)? Can you inspect the page and determine if canvas elements are actually created?
– ymz
Oct 29 '18 at 12:07
No, the canvas elements are not created. Also, when I debug it, the code never goes to the var pdfjsLib declaration line in the script.
– Jitesh G
Oct 29 '18 at 12:10
Seems like the first line of code is throwing an error. By the way - are you sure you are not missing a razor block? JS does not now what the value behind@Model.B
– ymz
Oct 29 '18 at 12:13
I tried the same code earlier without the for loop to display the first page, and it worked perfectly fine. I was referring to the example shown here: mozilla.github.io/pdf.js/examples
– Jitesh G
Oct 29 '18 at 12:19
I think it is better for you do replace the loop with aconsole.log(pdf)
statement, inspect the values from your debugger / dev console and proceed from there
– ymz
Oct 29 '18 at 12:23
|
show 1 more comment
1
Any errors (on development console)? Can you inspect the page and determine if canvas elements are actually created?
– ymz
Oct 29 '18 at 12:07
No, the canvas elements are not created. Also, when I debug it, the code never goes to the var pdfjsLib declaration line in the script.
– Jitesh G
Oct 29 '18 at 12:10
Seems like the first line of code is throwing an error. By the way - are you sure you are not missing a razor block? JS does not now what the value behind@Model.B
– ymz
Oct 29 '18 at 12:13
I tried the same code earlier without the for loop to display the first page, and it worked perfectly fine. I was referring to the example shown here: mozilla.github.io/pdf.js/examples
– Jitesh G
Oct 29 '18 at 12:19
I think it is better for you do replace the loop with aconsole.log(pdf)
statement, inspect the values from your debugger / dev console and proceed from there
– ymz
Oct 29 '18 at 12:23
1
1
Any errors (on development console)? Can you inspect the page and determine if canvas elements are actually created?
– ymz
Oct 29 '18 at 12:07
Any errors (on development console)? Can you inspect the page and determine if canvas elements are actually created?
– ymz
Oct 29 '18 at 12:07
No, the canvas elements are not created. Also, when I debug it, the code never goes to the var pdfjsLib declaration line in the script.
– Jitesh G
Oct 29 '18 at 12:10
No, the canvas elements are not created. Also, when I debug it, the code never goes to the var pdfjsLib declaration line in the script.
– Jitesh G
Oct 29 '18 at 12:10
Seems like the first line of code is throwing an error. By the way - are you sure you are not missing a razor block? JS does not now what the value behind
@Model.B
– ymz
Oct 29 '18 at 12:13
Seems like the first line of code is throwing an error. By the way - are you sure you are not missing a razor block? JS does not now what the value behind
@Model.B
– ymz
Oct 29 '18 at 12:13
I tried the same code earlier without the for loop to display the first page, and it worked perfectly fine. I was referring to the example shown here: mozilla.github.io/pdf.js/examples
– Jitesh G
Oct 29 '18 at 12:19
I tried the same code earlier without the for loop to display the first page, and it worked perfectly fine. I was referring to the example shown here: mozilla.github.io/pdf.js/examples
– Jitesh G
Oct 29 '18 at 12:19
I think it is better for you do replace the loop with a
console.log(pdf)
statement, inspect the values from your debugger / dev console and proceed from there– ymz
Oct 29 '18 at 12:23
I think it is better for you do replace the loop with a
console.log(pdf)
statement, inspect the values from your debugger / dev console and proceed from there– ymz
Oct 29 '18 at 12:23
|
show 1 more comment
1 Answer
1
active
oldest
votes
This is a solution using the viewer.html from PDF.js.
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<script type="text/javascript" src="./pdfjs-dist/build/pdf.js"></script>
</head>
<body>
<iframe id="pdfFrame" style="width:900px;height:1000px;"></iframe>
<script>
function base64ToUint8Array(base64) {
var raw = atob(base64);
var uint8Array = new Uint8Array(raw.length);
for (var i = 0; i < raw.length; i++) {
uint8Array[i] = raw.charCodeAt(i);
}
return uint8Array;
}
var pdfData = base64ToUint8Array("JVBERi0xLjcKCjEgMCBvYmogICUgZW50cnkgcG9pbnQKPDwKICAvVHlwZSAvQ2F0YWxvZwogIC9QYWdlcyAyIDAgUgo+PgplbmRvYmoKCjIgMCBvYmoKPDwKICAvVHlwZSAvUGFnZXMKICAvTWVkaWFCb3ggWyAwIDAgMjAwIDIwMCBdCiAgL0NvdW50IDEKICAvS2lkcyBbIDMgMCBSIF0KPj4KZW5kb2JqCgozIDAgb2JqCjw8CiAgL1R5cGUgL1BhZ2UKICAvUGFyZW50IDIgMCBSCiAgL1Jlc291cmNlcyA8PAogICAgL0ZvbnQgPDwKICAgICAgL0YxIDQgMCBSIAogICAgPj4KICA+PgogIC9Db250ZW50cyA1IDAgUgo+PgplbmRvYmoKCjQgMCBvYmoKPDwKICAvVHlwZSAvRm9udAogIC9TdWJ0eXBlIC9UeXBlMQogIC9CYXNlRm9udCAvVGltZXMtUm9tYW4KPj4KZW5kb2JqCgo1IDAgb2JqICAlIHBhZ2UgY29udGVudAo8PAogIC9MZW5ndGggNDQKPj4Kc3RyZWFtCkJUCjcwIDUwIFRECi9GMSAxMiBUZgooSGVsbG8sIHdvcmxkISkgVGoKRVQKZW5kc3RyZWFtCmVuZG9iagoKeHJlZgowIDYKMDAwMDAwMDAwMCA2NTUzNSBmIAowMDAwMDAwMDEwIDAwMDAwIG4gCjAwMDAwMDAwNzkgMDAwMDAgbiAKMDAwMDAwMDE3MyAwMDAwMCBuIAowMDAwMDAwMzAxIDAwMDAwIG4gCjAwMDAwMDAzODAgMDAwMDAgbiAKdHJhaWxlcgo8PAogIC9TaXplIDYKICAvUm9vdCAxIDAgUgo+PgpzdGFydHhyZWYKNDkyCiUlRU9G");
var pdfViewerFrame = document.getElementById("pdfFrame");
pdfViewerFrame.onload = function() {
pdfViewerFrame.contentWindow.PDFViewerApplication.open(pdfData);
}
pdfViewerFrame.setAttribute("src","./pdfjs-dist/web/viewer.html?file=");
</script>
</body>
</html>
And here is another example without using viewer.html: https://jsfiddle.net/pdfjs/cq0asLqz/
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53045127%2fhow-do-i-display-all-pages-in-a-pdf-stored-in-base64-using-pdf-js%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
This is a solution using the viewer.html from PDF.js.
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<script type="text/javascript" src="./pdfjs-dist/build/pdf.js"></script>
</head>
<body>
<iframe id="pdfFrame" style="width:900px;height:1000px;"></iframe>
<script>
function base64ToUint8Array(base64) {
var raw = atob(base64);
var uint8Array = new Uint8Array(raw.length);
for (var i = 0; i < raw.length; i++) {
uint8Array[i] = raw.charCodeAt(i);
}
return uint8Array;
}
var pdfData = base64ToUint8Array("JVBERi0xLjcKCjEgMCBvYmogICUgZW50cnkgcG9pbnQKPDwKICAvVHlwZSAvQ2F0YWxvZwogIC9QYWdlcyAyIDAgUgo+PgplbmRvYmoKCjIgMCBvYmoKPDwKICAvVHlwZSAvUGFnZXMKICAvTWVkaWFCb3ggWyAwIDAgMjAwIDIwMCBdCiAgL0NvdW50IDEKICAvS2lkcyBbIDMgMCBSIF0KPj4KZW5kb2JqCgozIDAgb2JqCjw8CiAgL1R5cGUgL1BhZ2UKICAvUGFyZW50IDIgMCBSCiAgL1Jlc291cmNlcyA8PAogICAgL0ZvbnQgPDwKICAgICAgL0YxIDQgMCBSIAogICAgPj4KICA+PgogIC9Db250ZW50cyA1IDAgUgo+PgplbmRvYmoKCjQgMCBvYmoKPDwKICAvVHlwZSAvRm9udAogIC9TdWJ0eXBlIC9UeXBlMQogIC9CYXNlRm9udCAvVGltZXMtUm9tYW4KPj4KZW5kb2JqCgo1IDAgb2JqICAlIHBhZ2UgY29udGVudAo8PAogIC9MZW5ndGggNDQKPj4Kc3RyZWFtCkJUCjcwIDUwIFRECi9GMSAxMiBUZgooSGVsbG8sIHdvcmxkISkgVGoKRVQKZW5kc3RyZWFtCmVuZG9iagoKeHJlZgowIDYKMDAwMDAwMDAwMCA2NTUzNSBmIAowMDAwMDAwMDEwIDAwMDAwIG4gCjAwMDAwMDAwNzkgMDAwMDAgbiAKMDAwMDAwMDE3MyAwMDAwMCBuIAowMDAwMDAwMzAxIDAwMDAwIG4gCjAwMDAwMDAzODAgMDAwMDAgbiAKdHJhaWxlcgo8PAogIC9TaXplIDYKICAvUm9vdCAxIDAgUgo+PgpzdGFydHhyZWYKNDkyCiUlRU9G");
var pdfViewerFrame = document.getElementById("pdfFrame");
pdfViewerFrame.onload = function() {
pdfViewerFrame.contentWindow.PDFViewerApplication.open(pdfData);
}
pdfViewerFrame.setAttribute("src","./pdfjs-dist/web/viewer.html?file=");
</script>
</body>
</html>
And here is another example without using viewer.html: https://jsfiddle.net/pdfjs/cq0asLqz/
add a comment |
This is a solution using the viewer.html from PDF.js.
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<script type="text/javascript" src="./pdfjs-dist/build/pdf.js"></script>
</head>
<body>
<iframe id="pdfFrame" style="width:900px;height:1000px;"></iframe>
<script>
function base64ToUint8Array(base64) {
var raw = atob(base64);
var uint8Array = new Uint8Array(raw.length);
for (var i = 0; i < raw.length; i++) {
uint8Array[i] = raw.charCodeAt(i);
}
return uint8Array;
}
var pdfData = base64ToUint8Array("JVBERi0xLjcKCjEgMCBvYmogICUgZW50cnkgcG9pbnQKPDwKICAvVHlwZSAvQ2F0YWxvZwogIC9QYWdlcyAyIDAgUgo+PgplbmRvYmoKCjIgMCBvYmoKPDwKICAvVHlwZSAvUGFnZXMKICAvTWVkaWFCb3ggWyAwIDAgMjAwIDIwMCBdCiAgL0NvdW50IDEKICAvS2lkcyBbIDMgMCBSIF0KPj4KZW5kb2JqCgozIDAgb2JqCjw8CiAgL1R5cGUgL1BhZ2UKICAvUGFyZW50IDIgMCBSCiAgL1Jlc291cmNlcyA8PAogICAgL0ZvbnQgPDwKICAgICAgL0YxIDQgMCBSIAogICAgPj4KICA+PgogIC9Db250ZW50cyA1IDAgUgo+PgplbmRvYmoKCjQgMCBvYmoKPDwKICAvVHlwZSAvRm9udAogIC9TdWJ0eXBlIC9UeXBlMQogIC9CYXNlRm9udCAvVGltZXMtUm9tYW4KPj4KZW5kb2JqCgo1IDAgb2JqICAlIHBhZ2UgY29udGVudAo8PAogIC9MZW5ndGggNDQKPj4Kc3RyZWFtCkJUCjcwIDUwIFRECi9GMSAxMiBUZgooSGVsbG8sIHdvcmxkISkgVGoKRVQKZW5kc3RyZWFtCmVuZG9iagoKeHJlZgowIDYKMDAwMDAwMDAwMCA2NTUzNSBmIAowMDAwMDAwMDEwIDAwMDAwIG4gCjAwMDAwMDAwNzkgMDAwMDAgbiAKMDAwMDAwMDE3MyAwMDAwMCBuIAowMDAwMDAwMzAxIDAwMDAwIG4gCjAwMDAwMDAzODAgMDAwMDAgbiAKdHJhaWxlcgo8PAogIC9TaXplIDYKICAvUm9vdCAxIDAgUgo+PgpzdGFydHhyZWYKNDkyCiUlRU9G");
var pdfViewerFrame = document.getElementById("pdfFrame");
pdfViewerFrame.onload = function() {
pdfViewerFrame.contentWindow.PDFViewerApplication.open(pdfData);
}
pdfViewerFrame.setAttribute("src","./pdfjs-dist/web/viewer.html?file=");
</script>
</body>
</html>
And here is another example without using viewer.html: https://jsfiddle.net/pdfjs/cq0asLqz/
add a comment |
This is a solution using the viewer.html from PDF.js.
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<script type="text/javascript" src="./pdfjs-dist/build/pdf.js"></script>
</head>
<body>
<iframe id="pdfFrame" style="width:900px;height:1000px;"></iframe>
<script>
function base64ToUint8Array(base64) {
var raw = atob(base64);
var uint8Array = new Uint8Array(raw.length);
for (var i = 0; i < raw.length; i++) {
uint8Array[i] = raw.charCodeAt(i);
}
return uint8Array;
}
var pdfData = base64ToUint8Array("JVBERi0xLjcKCjEgMCBvYmogICUgZW50cnkgcG9pbnQKPDwKICAvVHlwZSAvQ2F0YWxvZwogIC9QYWdlcyAyIDAgUgo+PgplbmRvYmoKCjIgMCBvYmoKPDwKICAvVHlwZSAvUGFnZXMKICAvTWVkaWFCb3ggWyAwIDAgMjAwIDIwMCBdCiAgL0NvdW50IDEKICAvS2lkcyBbIDMgMCBSIF0KPj4KZW5kb2JqCgozIDAgb2JqCjw8CiAgL1R5cGUgL1BhZ2UKICAvUGFyZW50IDIgMCBSCiAgL1Jlc291cmNlcyA8PAogICAgL0ZvbnQgPDwKICAgICAgL0YxIDQgMCBSIAogICAgPj4KICA+PgogIC9Db250ZW50cyA1IDAgUgo+PgplbmRvYmoKCjQgMCBvYmoKPDwKICAvVHlwZSAvRm9udAogIC9TdWJ0eXBlIC9UeXBlMQogIC9CYXNlRm9udCAvVGltZXMtUm9tYW4KPj4KZW5kb2JqCgo1IDAgb2JqICAlIHBhZ2UgY29udGVudAo8PAogIC9MZW5ndGggNDQKPj4Kc3RyZWFtCkJUCjcwIDUwIFRECi9GMSAxMiBUZgooSGVsbG8sIHdvcmxkISkgVGoKRVQKZW5kc3RyZWFtCmVuZG9iagoKeHJlZgowIDYKMDAwMDAwMDAwMCA2NTUzNSBmIAowMDAwMDAwMDEwIDAwMDAwIG4gCjAwMDAwMDAwNzkgMDAwMDAgbiAKMDAwMDAwMDE3MyAwMDAwMCBuIAowMDAwMDAwMzAxIDAwMDAwIG4gCjAwMDAwMDAzODAgMDAwMDAgbiAKdHJhaWxlcgo8PAogIC9TaXplIDYKICAvUm9vdCAxIDAgUgo+PgpzdGFydHhyZWYKNDkyCiUlRU9G");
var pdfViewerFrame = document.getElementById("pdfFrame");
pdfViewerFrame.onload = function() {
pdfViewerFrame.contentWindow.PDFViewerApplication.open(pdfData);
}
pdfViewerFrame.setAttribute("src","./pdfjs-dist/web/viewer.html?file=");
</script>
</body>
</html>
And here is another example without using viewer.html: https://jsfiddle.net/pdfjs/cq0asLqz/
This is a solution using the viewer.html from PDF.js.
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<script type="text/javascript" src="./pdfjs-dist/build/pdf.js"></script>
</head>
<body>
<iframe id="pdfFrame" style="width:900px;height:1000px;"></iframe>
<script>
function base64ToUint8Array(base64) {
var raw = atob(base64);
var uint8Array = new Uint8Array(raw.length);
for (var i = 0; i < raw.length; i++) {
uint8Array[i] = raw.charCodeAt(i);
}
return uint8Array;
}
var pdfData = base64ToUint8Array("JVBERi0xLjcKCjEgMCBvYmogICUgZW50cnkgcG9pbnQKPDwKICAvVHlwZSAvQ2F0YWxvZwogIC9QYWdlcyAyIDAgUgo+PgplbmRvYmoKCjIgMCBvYmoKPDwKICAvVHlwZSAvUGFnZXMKICAvTWVkaWFCb3ggWyAwIDAgMjAwIDIwMCBdCiAgL0NvdW50IDEKICAvS2lkcyBbIDMgMCBSIF0KPj4KZW5kb2JqCgozIDAgb2JqCjw8CiAgL1R5cGUgL1BhZ2UKICAvUGFyZW50IDIgMCBSCiAgL1Jlc291cmNlcyA8PAogICAgL0ZvbnQgPDwKICAgICAgL0YxIDQgMCBSIAogICAgPj4KICA+PgogIC9Db250ZW50cyA1IDAgUgo+PgplbmRvYmoKCjQgMCBvYmoKPDwKICAvVHlwZSAvRm9udAogIC9TdWJ0eXBlIC9UeXBlMQogIC9CYXNlRm9udCAvVGltZXMtUm9tYW4KPj4KZW5kb2JqCgo1IDAgb2JqICAlIHBhZ2UgY29udGVudAo8PAogIC9MZW5ndGggNDQKPj4Kc3RyZWFtCkJUCjcwIDUwIFRECi9GMSAxMiBUZgooSGVsbG8sIHdvcmxkISkgVGoKRVQKZW5kc3RyZWFtCmVuZG9iagoKeHJlZgowIDYKMDAwMDAwMDAwMCA2NTUzNSBmIAowMDAwMDAwMDEwIDAwMDAwIG4gCjAwMDAwMDAwNzkgMDAwMDAgbiAKMDAwMDAwMDE3MyAwMDAwMCBuIAowMDAwMDAwMzAxIDAwMDAwIG4gCjAwMDAwMDAzODAgMDAwMDAgbiAKdHJhaWxlcgo8PAogIC9TaXplIDYKICAvUm9vdCAxIDAgUgo+PgpzdGFydHhyZWYKNDkyCiUlRU9G");
var pdfViewerFrame = document.getElementById("pdfFrame");
pdfViewerFrame.onload = function() {
pdfViewerFrame.contentWindow.PDFViewerApplication.open(pdfData);
}
pdfViewerFrame.setAttribute("src","./pdfjs-dist/web/viewer.html?file=");
</script>
</body>
</html>
And here is another example without using viewer.html: https://jsfiddle.net/pdfjs/cq0asLqz/
edited Nov 14 '18 at 10:53
answered Nov 14 '18 at 10:35
de-factode-facto
686
686
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53045127%2fhow-do-i-display-all-pages-in-a-pdf-stored-in-base64-using-pdf-js%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Any errors (on development console)? Can you inspect the page and determine if canvas elements are actually created?
– ymz
Oct 29 '18 at 12:07
No, the canvas elements are not created. Also, when I debug it, the code never goes to the var pdfjsLib declaration line in the script.
– Jitesh G
Oct 29 '18 at 12:10
Seems like the first line of code is throwing an error. By the way - are you sure you are not missing a razor block? JS does not now what the value behind
@Model.B
– ymz
Oct 29 '18 at 12:13
I tried the same code earlier without the for loop to display the first page, and it worked perfectly fine. I was referring to the example shown here: mozilla.github.io/pdf.js/examples
– Jitesh G
Oct 29 '18 at 12:19
I think it is better for you do replace the loop with a
console.log(pdf)
statement, inspect the values from your debugger / dev console and proceed from there– ymz
Oct 29 '18 at 12:23