Preview PDF/Image file before upload
You have an input, check the onChange and then make a
src= URL.createObjectURL(event.target.files[0])
to create URL: and then use it to preview with embed
<embed
src=src
width="250"
height="200">
Read more here https://developer.mozilla.org/en-US/docs/Web/HTML/Element/embed
you can use javascript pdf library like this one (i'm using pdf.js) from this link : https://mozilla.github.io/pdf.js/examples/index.html#interactive-examples
// Loaded via <script> tag, create shortcut to access PDF.js exports.
var pdfjsLib = window['pdfjs-dist/build/pdf'];
// The workerSrc property shall be specified.
pdfjsLib.GlobalWorkerOptions.workerSrc = 'https://mozilla.github.io/pdf.js/build/pdf.worker.js';
$("#myPdf").on("change", function(e){
var file = e.target.files[0]
if(file.type == "application/pdf"){
var fileReader = new FileReader();
fileReader.onload = function() {
var pdfData = new Uint8Array(this.result);
// Using DocumentInitParameters object to load binary data.
var loadingTask = pdfjsLib.getDocument({data: pdfData});
loadingTask.promise.then(function(pdf) {
console.log('PDF loaded');
// Fetch the first page
var pageNumber = 1;
pdf.getPage(pageNumber).then(function(page) {
console.log('Page loaded');
var scale = 1.5;
var viewport = page.getViewport({scale: scale});
// Prepare canvas using PDF page dimensions
var canvas = $("#pdfViewer")[0];
var context = canvas.getContext('2d');
canvas.height = viewport.height;
canvas.width = viewport.width;
// Render PDF page into canvas context
var renderContext = {
canvasContext: context,
viewport: viewport
};
var renderTask = page.render(renderContext);
renderTask.promise.then(function () {
console.log('Page rendered');
});
});
}, function (reason) {
// PDF loading error
console.error(reason);
});
};
fileReader.readAsArrayBuffer(file);
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://mozilla.github.io/pdf.js/build/pdf.js"></script>
<input type="file" id="myPdf" /><br>
<canvas id="pdfViewer"></canvas>