From 944085169491897a18f8b6f587a1dd24e9d07d07 Mon Sep 17 00:00:00 2001 From: Naman-B-Parlecha Date: Sun, 5 Jan 2025 22:58:42 +0530 Subject: [PATCH 1/2] Fixing PDF generation --- eduaid_web/src/pages/Output.jsx | 49 ++++++++++++++++++++++++--------- 1 file changed, 36 insertions(+), 13 deletions(-) diff --git a/eduaid_web/src/pages/Output.jsx b/eduaid_web/src/pages/Output.jsx index e1b24248..126aec1f 100644 --- a/eduaid_web/src/pages/Output.jsx +++ b/eduaid_web/src/pages/Output.jsx @@ -58,17 +58,13 @@ const Output = () => { }); } - if (questionType === "get_mcq") { + if (questionType === "get_mcq") { qaPairsFromStorage["output"].forEach((qaPair) => { - const options = qaPair.answer - .filter((ans) => !ans.correct) - .map((ans) => ans.answer); - const correctAnswer = qaPair.answer.find( - (ans) => ans.correct - )?.answer; + const options = qaPair.options; + const correctAnswer = qaPair.answer; combinedQaPairs.push({ - question: qaPair.question, + question: qaPair.question_statement, question_type: "MCQ_Hard", options: options, answer: correctAnswer, @@ -131,18 +127,40 @@ const Output = () => { let y = 700; // Starting y position for content let questionIndex = 1; + console.log("here inside downloading", qaPairs) + qaPairs.forEach((qaPair) => { if (y < 50) { page = pdfDoc.addPage(); y = 700; } - page.drawText(`Q${questionIndex}) ${qaPair.question}`, { - x: 50, - y, - size: 15, + // i'm implementing a question text wrapping logic so that it doesn't overflow the page + const questionText = `Q${questionIndex}) ${qaPair.question}`; + const maxLineLength = 67; + const lines = []; + + let start = 0; + while (start < questionText.length) { + let end = start + maxLineLength; + if (end < questionText.length && questionText[end] !== ' ') { + while (end > start && questionText[end] !== ' ') { + end--; + } + } + if (end === start) { + end = start + maxLineLength; + } + lines.push(questionText.substring(start, end).trim()); + start = end + 1; + } + + lines.forEach((line) => { + page.drawText(line, { x: 50, y, size: 15 }); + y -= 20; }); - y -= 30; + + y -= 10; if (qaPair.question_type === "Boolean") { // Create radio buttons for True/False @@ -190,6 +208,11 @@ const Output = () => { }; drawRadioButton(option, false); }); + + if (questionIndex % 5 === 0) { + page = pdfDoc.addPage(); + y = 700; + } } else if (qaPair.question_type === "Short") { // Text field for Short answer const answerField = form.createTextField( From 0c37e740c874289363c287ac0b3a8bf5162904ae Mon Sep 17 00:00:00 2001 From: Naman-B-Parlecha Date: Thu, 9 Jan 2025 13:18:48 +0530 Subject: [PATCH 2/2] chore(app): Refactoring --- eduaid_web/src/pages/Output.jsx | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/eduaid_web/src/pages/Output.jsx b/eduaid_web/src/pages/Output.jsx index 126aec1f..0985ace7 100644 --- a/eduaid_web/src/pages/Output.jsx +++ b/eduaid_web/src/pages/Output.jsx @@ -127,8 +127,6 @@ const Output = () => { let y = 700; // Starting y position for content let questionIndex = 1; - console.log("here inside downloading", qaPairs) - qaPairs.forEach((qaPair) => { if (y < 50) { page = pdfDoc.addPage(); @@ -194,8 +192,8 @@ const Output = () => { `question${questionIndex}_answer` ); - options.forEach((option, index) => { - const drawRadioButton = (text, selected) => { + options.forEach((option) => { + const drawRadioButton = (text) => { const radioOptions = { x: 70, y, @@ -208,11 +206,6 @@ const Output = () => { }; drawRadioButton(option, false); }); - - if (questionIndex % 5 === 0) { - page = pdfDoc.addPage(); - y = 700; - } } else if (qaPair.question_type === "Short") { // Text field for Short answer const answerField = form.createTextField(