Skip to content

Commit

Permalink
feat: html to pdf margin and content type improvement
Browse files Browse the repository at this point in the history
Signed-off-by: hxtree <[email protected]>
  • Loading branch information
hxtree committed Oct 27, 2023
1 parent 14a1f9f commit da41c61
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 12 deletions.
4 changes: 2 additions & 2 deletions services/html-to-pdf/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ async function bootstrap() {
new ExpressAdapter(expressApp),
);

const binaryMimeTypes = [
const contentTypes = [
'application/pdf',
'application/javascript',
'application/json',
Expand Down Expand Up @@ -51,7 +51,7 @@ async function bootstrap() {

cachedServer = serverlessExpress({
app: expressApp,
binaryMimeTypes,
binarySettings: { contentTypes },
});
}

Expand Down
21 changes: 12 additions & 9 deletions services/html-to-pdf/src/module/pdf/pdf.service.ts
Original file line number Diff line number Diff line change
@@ -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<any> {
const browser = await this.getBrowser();
const page = await browser.newPage();
Expand All @@ -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;
Expand All @@ -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();

Expand All @@ -49,6 +51,7 @@ export class PdfService {
};

await browser.close();

return data;
}

Expand Down
5 changes: 5 additions & 0 deletions services/html-to-pdf/stacks/download-lambda-layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ export const CHROMIUM_ZIP_FILEPATH = path.resolve(
);

export async function downloadChromiumZip(): Promise<any> {
if (fs.existsSync(CHROMIUM_ZIP_FILEPATH)) {
console.log(`Cached ${CHROMIUM_ZIP_URL}`);
return;
}

console.log(`Downloading ${CHROMIUM_ZIP_URL}`);

await axios({
Expand Down
2 changes: 1 addition & 1 deletion services/html-to-pdf/stacks/main-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
});
}
Expand Down

0 comments on commit da41c61

Please sign in to comment.