Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
sankeer28 committed Feb 7, 2024
1 parent f63d5f1 commit 5b86700
Show file tree
Hide file tree
Showing 46 changed files with 55 additions and 21 deletions.
Binary file added images/1.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/10.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/10.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/11.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/12.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/13.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/14.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/15.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/16.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/17.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/18.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/19.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/2.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/20.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/21.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/22.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/23.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/24.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/25.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/26.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/27.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/28.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/29.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/3.jpeg
Binary file added images/3.png
Binary file added images/30.png
Binary file added images/31.png
Binary file added images/32.png
Binary file added images/33.png
Binary file added images/34.png
Binary file added images/4.jpeg
Binary file added images/4.png
Binary file added images/5.jpeg
Binary file added images/5.png
Binary file added images/6.jpeg
Binary file added images/6.png
Binary file added images/7.jpeg
Binary file added images/7.png
Binary file added images/8.jpeg
Binary file added images/8.png
Binary file added images/9.jpeg
Binary file added images/9.png
7 changes: 6 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,16 @@
<div id="main-content">
<div id="container">
<h1>Math PDF Generator</h1>
<form id="pdfForm" onsubmit="event.preventDefault(); generatePDF(document.getElementById('numPDFs').value);">
<form id="pdfForm" onsubmit="event.preventDefault(); generatePDF(document.getElementById('numPDFs').value, document.getElementById('numPages').value);">
<label for="numPDFs">Number of PDFs to generate:</label>
<input type="number" id="numPDFs" name="numPDFs" min="1" value="20">

<label for="numPages">Number of pages per PDF:</label>
<input type="number" id="numPages" name="numPages" min="1" max="50" value="5">

<button type="submit">Generate PDFs</button>
</form>

<div id="progress-message" style="margin-top: 10px;"></div>

</div>
Expand Down
69 changes: 49 additions & 20 deletions script.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

function generateRandomFilename() {
var result = 'math';
var numbers = '0123456789';
Expand All @@ -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);
}
}
}
Expand All @@ -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'));
}
Expand All @@ -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);
});
var numPDFs = document.getElementById('numPDFs').value;
var numPages = document.getElementById('numPages').value;
generatePDF(numPDFs, numPages);
});

0 comments on commit 5b86700

Please sign in to comment.