Skip to content

Commit

Permalink
feat: added image endpoint (#1)
Browse files Browse the repository at this point in the history
Co-authored-by: CodiumAI-Agent <137281646+CodiumAI-Agent@users.noreply.github.com>
  • Loading branch information
RahulLanjewar93 and CodiumAI-Agent authored Sep 28, 2023
1 parent 077666d commit 681af5d
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 15 deletions.
24 changes: 11 additions & 13 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import compression from 'compression';
import express from 'express';
import morgan from 'morgan';
import { getBrowser } from './browser.js';
import render from './render.js';
import { renderPdf, renderImage } from './render.js';

const app = express();

Expand Down Expand Up @@ -41,23 +41,21 @@ app.get('/.live', async (req, res, next) => {
}
});

// render page
app.post('/', async (req, res, next) => {
// render pdf
const handleRender = (renderFunc, contentType) => async (req, res, next) => {
try {
// get inputs from the json payload
const { html = '', options = null } = req.body;

// render the PDF
const pdf = await render(html, options);

// reply with express
res.set('content-type', 'application/pdf');
res.send(pdf);
const result = await renderFunc(html, options);
res.set('content-type', contentType);
res.send(result);
} catch (error) {
// continue with the error
next(error);
}
});
};

app.post('/image', handleRender(renderImage, 'image/jpeg'));
app.post('/pdf', handleRender(renderPdf, 'application/pdf'));
app.post('/', handleRender(renderPdf, 'application/pdf'));

// error handler
app.use((error, req, res, next) => {
Expand Down
45 changes: 43 additions & 2 deletions src/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,18 @@ export type RenderOptions = {
viewport: puppeteer.Viewport;
waitUntil: puppeteer.WaitForOptions['waitUntil'];
pdf: puppeteer.PDFOptions;
image: puppeteer.ScreenshotOptions;
};

const defaultOptions: RenderOptions = {
emulateScreenMedia: true,
viewport: { width: 1600, height: 1200 },
waitUntil: 'networkidle2',
pdf: { format: 'a4', printBackground: true },
image: {},
};

const render = async (html: string, customOptions?: Partial<RenderOptions> | null) => {
export const renderPdf = async (html: string, customOptions?: Partial<RenderOptions> | null) => {
const options = merge(defaultOptions, customOptions);

// start browser
Expand Down Expand Up @@ -57,4 +59,43 @@ const render = async (html: string, customOptions?: Partial<RenderOptions> | nul
return pdf;
};

export default render;
export const renderImage = async (html: string, customOptions?: Partial<RenderOptions> | null) => {
const options = merge(defaultOptions, customOptions);

// start browser
const browser = await getBrowser();
// start page
const page = await browser.newPage();

// print on console
page.on('console', (...messages) => console.info('Console logs:', ...messages));

// print on error
page.on('error', err => {
console.error(`Error event emitted: ${err}`);
console.error(err.stack);
browser.close();
});

let image: Buffer;

try {
// set view port
await page.setViewport(options.viewport);

if (options.emulateScreenMedia) {
await page.emulateMediaType('screen');
}

// set html content
await page.setContent(html, { waitUntil: options.waitUntil });

// render to image
image = (await page.screenshot(options.image)) as Buffer;
} finally {
// close the page
await page.close();
}

return image;
};

0 comments on commit 681af5d

Please sign in to comment.