diff --git a/lib/engines/puppeteer.js b/lib/engines/puppeteer.js index c74ae1b..496f355 100644 --- a/lib/engines/puppeteer.js +++ b/lib/engines/puppeteer.js @@ -41,7 +41,7 @@ class Puppeteer extends engine.Engine { async render(req, res, next) { const url = req.query.url || "https://www.google.com/"; - const format = req.query.format || "PNG"; + const format = (req.query.format || "PNG").toLowerCase(); const pageFormat = req.query.page_format || "A4"; const waitUntil = req.query.wait_until || "load"; const printBackground = req.query.print_background !== "0"; @@ -56,7 +56,7 @@ class Puppeteer extends engine.Engine { const scaleFactor = parseInt(req.query.scale_factor || 1); const timeout = parseInt(req.query.timeout || 500); const cookie = req.query.cookie || []; - const isPdf = ["pdf"].indexOf(format.toLowerCase()) !== -1; + const isPdf = ["pdf"].indexOf(format) !== -1; const cookieA = Array.isArray(cookie) ? cookie : [cookie]; // builds a new page instance (in a safe manner), taking @@ -141,14 +141,16 @@ class Puppeteer extends engine.Engine { res.send(data); } else { let data = await page.screenshot({ - fullPage: fullPage + fullPage: fullPage, + type: format }); if (trim) { const image = await jimp.read(data); + const mime = "image/" + format; image.autocrop(); - data = await image.getBufferAsync("image/png"); + data = await image.getBufferAsync(mime); } - res.type("png"); + res.type(format); res.send(data); } } finally { diff --git a/test/engines/puppeteer.js b/test/engines/puppeteer.js index 1ca7948..7a84463 100644 --- a/test/engines/puppeteer.js +++ b/test/engines/puppeteer.js @@ -42,6 +42,34 @@ describe("Puppeteer", function() { } }); + it("should render a jpeg", async () => { + const engine = new puppeteer.Puppeteer(); + await engine.init(); + + try { + const req = { + query: { + url: "https://example.com/", + format: "jpeg" + }, + body: {} + }; + const res = { + send: function(data) { + this.data = data; + }, + type: function(file) { + this.file = file; + } + }; + await engine.render(req, res); + assert.ok(res.data); + assert.strictEqual(res.file, "jpeg"); + } finally { + await engine.destroy(); + } + }); + it("should open new page", async () => { const engine = new puppeteer.Puppeteer(); await engine.init();