diff --git a/src/package.json b/src/package.json index 3c868bca42caa758aae7ffc68be2066740bfe285..2baa1967e456300d5933ea2b8442a3ac2722dedb 100644 --- a/src/package.json +++ b/src/package.json @@ -15,8 +15,6 @@ "bluebird": "^3.7.2", "cassandra-driver": "^4.4.0", "chai-sorted": "^0.2.0", - "cheerio": "^1.0.0-rc.5", - "cheerio-tableparser": "^1.0.1", "compression": "^1.7.4", "cookie-parser": "~1.4.4", "dateformat": "^3.0.3", diff --git a/src/routes/print.js b/src/routes/print.js index 4fc1e9c97217142bb42af8d5a3455d39fb489c36..d84c733306fd9154128fc8b58c7c12261680fb7d 100644 --- a/src/routes/print.js +++ b/src/routes/print.js @@ -29,7 +29,7 @@ async function printPDF(req, res) { content_id: id, base64string: binary, }, - }; + }; res.send(resJSON); } else { res.contentType(`application/pdf; name=${id}.pdf`); diff --git a/src/service/print/pdf.js b/src/service/print/pdf.js index 4202ea33137bcd21b501bb04502c5672f4f9862b..80693f75fa53cf1e5bb856c8b81f4d1b2be9b335 100644 --- a/src/service/print/pdf.js +++ b/src/service/print/pdf.js @@ -1,5 +1,6 @@ var PdfPrinter = require("pdfmake"); const { getData } = require("./dataImporter"); + var { docDefinition, getStudentTeacherDetails, @@ -15,13 +16,7 @@ var { getSA, getVSA, getLA, - getComprehension, - getMTFHeader, - getMTFChoice, } = require("./utils/docDefinition"); -var cheerio = require("cheerio"); -var cheerioTableparser = require("cheerio-tableparser"); - // /Users/apple/chaks/sunbird/exp/program-service/src/service/print/service/print/utils/fonts/Roboto/DroidSans-Bold.ttf var fonts = { Roboto: { @@ -69,7 +64,6 @@ const buildPDF = async (id) => { const buildPDFWithCallback = (id, callback) => { let error = false; let errorMsg = ""; - let totalMarks = 0; getData(id) .then((data) => { if (data.error) { @@ -82,19 +76,13 @@ const buildPDFWithCallback = (id, callback) => { let language = data.paperData.medium[0]; // const language = "Noto"; - data.sectionData.forEach((d) => { - d.questions.forEach((element, index) => { - const marks = parseInt(d.section.children[index].marks); - if (!isNaN(marks)) totalMarks += marks; - }); - }); const contentBase = [ getStudentTeacherDetails(), getExamName(examName), getGradeHeader(grade), getSubject(subject), - getTimeAndMarks(90, totalMarks), + getTimeAndMarks(), getInstructions(instructions, language), ]; @@ -114,26 +102,22 @@ const buildPDFWithCallback = (id, callback) => { questionCounter += 1; const marks = section.children[index].marks; let questionContent; - try { + try { if (question.category === "MCQ") - questionContent = [renderMCQ(question, questionCounter, marks)]; + questionContent = renderMCQ(question, questionCounter, marks); else if (question.category === "FTB") { - questionContent = [renderFTB(question, questionCounter, marks)]; + questionContent = renderFTB(question, questionCounter, marks); } else if (question.category === "SA") { - questionContent = [renderSA(question, questionCounter, marks)]; + questionContent = renderSA(question, questionCounter, marks); } else if (question.category === "LA") { - questionContent = [renderLA(question, questionCounter, marks)]; + questionContent = renderLA(question, questionCounter, marks); } else if (question.category === "VSA") { - questionContent = [renderVSA(question, questionCounter, marks)]; + questionContent = renderVSA(question, questionCounter, marks); } else if (question.category === "TF") { - questionContent = [renderTF(question, questionCounter, marks)]; - } else if (question.category === "MTF") { - questionContent = renderMTF(question, questionCounter, marks); - } else if(question.category === 'COMPREHENSION') { - questionContent = [renderComprehension(question, questionCounter, marks)] - } + questionContent = renderTF(question, questionCounter, marks); + } - questionPaperContent.push(...questionContent); + questionPaperContent.push(questionContent); } catch (e) { console.log(e); } @@ -163,9 +147,9 @@ const buildPDFWithCallback = (id, callback) => { }); }; -const cleanHTML = (str, nbspAsLineBreak = false) => { +const cleanHTML = (str) => { // Remove HTML characters since we are not converting HTML to PDF. - return str.replace(/<[^>]+>/g, "").replace(/ /g, nbspAsLineBreak ? "\n" : ""); + return str.replace(/<[^>]+>/g, "").replace(" ", ""); }; const detectLanguage = (str) => { @@ -244,12 +228,6 @@ function renderLA(question, questionCounter, marks) { return getLA(questionTitle, detectLanguage(questionTitle[0]), marks); } -function renderComprehension(question, questionCounter, marks) { - const questionTitle = - questionCounter + "." + cleanHTML(question.editorState.question, true); - return getComprehension(questionTitle, detectLanguage(questionTitle[0], marks)); -} - function renderVSA(question, questionCounter, marks) { const questionTitle = questionCounter + ". " + cleanHTML(question.editorState.question); @@ -262,35 +240,6 @@ function renderTF(question, questionCounter, marks) { return getTF(questionTitle, detectLanguage(questionTitle[0]), marks); } -function renderMTF(question, questionCounter, marks) { - $ = cheerio.load(question.editorState.question); - cheerioTableparser($); - var data = []; - var columns = $("table").parsetable(false, false, false); - let transposeColumns = columns[0].map((_, colIndex) => - columns.map((row) => row[colIndex]) - ); - - const heading = questionCounter + ". " + cleanHTML($("p").text()); - data.push(getFTB(heading, detectLanguage(heading), marks)); - - data.push( - getMTFHeader( - cleanHTML(transposeColumns[0][0]), - cleanHTML(transposeColumns[0][1]), - detectLanguage(cleanHTML(transposeColumns[0][0])) - ) - ); - - transposeColumns.shift(); - - const rows = transposeColumns.map((r) => { - return getMTFChoice(cleanHTML(r[0]), cleanHTML(r[1]), detectLanguage(r[0])); - }); - - return data.concat(rows); -} - module.exports = { buildPDF, buildPDFWithCallback, diff --git a/src/service/print/utils/docDefinition.js b/src/service/print/utils/docDefinition.js index 218b3236727ab619c96e1dbab2ae285694966cdd..787e39126c7544710eef833c8ad84df11fd78828 100644 --- a/src/service/print/utils/docDefinition.js +++ b/src/service/print/utils/docDefinition.js @@ -47,69 +47,27 @@ const docDefinition = { styles: styles, }; -function getComprehension(questionTitle, language, marks) { +function getLA() { return { - table: { - widths: ['100%', 'auto'], - body: [ - [ - { - border: [false, false, false, false], - text: questionTitle, - style: "question_COMPREHENSION", - font: language - }, - { - border: [false, false, false, false], - text: marks, - style: "header_right" - } - ] - ] - } - } -} + text: "21. What advice did the old bird give to the other birds?", + style: "question_LA", -function getLA(questionTitle, language, marks) { - return { - // Time + Marks table: { - widths: ["*", "auto"], + widths: ["*"], body: [ [ { border: [false, false, false, false], - // Question LA - text: questionTitle, + text: "21. What advice did the old bird give to the other birds?", style: "question_LA", - font: language, - - table: { - widths: ["*"], - body: [ - [ - { - border: [false, false, false, false], - text: questionTitle, - style: "question_LA", - font: language, - }, - ], - [ - { - border: [false, false, false, false], - text: - "_______________________________________________________________________________________\n\n_______________________________________________________________________________________\n\n_______________________________________________________________________________________\n\n_______________________________________________________________________________________\n\n_______________________________________________________________________________________\n\n_______________________________________________________________________________________\n\n_______________________________________________________________________________________\n\n_______________________________________________________________________________________\n\n_______________________________________________________________________________________\n\n_______________________________________________________________________________________\n\n_______________________________________________________________________________________", - style: "question_VSA", - }, - ], - ], - }, }, + ], + [ { border: [false, false, false, false], - text: marks, - style: "header_right", + text: + "____________________________________________________________________________________________\n\n____________________________________________________________________________________________\n\n____________________________________________________________________________________________\n\n____________________________________________________________________________________________\n\n____________________________________________________________________________________________\n\n____________________________________________________________________________________________\n\n____________________________________________________________________________________________\n\n____________________________________________________________________________________________\n\n____________________________________________________________________________________________\n\n____________________________________________________________________________________________\n\n____________________________________________________________________________________________", + style: "question_VSA", }, ], ], @@ -120,38 +78,22 @@ function getLA(questionTitle, language, marks) { function getVSA(questionTitle, language, marks) { return { table: { - widths: ["*", "auto"], + widths: ["*"], body: [ [ { border: [false, false, false, false], - // Question VSA - table: { - widths: ["*"], - body: [ - [ - { - border: [false, false, false, false], - text: questionTitle, - style: "question_VSA", - font: language, - }, - ], - [ - { - border: [false, false, false, false], - text: - "______________________________________________________________________________________", - style: "question_VSA", - }, - ], - ], - }, + text: questionTitle, + style: "question_VSA", + font: language, }, + ], + [ { border: [false, false, false, false], - text: marks, - style: "header_right", + text: + "____________________________________________________________________________________________", + style: "question_VSA", }, ], ], @@ -225,60 +167,6 @@ function getTF(questionTitle, language, marks) { }; } -function getMTFHeader(left, right, language) { - return { - table: { - widths: ["*", "*"], - body: [ - [ - { - border: [false, false, true, true], - margin: [40, 0], - text: left, - style: "question_MTF", - font: language, - bold: "true", - }, - { - border: [false, false, false, true], - margin: [40, 0], - text: right, - style: "question_MTF", - bold: "true", - font: language, - }, - ], - ], - }, - }; -} - -function getMTFChoice(left, right, language) { - return { - table: { - widths: ["*", "*"], - body: [ - [ - { - border: [false, false, true, true], - text: left, - margin: [40, 0], - style: "question_MTF", - font: language, - }, - { - border: [false, false, false, true], - text: right, - margin: [40, 0], - style: "question_MTF", - font: language, - }, - ], - ], - }, - }; -} - function getFTB(questionTitle, language, marks) { return { // Time + Marks @@ -395,17 +283,22 @@ function getInstructions(instructions, language) { }; } -function getTimeAndMarks(time, marks) { +function getTimeAndMarks() { return { table: { - widths: ["*"], + widths: ["*", "*"], body: [ [ { border: [false, false, false, false], - text: "Marks - " + marks, + text: "Time - " + "90" + "Minutes", style: "header_left", }, + { + border: [false, false, false, false], + text: "Marks - " + "100", + style: "header_right", + }, ], ], }, @@ -434,7 +327,4 @@ module.exports = { getSA, getVSA, getLA, - getComprehension, - getMTFHeader, - getMTFChoice, }; diff --git a/src/service/print/utils/docStyles.js b/src/service/print/utils/docStyles.js index cfe55b98970129bfb9c16554e2c222efd984ebd9..df0a912963a5c2d7ba3bbd9294dc739bcbc844b7 100644 --- a/src/service/print/utils/docStyles.js +++ b/src/service/print/utils/docStyles.js @@ -48,14 +48,6 @@ const styles = { fontSize: 12, margin: [5, 5, 0, 5], }, - question_MTF: { - fontSize: 12, - margin: [5, 5, 0, 5], - }, - question_COMPREHENSION: { - fontSize: 12, - margin: [5, 5, 0, 5] -} }; function getInstructionStyle() { diff --git a/src/test/service/printService.js b/src/test/service/printService.js index a052e654577bf44485cde90c06378de7d05c9170..e89470be03755eb64e0f9fa1483349a920054e04 100644 --- a/src/test/service/printService.js +++ b/src/test/service/printService.js @@ -24,9 +24,6 @@ const getQuestionForSection = dataImporter.__get__("getQuestionForSection"); const getItemsFromItemset = dataImporter.__get__("getItemsFromItemset"); const getQuestionFromItem = dataImporter.__get__("getQuestionFromItem"); -var cheerio = require("cheerio"); -var cheerioTableparser = require("cheerio-tableparser"); - // eslint-disable-next-line no-undef describe("Print Service", () => { it("[Integration test] should output error for wrong heirarchy ID", (done) => { @@ -122,7 +119,7 @@ describe("Print Service", () => { "do_113213251762339840191", (base64PDF, error, errorMsg) => { expect(error).to.be.false; - expect(errorMsg).to.equal(""); + expect(errorMsg).to.equal(""); done(); } ); @@ -146,19 +143,4 @@ describe("Print Service", () => { done(e); }); }); - - it("Should parse table", (done) => { - const table = `<p>Match the following:</p><figure class="table"><table><tbody><tr><td><strong>Column 1</strong></td><td><strong>Column 2</strong></td></tr><tr><td>1</td><td>1</td></tr></tbody></table></figure>`; - $ = cheerio.load(table); - cheerioTableparser($); - var data = []; - var columns = $("table").parsetable(false, false, false); - const transposeColumns = columns[0].map((_, colIndex) => - columns.map((row) => row[colIndex]) - ); - const heading = $("p").text(); - - console.log(heading); - done(); - }); });