Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
migingreen committed Dec 21, 2022
1 parent e9c8c07 commit ee26964
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 33 deletions.
27 changes: 0 additions & 27 deletions plugins/print/rendering-info.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ module.exports = {
tags: ["api"],
},
handler: async function (request, h) {
//console.log('DISPLAYOPTIONS', request.payload.toolRuntimeConfig.displayOptions);

try {
const displayOptions = request.payload.toolRuntimeConfig.displayOptions;
const screenshotRequestQuery = {};
Expand Down Expand Up @@ -80,29 +78,20 @@ module.exports = {
}
);

//console.log('SCREENSHOT_REQUEST_TOOLRUNTIMECONFIG', screenshotRequestToolRuntimeConfig);

const screenshotRequestPayload = {
toolRuntimeConfig: screenshotRequestToolRuntimeConfig,
item: request.payload.item,
};

//console.log('SCREENSHOT_REQUEST_PAYLOAD', screenshotRequestPayload);

const dpi = request.payload.toolRuntimeConfig.dpi || 300;
screenshotRequestQuery.dpr = dpi / chromePPI;

//console.log('dpi', dpi);
//console.log('chromePPI', chromePPI);

// the screenshot width is the width in inch * target dpi
const mm = await request.server.methods.plugins.q.print.colsToMm(
displayOptions.columnsProfile,
displayOptions.columns
);

//console.log('mm', mm);

screenshotRequestQuery.width = Math.round(
(mmToInch(mm) * dpi) / screenshotRequestQuery.dpr
);
Expand All @@ -113,16 +102,12 @@ module.exports = {
screenshotRequestQuery.target = await request.server.settings.app.print
.target;

//console.log('SCREENSHOT_REQUEST_QUERY', screenshotRequestQuery);

const screenshotImageResponse = await request.server.inject({
method: "POST",
url: `/screenshot.png?${querystring.stringify(screenshotRequestQuery)}`,
payload: screenshotRequestPayload,
});

//console.log('SCREENSHOT_IMAGE_RESPONSE', screenshotImageResponse);

// fail early if there is an error to generate the screenshot
if (screenshotImageResponse.statusCode !== 200) {
request.server.log(["error"], screenshotImageResponse.payload);
Expand Down Expand Up @@ -161,37 +146,25 @@ module.exports = {
.update(request.info.id)
.digest("hex");

//console.log('requestId', requestId);

// the following could all be optimised maybe by implementing it using streams and buffers
// instead of writing and reading files
// but we do it easy for now...
const fileNameBase = `${__dirname}/${requestId}`;

//console.log('fileNameBase', fileNameBase);

// write the tiff buffer to disk
await fs.writeFile(`${fileNameBase}orig.tiff`, tiffBuffer);
// remove the alpha channel for tiff2pdf to work
const { stdoutA, stderrA } = await exec(
`convert ${fileNameBase}orig.tiff -alpha off -compress lzw ${fileNameBase}-no-alpha.tiff`
);

// console.log('stdoutA', stdoutA);
// console.log('stderrA', stderrA);

// we need to use tiff2pdf instead of imagemagick since this produces pdf v1.3 compatible PDFs where imagemagick does not
const { stdoutP, stderrP } = await exec(
`tiff2pdf -z -o ${fileNameBase}.pdf ${fileNameBase}-no-alpha.tiff`
);

// console.log('stdoutP', stdoutP);
// console.log('stderrP', stderrP);

const pdfBuffer = await fs.readFile(`${fileNameBase}.pdf`);

// console.log('pdfBuffer', pdfBuffer);

resolve(h.response(pdfBuffer).type("application/pdf"));

// remove all the intermediate files
Expand Down
7 changes: 1 addition & 6 deletions plugins/screenshot/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,9 @@ async function startPcrChromiumProcess() {
args: [
"--single-process", // Reduce chances for timeouts
"--no-sandbox",
//"--disable-setuid-sandbox",
"--disable-dev-shm-usage",
"--font-render-hinting=none",
],
/* env: {
DISPLAY: ":10.0",
}, */
executablePath: stats.executablePath,
})
.catch(function (error) {
Expand Down Expand Up @@ -143,14 +139,13 @@ async function getFinishedPage(
</body>
</html>`;

// Potential fix for timeouts: https://github.com/puppeteer/puppeteer/issues/1552#issuecomment-350954419
await page.setContent(content, {
waitUntil: ["domcontentloaded"],
});

const scriptContent = await getConcatenatedAssets(scripts, userAgent);

if(scriptContent) {
if (scriptContent) {
await page.mainFrame().addScriptTag({
content: scriptContent,
});
Expand Down

0 comments on commit ee26964

Please sign in to comment.