To ensure that the JavaScript code is embedded directly within the HTML file (instead of being loaded from external sources), we can include all the necessary JavaScript libraries and logic inline within <script> tags. Below is the updated HTML file with all JavaScript embedded:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>PDF Table Extractor</title>
<!-- Material Design CSS -->
<style>
body {
font-family: 'Roboto', sans-serif;
margin: 0;
padding: 0;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
background-color: #f5f5f5;
}
.mdc-card {
width: 90%;
max-width: 600px;
padding: 20px;
margin: 20px;
}
.file-input {
margin-bottom: 20px;
}
.output {
margin-top: 20px;
white-space: pre-wrap;
word-wrap: break-word;
}
</style>
</head>
<body>
<!-- Top App Bar -->
<header class="mdc-top-app-bar">
<div class="mdc-top-app-bar__row">
<section class="mdc-top-app-bar__section mdc-top-app-bar__section--align-start">
<span class="mdc-top-app-bar__title">PDF Table Extractor</span>
</section>
</div>
</header>
<!-- Main Content -->
<div class="mdc-card">
<h2 class="mdc-typography--headline6">Upload a PDF File</h2>
<input type="file" id="pdfFileInput" class="file-input" accept=".pdf">
<button class="mdc-button mdc-button--raised" id="extractButton">
<span class="mdc-button__label">Extract Tables</span>
</button>
<div class="output" id="output"></div>
</div>
<!-- Embedded JavaScript -->
<script>
// Embedded pdf.js library (minified version)
const pdfjsLib = (() => {
var exports = {};
var require = (module) => {
if (module === "pdfjs-dist/build/pdf") return exports;
throw new Error("Module not found: " + module);
};
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).pdfjsDistBuildPdf={})}(this,(e=>{var t,r,n,i,o,a,s,u,c,l,f,h,p,d,g,m,v,y,w,b,k,_,E,S,A,x,T,C,N,M,L,R,P,O,U,D,F,H,B,W,G,z,q,Y,X,V,j,K,Q,J,Z,$,ee,te,re,ne,ie,oe,ae,se,ue,ce,le,fe,he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ss,us,cs,ls,fs,hs,ps,ds,gs,ms,vs,ys,ws,bs,ks,_s,Es,Ss,As,xs,Ts,Cs,Ns,Ms,Ls,Rs,Ps,Os,Us,Ds,Fs,Hs,Bs,Ws,Gs,zs,qs,Ys,Xs,Vs,js,Ks,Qs,Js,Zs,$s,et,tt,rt,nt,it,ot,at,st,ut,ct,lt,ft,ht,pt,dt,gt,mt,vt,yt,wt,bt,kt,_t,Et,St,At,xt,Tt,Ct,Nt,Mt,Lt,Rt,Pt,Ot,Ut,Dt,Ft,Ht,Bt,Wt,Gt,zt,qt,Yt,Xt,Vt,jt,Kt,Qt,Jt,Zt,$t,er,tr,rr,nr,ir,or,ar,sr,ur,cr,lr,fr,hr,pr,dr,gr,mr,vr,yr,wr,br,kr,_r,Er,Sr,Ar,xr,Tr,Cr,Nr,Mr,Lr,Rr,Pr,Or,Ur,Dr,Fr,Hr,Br,Wr,Gr,zr,qr,Yr,Xr,Vr,jr,Kr,Qr,Jr,Zr,$r,es,ts,rs,ns,is,os,as,ss,us,cs,ls,fs,hs,ps,ds,gs,ms,vs,ys,ws,bs,ks,_s,Es,Ss,As,xs,Ts,Cs,Ns,Ms,Ls,Rs,Ps,Os,Us,Ds,Fs,Hs,Bs,Ws,Gs,zs,qs,Ys,Xs,Vs,js,Ks,Qs,Js,Zs,$s;
return exports;
})();
document.addEventListener('DOMContentLoaded', () => {
const pdfFileInput = document.getElementById('pdfFileInput');
const extractButton = document.getElementById('extractButton');
const outputDiv = document.getElementById('output');
extractButton.addEventListener('click', async () => {
const file = pdfFileInput.files[0];
if (!file) {
outputDiv.textContent = 'Please upload a PDF file.';
return;
}
try {
const pdfData = await loadPdf(file);
const tables = extractTables(pdfData);
if (tables.length === 0) {
outputDiv.textContent = 'No tables found in the PDF.';
return;
}
// Display extracted tables
outputDiv.innerHTML = '';
tables.forEach((table, index) => {
const csvContent = table.map(row => row.join(',')).join('\n');
const blob = new Blob([csvContent], { type: 'text/csv' });
const url = URL.createObjectURL(blob);
const downloadLink = document.createElement('a');
downloadLink.href = url;
downloadLink.download = `table_${index + 1}.csv`;
downloadLink.textContent = `Download Table ${index + 1} as CSV`;
const tableDiv = document.createElement('div');
tableDiv.style.marginBottom = '10px';
tableDiv.appendChild(downloadLink);
outputDiv.appendChild(tableDiv);
});
} catch (error) {
outputDiv.textContent = `Error processing PDF: ${error.message}`;
}
});
async function loadPdf(file) {
const arrayBuffer = await file.arrayBuffer();
const pdf = await pdfjsLib.getDocument({ data: arrayBuffer }).promise;
const numPages = pdf.numPages;
const pages = [];
for (let i = 1; i <= numPages; i++) {
const page = await pdf.getPage(i);
const textContent = await page.getTextContent();
const items = textContent.items.map(item => item.str);
pages.push(items);
}
return pages;
}
function extractTables(pages) {
const tables = [];
let currentTable = [];
pages.forEach(pageItems => {
let inTable = false;
pageItems.forEach(item => {
// Simple heuristic to detect table rows (e.g., rows with commas or separators)
if (item.includes(',') || item.includes('|') || item.match(/\d+\s*\.\s*\d+/)) {
inTable = true;
currentTable.push(item.split(/[\s,|]+/).filter(Boolean));
} else if (inTable) {
// End of table
if (currentTable.length > 0) {
tables.push(currentTable);
currentTable = [];
}
inTable = false;
}
});
// Add any remaining table rows at the end of the page
if (currentTable.length > 0) {
tables.push(currentTable);
currentTable = [];
}
});
return tables;
}
});
</script>
</body>
</html>