add prettier
This commit is contained in:
parent
c33e0653af
commit
f7178d212a
5 changed files with 339 additions and 267 deletions
|
|
@ -3,8 +3,8 @@ export const htmlTemplateRoute = express.Router()
|
|||
import { mimeToExtension, templateOption } from "./report-template"
|
||||
// import fs from "fs"
|
||||
//import { chromium } from 'playwright'
|
||||
import puppeteer, {PDFOptions} from 'puppeteer'
|
||||
import Handlebars from 'handlebars'
|
||||
import puppeteer, { PDFOptions } from "puppeteer"
|
||||
import Handlebars from "handlebars"
|
||||
|
||||
//import { createReport } from "docx-templates"
|
||||
// แก้ package.json ของ LibreOfficeFileConverter
|
||||
|
|
@ -13,12 +13,12 @@ import Handlebars from 'handlebars'
|
|||
const TEMPLATE_FOLDER_NAME = "templates/html"
|
||||
|
||||
function wait(ms: number) {
|
||||
return new Promise(resolve => setTimeout(resolve, ms));
|
||||
return new Promise((resolve) => setTimeout(resolve, ms))
|
||||
}
|
||||
|
||||
/**
|
||||
* docxTemplate Uses docx-template to convert input data and template to output buffer.
|
||||
* SPA and lazy load page may not fully render(eg. pantip.com).
|
||||
* SPA and lazy load page may not fully render(eg. pantip.com).
|
||||
* You have to handle exception throw by function
|
||||
* handlebars template support only content from Buffer
|
||||
* @param {Buffer|String} t template in buffer format or url to web page
|
||||
|
|
@ -26,117 +26,127 @@ function wait(ms: number) {
|
|||
* @param {String} outputMediaType output extension, support pdf, jpeg, png
|
||||
* @return {Promise<Uint8Array>} output buffer after apply template.
|
||||
*/
|
||||
export async function htmlTemplateX(t: Buffer | String, templOpt: templateOption, outputMediaType: string = "pdf"): Promise<Uint8Array> {
|
||||
try {
|
||||
if (!["pdf", "jpeg", "png"].find((e) => e === outputMediaType)) {
|
||||
throw "FormatError"
|
||||
}
|
||||
const browser = await puppeteer.launch({ headless: true, args: ['--no-sandbox'] });
|
||||
const page = await browser.newPage();
|
||||
if(templOpt.htmlOption?.navigationTimeout)
|
||||
page.setDefaultNavigationTimeout(120000);
|
||||
export async function htmlTemplateX(
|
||||
t: Buffer | String,
|
||||
templOpt: templateOption,
|
||||
outputMediaType: string = "pdf"
|
||||
): Promise<Uint8Array> {
|
||||
try {
|
||||
if (!["pdf", "jpeg", "png"].find((e) => e === outputMediaType)) {
|
||||
throw "FormatError"
|
||||
}
|
||||
const browser = await puppeteer.launch({
|
||||
headless: true,
|
||||
args: ["--no-sandbox"],
|
||||
})
|
||||
const page = await browser.newPage()
|
||||
if (templOpt.htmlOption?.navigationTimeout)
|
||||
page.setDefaultNavigationTimeout(120000)
|
||||
|
||||
if (typeof t === 'string') {
|
||||
if (typeof t === "string") {
|
||||
switch (templOpt.htmlOption?.waitUntil) {
|
||||
case "networkidle0":
|
||||
await page.goto(t, { waitUntil: "networkidle0" })
|
||||
break
|
||||
case "networkidle2":
|
||||
await page.goto(t, { waitUntil: "networkidle2" })
|
||||
break
|
||||
default:
|
||||
await page.goto(t)
|
||||
}
|
||||
} else {
|
||||
if (templOpt.data) {
|
||||
const template = Handlebars.compile(t.toString())
|
||||
const html = template(templOpt.data)
|
||||
await page.setContent(html)
|
||||
} else {
|
||||
await page.setContent(t.toString())
|
||||
}
|
||||
}
|
||||
const totalHeight = await page.evaluate(async (_templOpt) => {
|
||||
//force scroll for lazy load page
|
||||
const _scroll = _templOpt.htmlOption?.preloadScroll ?? 1000
|
||||
const _wait = _templOpt.htmlOption?.preloadWait ?? 400
|
||||
const _loop = _templOpt.htmlOption?.preloadLoop ?? 4
|
||||
|
||||
switch(templOpt.htmlOption?.waitUntil){
|
||||
case 'networkidle0': await page.goto(t, { waitUntil: 'networkidle0' })
|
||||
break
|
||||
case 'networkidle2': await page.goto(t, { waitUntil: 'networkidle2' })
|
||||
break
|
||||
default: await page.goto(t)
|
||||
}
|
||||
} else {
|
||||
if (templOpt.data) {
|
||||
const template = Handlebars.compile(t.toString());
|
||||
const html = template(templOpt.data);
|
||||
await page.setContent(html);
|
||||
} else {
|
||||
await page.setContent(t.toString())
|
||||
}
|
||||
}
|
||||
const totalHeight = await page.evaluate(async (_templOpt) => {
|
||||
//force scroll for lazy load page
|
||||
const _scroll = _templOpt.htmlOption?.preloadScroll??1000
|
||||
const _wait = _templOpt.htmlOption?.preloadWait??400
|
||||
const _loop =_templOpt.htmlOption?.preloadLoop??4
|
||||
for (let i = 0; i < _loop; i++) {
|
||||
window.scrollBy(0, _scroll)
|
||||
await new Promise((resolve) => setTimeout(resolve, _wait))
|
||||
}
|
||||
//extra wait
|
||||
await new Promise((resolve) => setTimeout(resolve, _wait * 2))
|
||||
|
||||
for (let i = 0; i < _loop; i++) {
|
||||
window.scrollBy(0, _scroll);
|
||||
await new Promise(resolve => setTimeout(resolve, _wait));
|
||||
}
|
||||
//extra wait
|
||||
await new Promise(resolve => setTimeout(resolve, _wait*2));
|
||||
let scrollableSection =
|
||||
_templOpt.htmlOption?.querySelector &&
|
||||
document.querySelector(_templOpt.htmlOption.querySelector)
|
||||
? document.querySelector(_templOpt.htmlOption.querySelector)
|
||||
: null
|
||||
|
||||
let scrollableSection =
|
||||
(_templOpt.htmlOption?.querySelector &&
|
||||
document.querySelector(_templOpt.htmlOption.querySelector)) ?
|
||||
document.querySelector(_templOpt.htmlOption.querySelector) : null
|
||||
const childElement = scrollableSection ? scrollableSection : document.body
|
||||
|
||||
const childElement = scrollableSection ? scrollableSection : document.body
|
||||
|
||||
if (scrollableSection == null)
|
||||
scrollableSection = document.body
|
||||
//const childElement = scrollableSection.firstElementChild;
|
||||
let scrollPosition = 0;
|
||||
let viewportHeight = window.innerHeight;
|
||||
while (scrollPosition < childElement.scrollHeight) {
|
||||
scrollableSection.scrollBy(0, viewportHeight);
|
||||
await new Promise(resolve => setTimeout(resolve, 500));
|
||||
scrollPosition += viewportHeight;
|
||||
}
|
||||
//return scrollPosition
|
||||
return childElement.scrollHeight
|
||||
}, templOpt);
|
||||
if (scrollableSection == null) scrollableSection = document.body
|
||||
//const childElement = scrollableSection.firstElementChild;
|
||||
let scrollPosition = 0
|
||||
let viewportHeight = window.innerHeight
|
||||
while (scrollPosition < childElement.scrollHeight) {
|
||||
scrollableSection.scrollBy(0, viewportHeight)
|
||||
await new Promise((resolve) => setTimeout(resolve, 500))
|
||||
scrollPosition += viewportHeight
|
||||
}
|
||||
//return scrollPosition
|
||||
return childElement.scrollHeight
|
||||
}, templOpt)
|
||||
|
||||
console.log("totalHeight")
|
||||
if (!totalHeight) {
|
||||
throw new Error(`Unable to determine the page height ${totalHeight}. The selector may not correct or no body tag`);
|
||||
} else {
|
||||
console.log("Page height adjusted to:", totalHeight);
|
||||
}
|
||||
console.log("totalHeight")
|
||||
if (!totalHeight) {
|
||||
throw new Error(
|
||||
`Unable to determine the page height ${totalHeight}. The selector may not correct or no body tag`
|
||||
)
|
||||
} else {
|
||||
console.log("Page height adjusted to:", totalHeight)
|
||||
}
|
||||
|
||||
await page.setViewport({
|
||||
width: Number(templOpt.htmlOption?.pdfOption?.width??1200),
|
||||
height: totalHeight,
|
||||
deviceScaleFactor: 2,
|
||||
isMobile: false
|
||||
});
|
||||
await page.setViewport({
|
||||
width: Number(templOpt.htmlOption?.pdfOption?.width ?? 1200),
|
||||
height: totalHeight,
|
||||
deviceScaleFactor: 2,
|
||||
isMobile: false,
|
||||
})
|
||||
|
||||
|
||||
///// output to photo end here
|
||||
if (outputMediaType === "png" || outputMediaType === "jpeg") {
|
||||
const photoBuffer = await page.screenshot({
|
||||
// path: 'url_pup.png',
|
||||
fullPage: true,
|
||||
type: outputMediaType // 'webp'
|
||||
})
|
||||
await browser.close();
|
||||
return photoBuffer
|
||||
}
|
||||
///// output to PDF
|
||||
//TODO overide option from htmlTemplateOption
|
||||
let o:PDFOptions ={
|
||||
// path: './url_prop.pdf',
|
||||
// format:"A4",
|
||||
width: 1200,
|
||||
height: totalHeight,
|
||||
printBackground: true,
|
||||
scale: 1,
|
||||
displayHeaderFooter: false,
|
||||
margin: { top: 5, right: 5, bottom: 5, left: 5 }
|
||||
}
|
||||
//Murge with default
|
||||
if(templOpt.htmlOption?.pdfOption)
|
||||
o = {...o,...templOpt.htmlOption.pdfOption}
|
||||
const {path,...pdfOption} = o //remove path if exists
|
||||
console.log("pdfOption:",pdfOption)
|
||||
const buffer = await page.pdf(pdfOption);
|
||||
await browser.close();
|
||||
return buffer
|
||||
} catch (e) {
|
||||
//console.log(e)
|
||||
throw e
|
||||
}
|
||||
///// output to photo end here
|
||||
if (outputMediaType === "png" || outputMediaType === "jpeg") {
|
||||
const photoBuffer = await page.screenshot({
|
||||
// path: 'url_pup.png',
|
||||
fullPage: true,
|
||||
type: outputMediaType, // 'webp'
|
||||
})
|
||||
await browser.close()
|
||||
return photoBuffer
|
||||
}
|
||||
///// output to PDF
|
||||
//TODO overide option from htmlTemplateOption
|
||||
let o: PDFOptions = {
|
||||
// path: './url_prop.pdf',
|
||||
// format:"A4",
|
||||
width: 1200,
|
||||
height: totalHeight,
|
||||
printBackground: true,
|
||||
scale: 1,
|
||||
displayHeaderFooter: false,
|
||||
margin: { top: 5, right: 5, bottom: 5, left: 5 },
|
||||
}
|
||||
//Murge with default
|
||||
if (templOpt.htmlOption?.pdfOption)
|
||||
o = { ...o, ...templOpt.htmlOption.pdfOption }
|
||||
const { path, ...pdfOption } = o //remove path if exists
|
||||
console.log("pdfOption:", pdfOption)
|
||||
const buffer = await page.pdf(pdfOption)
|
||||
await browser.close()
|
||||
return buffer
|
||||
} catch (e) {
|
||||
//console.log(e)
|
||||
throw e
|
||||
}
|
||||
}
|
||||
|
||||
/** javascript-obfuscator:disable
|
||||
|
|
@ -178,28 +188,36 @@ export async function htmlTemplateX(t: Buffer | String, templOpt: templateOption
|
|||
*
|
||||
*/
|
||||
htmlTemplateRoute.post("/", async function (req, res) {
|
||||
try {
|
||||
if (!req.headers["content-type"] || !req.headers["accept"]) throw new Error("Require header content-type, accept")
|
||||
let inputType = mimeToExtension(req.headers["content-type"])// application/json
|
||||
let outputMediaType = mimeToExtension(req.headers["accept"])
|
||||
let buffer = await htmlTemplateX(req.body.template, req.body, outputMediaType)
|
||||
res.statusCode = 201
|
||||
res.setHeader("Content-Type", req.headers["accept"])
|
||||
res.setHeader("Content-Disposition", `attachment;filename=${req.body.reportName}.${outputMediaType}`)
|
||||
res.setHeader("Content-Length", buffer.length)
|
||||
res.end(buffer)
|
||||
} catch (ex) {
|
||||
if (ex instanceof SyntaxError) {
|
||||
res.statusCode = 400
|
||||
res.statusMessage = ex.message
|
||||
res.end(res.statusMessage)
|
||||
console.error("report-template/html: ", ex)
|
||||
} else {
|
||||
res.statusCode = 500
|
||||
res.statusMessage = "Internal Server Error during POST report-template/html"
|
||||
res.end(res.statusMessage)
|
||||
console.error("report-template/html: ", ex)
|
||||
}
|
||||
}
|
||||
try {
|
||||
if (!req.headers["content-type"] || !req.headers["accept"])
|
||||
throw new Error("Require header content-type, accept")
|
||||
let inputType = mimeToExtension(req.headers["content-type"]) // application/json
|
||||
let outputMediaType = mimeToExtension(req.headers["accept"])
|
||||
let buffer = await htmlTemplateX(
|
||||
req.body.template,
|
||||
req.body,
|
||||
outputMediaType
|
||||
)
|
||||
res.statusCode = 201
|
||||
res.setHeader("Content-Type", req.headers["accept"])
|
||||
res.setHeader(
|
||||
"Content-Disposition",
|
||||
`attachment;filename=${req.body.reportName}.${outputMediaType}`
|
||||
)
|
||||
res.setHeader("Content-Length", buffer.length)
|
||||
res.end(buffer)
|
||||
} catch (ex) {
|
||||
if (ex instanceof SyntaxError) {
|
||||
res.statusCode = 400
|
||||
res.statusMessage = ex.message
|
||||
res.end(res.statusMessage)
|
||||
console.error("report-template/html: ", ex)
|
||||
} else {
|
||||
res.statusCode = 500
|
||||
res.statusMessage =
|
||||
"Internal Server Error during POST report-template/html"
|
||||
res.end(res.statusMessage)
|
||||
console.error("report-template/html: ", ex)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue