diff --git a/services/html-to-pdf/src/index.ts b/services/html-to-pdf/src/index.ts index 6eedc0e0..6f3dce35 100644 --- a/services/html-to-pdf/src/index.ts +++ b/services/html-to-pdf/src/index.ts @@ -18,7 +18,7 @@ async function bootstrap() { new ExpressAdapter(expressApp), ); - const binaryMimeTypes = [ + const contentTypes = [ 'application/pdf', 'application/javascript', 'application/json', @@ -51,7 +51,7 @@ async function bootstrap() { cachedServer = serverlessExpress({ app: expressApp, - binaryMimeTypes, + binarySettings: { contentTypes }, }); } diff --git a/services/html-to-pdf/src/module/pdf/pdf.service.ts b/services/html-to-pdf/src/module/pdf/pdf.service.ts index 9edb7e84..54b98d7e 100644 --- a/services/html-to-pdf/src/module/pdf/pdf.service.ts +++ b/services/html-to-pdf/src/module/pdf/pdf.service.ts @@ -1,11 +1,19 @@ /* eslint @typescript-eslint/no-var-requires: "off" */ import { Injectable } from '@nestjs/common'; import { Readable } from 'stream'; -import puppeteer from 'puppeteer-core'; +import puppeteer, { PDFOptions } from 'puppeteer-core'; import chromium from '@sparticuz/chromium-min'; @Injectable() export class PdfService { + pageSettings: PDFOptions = { + format: 'A4', + landscape: false, + printBackground: true, + margin: { top: '0px' }, + scale: 0.97, + }; + async htmlToPdf(html: string): Promise { const browser = await this.getBrowser(); const page = await browser.newPage(); @@ -14,7 +22,7 @@ export class PdfService { waitUntil: ['networkidle0', 'domcontentloaded'], }); - const buffer = await page.pdf({ format: 'a4', printBackground: true }); + const buffer = await page.pdf(this.pageSettings); await browser.close(); return buffer; @@ -25,13 +33,7 @@ export class PdfService { const page = await browser.newPage(); await page.goto(url, { waitUntil: ['networkidle2', 'domcontentloaded'] }); - const buffer = await page.pdf({ - format: 'A4', - landscape: false, - printBackground: true, - margin: { top: '30px' }, - scale: 0.98, - }); + const buffer = await page.pdf(this.pageSettings); await browser.close(); @@ -49,6 +51,7 @@ export class PdfService { }; await browser.close(); + return data; } diff --git a/services/html-to-pdf/stacks/download-lambda-layer.ts b/services/html-to-pdf/stacks/download-lambda-layer.ts index d230eee2..a9b96833 100644 --- a/services/html-to-pdf/stacks/download-lambda-layer.ts +++ b/services/html-to-pdf/stacks/download-lambda-layer.ts @@ -11,6 +11,11 @@ export const CHROMIUM_ZIP_FILEPATH = path.resolve( ); export async function downloadChromiumZip(): Promise { + if (fs.existsSync(CHROMIUM_ZIP_FILEPATH)) { + console.log(`Cached ${CHROMIUM_ZIP_URL}`); + return; + } + console.log(`Downloading ${CHROMIUM_ZIP_URL}`); await axios({ diff --git a/services/html-to-pdf/stacks/main-stack.ts b/services/html-to-pdf/stacks/main-stack.ts index c872a06a..8bcbb102 100644 --- a/services/html-to-pdf/stacks/main-stack.ts +++ b/services/html-to-pdf/stacks/main-stack.ts @@ -28,7 +28,7 @@ export class HtmlToPdfStack extends cdk.Stack { value: `${microservice.getBaseUrl()}/health`, }); - new cdk.CfnOutput(this, 'test endpoint', { + new cdk.CfnOutput(this, 'example test endpoint', { value: `${microservice.getBaseUrl()}/pdf?url=https://google.com`, }); }