From b604cb63c8b4a28e4c31708460463ea492cf696e Mon Sep 17 00:00:00 2001 From: Arpit Chakladar <54011232+arpitchakladar@users.noreply.github.com> Date: Tue, 11 Apr 2023 20:37:34 +0530 Subject: Using a seperate HTML file for the generating the PDF. Putting the footer at the bottom of each page. --- config/tsconfig.json | 3 + config/webpack.config.js | 6 +- config/webpack/loader/to-string-loader.js | 41 ++++ src/html.d.ts | 3 + .../download-information-pdf-page-content.html | 273 +++++++++++++++++++++ .../download-information-pdf-page-content.ts | 219 ----------------- src/scripts/functionality/view-khatian/index.ts | 15 +- src/shared/download-pdf.ts | 17 -- src/shared/generate-web-page.ts | 9 + 9 files changed, 343 insertions(+), 243 deletions(-) create mode 100644 config/webpack/loader/to-string-loader.js create mode 100644 src/html.d.ts create mode 100644 src/scripts/functionality/view-khatian/download-information-pdf-page-content.html delete mode 100644 src/scripts/functionality/view-khatian/download-information-pdf-page-content.ts delete mode 100644 src/shared/download-pdf.ts create mode 100644 src/shared/generate-web-page.ts diff --git a/config/tsconfig.json b/config/tsconfig.json index 120bfcb..ec61595 100644 --- a/config/tsconfig.json +++ b/config/tsconfig.json @@ -1,4 +1,7 @@ { + "files": [ + "../src/html.d.ts" + ], "compilerOptions": { "outDir": "dist", "sourceMap": true, diff --git a/config/webpack.config.js b/config/webpack.config.js index 31bbb8d..2ae6544 100644 --- a/config/webpack.config.js +++ b/config/webpack.config.js @@ -84,7 +84,7 @@ const commonOptions = { mode: "production", target: "web", resolve: { - extensions: [".ts", ".js"], + extensions: [".ts", ".js", ".html"], alias: { "@": global.SOURCE_DIR } @@ -97,6 +97,10 @@ const commonOptions = { options: { configFile: "config/tsconfig.json" } + }, + { + test: /\.html$/, + loader: path.resolve(CONFIG_DIR, "webpack/loader/to-string-loader.js") } ] }, diff --git a/config/webpack/loader/to-string-loader.js b/config/webpack/loader/to-string-loader.js new file mode 100644 index 0000000..cf26b28 --- /dev/null +++ b/config/webpack/loader/to-string-loader.js @@ -0,0 +1,41 @@ +module.exports = function(source) { + source = source + .replaceAll("\\", "\\\\") + .replaceAll("`","\\`") + .replaceAll("${", "\\${"); + + const replacements = {}; + let i = 0; + while (true) { + i = source.indexOf("$", i) + 1; + + if (i <= 0) { + break; + } + + const end = source.indexOf("$", i); + + if (end < 0) { + break; + } + + const replacementVariable = source.substring(i, end); + replacements[replacementVariable] = 1; + source = source.substring(0, i - 1) + `\${${replacementVariable}}` + source.substring(end + 1); + i = end + 1; + } + + source = `\`${source}\``; + + const replacementList = []; + + for (const replacement in replacements) { + replacementList.push(replacement); + } + + const replacementArguments = replacementList.length > 0 + ? `{ ${replacementList.join(", ")} }` + : ""; + + return `export default (${replacementArguments}) => ${source};`; +}; diff --git a/src/html.d.ts b/src/html.d.ts new file mode 100644 index 0000000..b913216 --- /dev/null +++ b/src/html.d.ts @@ -0,0 +1,3 @@ +declare module "*.html" { + export default (replacements: any) => string; +} diff --git a/src/scripts/functionality/view-khatian/download-information-pdf-page-content.html b/src/scripts/functionality/view-khatian/download-information-pdf-page-content.html new file mode 100644 index 0000000..e91c15e --- /dev/null +++ b/src/scripts/functionality/view-khatian/download-information-pdf-page-content.html @@ -0,0 +1,273 @@ +
+ + +
$details$
+ + +
diff --git a/src/scripts/functionality/view-khatian/download-information-pdf-page-content.ts b/src/scripts/functionality/view-khatian/download-information-pdf-page-content.ts deleted file mode 100644 index 9ff18ec..0000000 --- a/src/scripts/functionality/view-khatian/download-information-pdf-page-content.ts +++ /dev/null @@ -1,219 +0,0 @@ -const sanghaFacilitationCentreBannerUrl = chrome.runtime.getURL("/assets/sangha-facilitation-centre-banner.jpg"); - -export default (isPlotInformation: boolean, details: string, { district, block, mouza }: { district: string, block: string, mouza: string }) => ` - - -
${details}
- - -`; diff --git a/src/scripts/functionality/view-khatian/index.ts b/src/scripts/functionality/view-khatian/index.ts index e29e8a4..38e500b 100644 --- a/src/scripts/functionality/view-khatian/index.ts +++ b/src/scripts/functionality/view-khatian/index.ts @@ -1,8 +1,10 @@ -import { downloadPDF } from "@/shared/download-pdf"; -import getDownloadInformationPDFPageContent from "@/scripts/functionality/view-khatian/download-information-pdf-page-content"; +import { generateWebPage } from "@/shared/generate-web-page"; +import getDownloadInformationPDFPageContent from "@/scripts/functionality/view-khatian/download-information-pdf-page-content.html"; declare function load(): void; +const sanghaFacilitationCentreBannerUrl = chrome.runtime.getURL("/assets/sangha-facilitation-centre-banner.jpg"); + document.addEventListener("DOMContentLoaded", () => { const proxiedPost = $.post; @@ -26,13 +28,14 @@ document.addEventListener("DOMContentLoaded", () => { let isPlotInformation: boolean | null = null; const downloadInformationPDF = () => { - let contentElement = document.getElementById(isPlotInformation ? "plotdetails" : "khdetails"); - const documentStringContent = getDownloadInformationPDFPageContent(!!isPlotInformation, contentElement!.innerHTML, { + generateWebPage(getDownloadInformationPDFPageContent({ + sanghaFacilitationCentreBannerUrl, + isPlotInformation: !!isPlotInformation, + details: document.getElementById(isPlotInformation ? "plotdetails" : "khdetails")!.innerHTML, district: getValueOfSelectElement("#lstDistrictCode1"), block: getValueOfSelectElement("#lstBlockCode1"), mouza: getValueOfSelectElement("#lstMouzaList") - }); - downloadPDF(`
${documentStringContent}
`); + })); }; $.post = function() { diff --git a/src/shared/download-pdf.ts b/src/shared/download-pdf.ts deleted file mode 100644 index bd8f815..0000000 --- a/src/shared/download-pdf.ts +++ /dev/null @@ -1,17 +0,0 @@ -export const downloadPDF = (content: string) => { - const tab = window.open("about:blank", "_blank"); - - if (tab) { - tab.document.write(content); - tab.document.close(); - tab.focus(); - Promise.all(Array.from(tab.document.images) - .filter(img => !img.complete) - .map(img => new Promise(resolve => { - img.onload = img.onerror = resolve; - }))).then(() => { - tab.print(); - tab.close(); - }); - } -}; diff --git a/src/shared/generate-web-page.ts b/src/shared/generate-web-page.ts new file mode 100644 index 0000000..d7de655 --- /dev/null +++ b/src/shared/generate-web-page.ts @@ -0,0 +1,9 @@ +export const generateWebPage = (content: string) => { + const tab = window.open("about:blank", "_blank"); + + if (tab) { + tab.document.write(content); + tab.document.close(); + tab.focus(); + } +}; -- cgit v1.2.3