Skip to content

Instantly share code, notes, and snippets.

@Guley
Created February 11, 2020 08:04
Show Gist options
  • Save Guley/dcceb3b2f403dec137c2af6188ec12c4 to your computer and use it in GitHub Desktop.
Save Guley/dcceb3b2f403dec137c2af6188ec12c4 to your computer and use it in GitHub Desktop.
Get content from file using pdf.js
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/1.8.349/pdf.min.js"></script>
<button type="button" class="btn btn-default btn-lg tts-btn" title="Upload text, pdf or ebook file">
<span class="glyphicon glyphicon-open-file btn-glyph" aria-hidden="true"></span>
<input type="file" id="files" name="files[]">
</button>
<div class="buttons_row">
<select class="btn btn-template-primary" id="select_language">
<option value=" UK English Male">UK English (default)</option>
</select>
<select class="btn btn-template-primary" id="select_speed">
<option value="0.9">Normal Speed</option>
</select>
</div>
<textarea id="text_box" rows="7" cols="50"></textarea>
<script>
document.querySelector("#files").addEventListener("change", function(e){
var canvasElement = document.querySelector("canvas")
var file = e.target.files[0]
if(file.type != "application/pdf"){
console.error(file.name, "is not a pdf file.")
return
}
var fileReader = new FileReader();
fileReader.onload = function() {
var typedarray = new Uint8Array(this.result);
PDFJS.getDocument(typedarray).then(function(pdf) {
// you can now use *pdf* here
pdf.getPage(pdf.numPages).then(function(page) {
// you can now use *page* here
page.getTextContent().then(function(textContent) {
var resultDiv = $("#text_box");
var tempContent ='';
textContent.items.forEach(function(textItem) {
tempContent += textItem.str;
});
resultDiv.html(tempContent)
});
});
});
};
fileReader.readAsArrayBuffer(file);
})
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment