diff --git a/images/1.jpeg b/images/1.jpeg new file mode 100644 index 0000000..5c30b4f Binary files /dev/null and b/images/1.jpeg differ diff --git a/images/1.png b/images/1.png new file mode 100644 index 0000000..50544d7 Binary files /dev/null and b/images/1.png differ diff --git a/images/10.jpeg b/images/10.jpeg new file mode 100644 index 0000000..8b982f9 Binary files /dev/null and b/images/10.jpeg differ diff --git a/images/10.png b/images/10.png new file mode 100644 index 0000000..bfe89a2 Binary files /dev/null and b/images/10.png differ diff --git a/images/11.png b/images/11.png new file mode 100644 index 0000000..355d242 Binary files /dev/null and b/images/11.png differ diff --git a/images/12.png b/images/12.png new file mode 100644 index 0000000..603ccf3 Binary files /dev/null and b/images/12.png differ diff --git a/images/13.png b/images/13.png new file mode 100644 index 0000000..6f54d4e Binary files /dev/null and b/images/13.png differ diff --git a/images/14.png b/images/14.png new file mode 100644 index 0000000..d478e4c Binary files /dev/null and b/images/14.png differ diff --git a/images/15.png b/images/15.png new file mode 100644 index 0000000..a4d8e60 Binary files /dev/null and b/images/15.png differ diff --git a/images/16.png b/images/16.png new file mode 100644 index 0000000..67db710 Binary files /dev/null and b/images/16.png differ diff --git a/images/17.png b/images/17.png new file mode 100644 index 0000000..de2d6b4 Binary files /dev/null and b/images/17.png differ diff --git a/images/18.png b/images/18.png new file mode 100644 index 0000000..b14ecb7 Binary files /dev/null and b/images/18.png differ diff --git a/images/19.png b/images/19.png new file mode 100644 index 0000000..1adc1ec Binary files /dev/null and b/images/19.png differ diff --git a/images/2.jpeg b/images/2.jpeg new file mode 100644 index 0000000..877a93f Binary files /dev/null and b/images/2.jpeg differ diff --git a/images/2.png b/images/2.png new file mode 100644 index 0000000..0a4d03e Binary files /dev/null and b/images/2.png differ diff --git a/images/20.png b/images/20.png new file mode 100644 index 0000000..13c5c16 Binary files /dev/null and b/images/20.png differ diff --git a/images/21.png b/images/21.png new file mode 100644 index 0000000..cbc5505 Binary files /dev/null and b/images/21.png differ diff --git a/images/22.png b/images/22.png new file mode 100644 index 0000000..d527605 Binary files /dev/null and b/images/22.png differ diff --git a/images/23.png b/images/23.png new file mode 100644 index 0000000..9637381 Binary files /dev/null and b/images/23.png differ diff --git a/images/24.png b/images/24.png new file mode 100644 index 0000000..54aa825 Binary files /dev/null and b/images/24.png differ diff --git a/images/25.png b/images/25.png new file mode 100644 index 0000000..8dea02a Binary files /dev/null and b/images/25.png differ diff --git a/images/26.png b/images/26.png new file mode 100644 index 0000000..98d1ac5 Binary files /dev/null and b/images/26.png differ diff --git a/images/27.png b/images/27.png new file mode 100644 index 0000000..2d3fd76 Binary files /dev/null and b/images/27.png differ diff --git a/images/28.png b/images/28.png new file mode 100644 index 0000000..d8440e5 Binary files /dev/null and b/images/28.png differ diff --git a/images/29.png b/images/29.png new file mode 100644 index 0000000..d538851 Binary files /dev/null and b/images/29.png differ diff --git a/images/3.jpeg b/images/3.jpeg new file mode 100644 index 0000000..0978edc Binary files /dev/null and b/images/3.jpeg differ diff --git a/images/3.png b/images/3.png new file mode 100644 index 0000000..8ffb81e Binary files /dev/null and b/images/3.png differ diff --git a/images/30.png b/images/30.png new file mode 100644 index 0000000..fc74634 Binary files /dev/null and b/images/30.png differ diff --git a/images/31.png b/images/31.png new file mode 100644 index 0000000..619c83e Binary files /dev/null and b/images/31.png differ diff --git a/images/32.png b/images/32.png new file mode 100644 index 0000000..849f8a1 Binary files /dev/null and b/images/32.png differ diff --git a/images/33.png b/images/33.png new file mode 100644 index 0000000..6ecc3b6 Binary files /dev/null and b/images/33.png differ diff --git a/images/34.png b/images/34.png new file mode 100644 index 0000000..163f83a Binary files /dev/null and b/images/34.png differ diff --git a/images/4.jpeg b/images/4.jpeg new file mode 100644 index 0000000..de10ecb Binary files /dev/null and b/images/4.jpeg differ diff --git a/images/4.png b/images/4.png new file mode 100644 index 0000000..aa0e969 Binary files /dev/null and b/images/4.png differ diff --git a/images/5.jpeg b/images/5.jpeg new file mode 100644 index 0000000..746da90 Binary files /dev/null and b/images/5.jpeg differ diff --git a/images/5.png b/images/5.png new file mode 100644 index 0000000..32bee46 Binary files /dev/null and b/images/5.png differ diff --git a/images/6.jpeg b/images/6.jpeg new file mode 100644 index 0000000..79c87c0 Binary files /dev/null and b/images/6.jpeg differ diff --git a/images/6.png b/images/6.png new file mode 100644 index 0000000..b6bc4e4 Binary files /dev/null and b/images/6.png differ diff --git a/images/7.jpeg b/images/7.jpeg new file mode 100644 index 0000000..23691f1 Binary files /dev/null and b/images/7.jpeg differ diff --git a/images/7.png b/images/7.png new file mode 100644 index 0000000..f97460f Binary files /dev/null and b/images/7.png differ diff --git a/images/8.jpeg b/images/8.jpeg new file mode 100644 index 0000000..9467607 Binary files /dev/null and b/images/8.jpeg differ diff --git a/images/8.png b/images/8.png new file mode 100644 index 0000000..df1745c Binary files /dev/null and b/images/8.png differ diff --git a/images/9.jpeg b/images/9.jpeg new file mode 100644 index 0000000..2b5c528 Binary files /dev/null and b/images/9.jpeg differ diff --git a/images/9.png b/images/9.png new file mode 100644 index 0000000..81631fb Binary files /dev/null and b/images/9.png differ diff --git a/index.html b/index.html index 10022e8..7e8df22 100644 --- a/index.html +++ b/index.html @@ -12,11 +12,16 @@

