Created
July 2, 2018 06:41
-
-
Save eroltutumlu/f17f2af3d509d5f8e1a24e6bfa8fe129 to your computer and use it in GitHub Desktop.
This file contains hidden or 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> | |
<head> | |
<meta charset="utf-8" /> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | |
<title>PDF TASK</title> | |
<meta name="viewport" content="width=device-width, initial-scale=1"> | |
</head> | |
<body> | |
<div id="holder"></div> | |
<script src="https://mozilla.github.io/pdf.js/build/pdf.js"></script> | |
<script type="text/javascript"> | |
var txtContent = ''; | |
function renderPDF(url, canvasContainer, options) { | |
var options = options || { scale: 1 }; | |
function renderPage(page) { | |
var viewport = page.getViewport(options.scale); | |
var canvas = document.createElement('canvas'); | |
var ctx = canvas.getContext('2d'); | |
var renderContext = { | |
canvasContext: ctx, | |
viewport: viewport | |
}; | |
canvas.height = viewport.height; | |
canvas.width = viewport.width; | |
canvasContainer.appendChild(canvas); | |
page.render(renderContext); | |
page.getTextContent().then(fetchTextContent).then(function(arr){ | |
if(arr.length == 11){ | |
for(let i = 0; i < arr.length-1; i++){ | |
arr[i] = `${i+1} ${arr[i]}`; | |
txtContent += arr[i] + "\n"; | |
} | |
txtContent += arr[arr.length-1] + "\n"; | |
console.log(txtContent); | |
let a = document.createElement("a"); | |
let file = new Blob([txtContent.replace(/\n/g, "\r\n")], {type: 'text/plain'}); | |
a.href = window.URL.createObjectURL(file); | |
a.download = 'product.txt'; | |
a.click(); | |
window.URL.revokeObjectURL(window.URL.createObjectURL(file)); | |
} | |
}); | |
} | |
let products = []; | |
function fetchTextContent(textContent){ | |
let itemsLength = textContent.items.length; | |
let fullPrice; | |
if(itemsLength == 90){ | |
for(let i = 43; i < textContent.items.length; i+=16){ | |
if(i == 75) i = 74; | |
let name = textContent.items[i].str; | |
let count = textContent.items[i+4].str; | |
let price = textContent.items[i+10].str; | |
let fullTitle = `${name} ${count} ${price}`; | |
products.push(fullTitle); | |
} | |
}else{ | |
fullPrice = textContent.items[48].str; | |
for(let i = 101; i < textContent.items.length; i+=14){ | |
let name = textContent.items[i].str; | |
let count = textContent.items[i+2].str; | |
let price = textContent.items[i+8].str; | |
let fullTitle = `${name} ${count} ${price}`; | |
products.push(fullTitle); | |
} | |
products.push(products[0]); | |
products.push(products[1]); | |
products.push(products[2]); | |
products.splice(0,3); | |
products.push(fullPrice); | |
} | |
return products; | |
} | |
function renderPages(pdfDoc) { | |
for(var num = 1; num <= pdfDoc.numPages; num++) | |
pdfDoc.getPage(num).then(renderPage); | |
} | |
pdfjsLib.disableWorker = true; | |
pdfjsLib.getDocument(url).then(renderPages); | |
} | |
</script> | |
<div id="holder"></div> | |
<script type="text/javascript"> | |
renderPDF('test.pdf', document.getElementById('holder')); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment