- implement cloud functions for printing

- implement AMOLED mode
- implement reset layout
This commit is contained in:
Amruth Pillai
2020-07-12 15:04:07 +05:30
parent f468ca73c3
commit 5ccc360345
19 changed files with 4004 additions and 20 deletions

123
functions/.eslintrc.json Normal file
View File

@ -0,0 +1,123 @@
{
"parserOptions": {
// Required for certain syntax usages
"ecmaVersion": 2017
},
"plugins": [
"promise"
],
"extends": "eslint:recommended",
"rules": {
// Removed rule "disallow the use of console" from recommended eslint rules
"no-console": "off",
// Removed rule "disallow multiple spaces in regular expressions" from recommended eslint rules
"no-regex-spaces": "off",
// Removed rule "disallow the use of debugger" from recommended eslint rules
"no-debugger": "off",
// Removed rule "disallow unused variables" from recommended eslint rules
"no-unused-vars": "off",
// Removed rule "disallow mixed spaces and tabs for indentation" from recommended eslint rules
"no-mixed-spaces-and-tabs": "off",
// Removed rule "disallow the use of undeclared variables unless mentioned in /*global */ comments" from recommended eslint rules
"no-undef": "off",
// Warn against template literal placeholder syntax in regular strings
"no-template-curly-in-string": 1,
// Warn if return statements do not either always or never specify values
"consistent-return": 1,
// Warn if no return statements in callbacks of array methods
"array-callback-return": 1,
// Require the use of === and !==
"eqeqeq": 2,
// Disallow the use of alert, confirm, and prompt
"no-alert": 2,
// Disallow the use of arguments.caller or arguments.callee
"no-caller": 2,
// Disallow null comparisons without type-checking operators
"no-eq-null": 2,
// Disallow the use of eval()
"no-eval": 2,
// Warn against extending native types
"no-extend-native": 1,
// Warn against unnecessary calls to .bind()
"no-extra-bind": 1,
// Warn against unnecessary labels
"no-extra-label": 1,
// Disallow leading or trailing decimal points in numeric literals
"no-floating-decimal": 2,
// Warn against shorthand type conversions
"no-implicit-coercion": 1,
// Warn against function declarations and expressions inside loop statements
"no-loop-func": 1,
// Disallow new operators with the Function object
"no-new-func": 2,
// Warn against new operators with the String, Number, and Boolean objects
"no-new-wrappers": 1,
// Disallow throwing literals as exceptions
"no-throw-literal": 2,
// Require using Error objects as Promise rejection reasons
"prefer-promise-reject-errors": 2,
// Enforce “for” loop update clause moving the counter in the right direction
"for-direction": 2,
// Enforce return statements in getters
"getter-return": 2,
// Disallow await inside of loops
"no-await-in-loop": 2,
// Disallow comparing against -0
"no-compare-neg-zero": 2,
// Warn against catch clause parameters from shadowing variables in the outer scope
"no-catch-shadow": 1,
// Disallow identifiers from shadowing restricted names
"no-shadow-restricted-names": 2,
// Enforce return statements in callbacks of array methods
"callback-return": 2,
// Require error handling in callbacks
"handle-callback-err": 2,
// Warn against string concatenation with __dirname and __filename
"no-path-concat": 1,
// Prefer using arrow functions for callbacks
"prefer-arrow-callback": 1,
// Return inside each then() to create readable and reusable Promise chains.
// Forces developers to return console logs and http calls in promises.
"promise/always-return": 2,
//Enforces the use of catch() on un-returned promises
"promise/catch-or-return": 2,
// Warn against nested then() or catch() statements
"promise/no-nesting": 1
}
}

1
functions/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
node_modules/

95
functions/index.js Normal file
View File

@ -0,0 +1,95 @@
const functions = require("firebase-functions");
const puppeteer = require("puppeteer");
require('dotenv').config();
const cors = require("cors")({
origin: true,
});
const BASE_URL = process.env.SITE_URL + "/r/";
function timeout(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
exports.printSinglePageResume = functions.https.onRequest((req, res) => {
if (req.method !== "POST") {
return res.status(403).send("Forbidden!");
}
if (!req.query.id) {
return res.status(400).send("Bad Request!");
}
async function run() {
const browser = await puppeteer.launch({ headless: true });
const page = await browser.newPage();
await page.goto(BASE_URL + req.query.id);
await timeout(5000);
await page.emulateMediaType("print");
const height = await page.evaluate(() => {
var body = document.body,
html = document.documentElement;
var height = Math.max(
body.scrollHeight,
body.offsetHeight,
html.clientHeight,
html.scrollHeight,
html.offsetHeight
);
return height;
});
const pdf = await page.pdf({
printBackground: true,
width: `21cm`,
height: `${height}px`,
pageRanges: "1",
});
await browser.close();
return pdf;
}
return cors(req, res, async () => {
const pdf = await run();
res.set({
"Content-Type": "application/pdf",
"Content-Length": pdf.length,
});
return res.send(pdf);
});
});
exports.printMultiPageResume = functions.https.onRequest((req, res) => {
if (req.method !== "POST") {
return res.status(403).send("Forbidden!");
}
if (!req.query.id) {
return res.status(400).send("Bad Request!");
}
async function run() {
const browser = await puppeteer.launch({ headless: true });
const page = await browser.newPage();
await page.goto(BASE_URL + req.query.id);
await timeout(5000);
await page.emulateMediaType("print");
const pdf = await page.pdf({
printBackground: true,
width: `21cm`,
});
await browser.close();
return pdf;
}
return cors(req, res, async () => {
const pdf = await run();
res.set({
"Content-Type": "application/pdf",
"Content-Length": pdf.length,
});
return res.send(pdf);
});
});

3157
functions/package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

28
functions/package.json Normal file
View File

@ -0,0 +1,28 @@
{
"name": "functions",
"description": "Cloud Functions for Firebase",
"scripts": {
"lint": "eslint .",
"serve": "firebase emulators:start --only functions",
"shell": "firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log"
},
"engines": {
"node": "10"
},
"dependencies": {
"cors": "^2.8.5",
"dotenv": "^8.2.0",
"firebase-admin": "^8.10.0",
"firebase-functions": "^3.7.0",
"puppeteer": "^5.0.0"
},
"devDependencies": {
"eslint": "^5.12.0",
"eslint-plugin-promise": "^4.0.1",
"firebase-functions-test": "^0.2.0"
},
"private": true
}