Math PDF Generator

-
+ + + + +
+
diff --git a/script.js b/script.js index dd2363d..55cd14c 100644 --- a/script.js +++ b/script.js @@ -1,3 +1,4 @@ + function generateRandomFilename() { var result = 'math'; var numbers = '0123456789'; @@ -13,36 +14,61 @@ function generateRandomFilename() { return result; } -function generatePDF(numPDFs=1, batchSize=20) { - var operators = ['+', '-', '*', '/']; +var images = [ + 'images/1.jpeg', 'images/2.jpeg', 'images/3.jpeg', 'images/4.jpeg', + 'images/5.jpeg', 'images/6.jpeg', 'images/7.jpeg', 'images/8.jpeg', + 'images/9.jpeg', 'images/10.jpeg' +]; + +async function loadImage(src) { + return new Promise((resolve, reject) => { + let img = new Image(); + img.onload = () => resolve(img); + img.onerror = reject; + img.src = src; + }); +} + +async function generatePDF(numPDFs=1, numPages=5, batchSize=20) { var zip = new JSZip(); var progressMessage = document.getElementById('progress-message'); - function generateBatch(startIndex, endIndex) { + async function generateBatch(startIndex, endIndex) { var startTime = Date.now(); for (var i = startIndex; i < endIndex; i++) { var doc = new jsPDF(); var answers = []; - for (var p = 0; p < 5; p++) { + var totalQuestions = 0; + doc.setFontSize(20); + doc.text("Math Problems", 105, 10, null, null, 'center'); + var imgSrc = images[Math.floor(Math.random() * images.length)]; + var img = await loadImage(imgSrc); + doc.addImage(img, 'JPEG', 15, 40, 180, 160); + + doc.addPage(); + + for (var p = 0; p < numPages; p++) { if (p != 0) { doc.addPage(); } doc.setFontSize(20); doc.text("Math Problems", 105, 10, null, null, 'center'); doc.setFontSize(12); - for (var j = 0; j < 26; j++) { + for (var j = 0; j < 23; j++) { var numOperands = Math.floor(Math.random() * 4) + 2; - for (var c = 0; c < 3; c++) { + for (var c = 0; c < 3; c++) { var equation = ""; + var numOperands = Math.floor(Math.random() * 4) + 2; + var operator = ['+', '-', '*', '/']; for (var k = 0; k < numOperands; k++) { - equation += Math.floor(Math.random() * 10) + " " + operators[Math.floor(Math.random() * operators.length)] + " "; + equation += Math.floor(Math.random() * 10) + " " + operator[Math.floor(Math.random() * 4)] + " "; } - equation = equation.slice(0, -2); - var answer = eval(equation).toFixed(2); + equation = equation.slice(0, -2) + " ="; + var answer = eval(equation.slice(0, -2)).toFixed(2); answers.push(answer); - var equation_number = (p === 0 ? p*78 : p*78 - 3) + j*3 + c + 1; + totalQuestions++; doc.setFontSize(12); - doc.text(equation_number + ") " + equation + " =", 10 + c*60, 20 + j * 10); + doc.text(totalQuestions + ") " + equation, 10 + c*70, 20 + j * 12); } } } @@ -51,10 +77,10 @@ function generatePDF(numPDFs=1, batchSize=20) { doc.text("Answer Key", 105, 10, null, null, 'center'); doc.setFontSize(12); for (var idx = 0; idx < answers.length; idx++) { - if (idx != 0 && idx % 78 == 0) { + if (idx != 0 && idx % 69 == 0) { doc.addPage(); } - doc.text((idx + 1) + ") " + answers[idx], 10 + (idx % 3) * 60, 20 + Math.floor((idx % 78) / 3) * 10); + doc.text((idx + 1) + ") " + answers[idx], 10 + (idx % 3) * 70, 20 + Math.floor((idx % 69) / 3) * 12); } zip.file(generateRandomFilename() + '.pdf', doc.output('blob')); } @@ -64,17 +90,20 @@ function generatePDF(numPDFs=1, batchSize=20) { } var numBatches = Math.ceil(numPDFs / batchSize); + var batchPromises = []; for (var i = 0; i < numBatches; i++) { - generateBatch(i * batchSize, Math.min((i + 1) * batchSize, numPDFs)); + batchPromises.push(generateBatch(i * batchSize, Math.min((i + 1) * batchSize, numPDFs))); } - zip.generateAsync({type:"blob"}).then(function(content) { - saveAs(content, "math_pdfs.zip"); + Promise.all(batchPromises).then(function() { + zip.generateAsync({type:"blob"}).then(function(content) { + saveAs(content, "math_pdfs.zip"); + }); }); } - document.getElementById('generateButton').addEventListener('click', function(event) { event.preventDefault(); - progressMessage.textContent = 'Starting PDF generation...'; - generatePDF(100, 20); -}); \ No newline at end of file + var numPDFs = document.getElementById('numPDFs').value; + var numPages = document.getElementById('numPages').value; + generatePDF(numPDFs, numPages); +});