-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1f6bf24
commit 7f67828
Showing
20 changed files
with
191 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"unknown": { | ||
"versionSfCore": null, | ||
"versionFramework": "4.2.4", | ||
"isWithinCompose": false, | ||
"composeOrgName": null, | ||
"composeServiceName": null, | ||
"command": [], | ||
"options": {}, | ||
"error": null, | ||
"machineId": "4fa7584a6810489c980dfeebe3fa579c", | ||
"serviceProviderAwsCfStackId": null, | ||
"serviceProviderAwsCfStackCreated": null, | ||
"serviceProviderAwsCfStackUpdated": null, | ||
"serviceProviderAwsCfStackStatus": null, | ||
"serviceProviderAwsCfStackOutputs": null | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
const puppeteer = require("puppeteer-core"); | ||
const chromium = require("@sparticuz/chromium"); | ||
const { S3Client, PutObjectCommand, DeleteObjectCommand } = require("@aws-sdk/client-s3"); | ||
|
||
const s3 = new S3Client({ region: 'eu-west-1' }); | ||
const S3_BUCKET = 'vic-staging-private-enid'; | ||
|
||
chromium.setHeadlessMode = true; | ||
|
||
exports.generatePDF = async (event) => { | ||
console.log('Received headers:', event.headers); | ||
|
||
const { | ||
'x-document-contract-id': documentContractId, | ||
'x-organization-id': organizationId, | ||
'x-existing-file-path': existingContractFilePath | ||
} = event.headers; | ||
|
||
if (!documentContractId || !organizationId) { | ||
return { | ||
statusCode: 400, | ||
body: JSON.stringify({ message: 'Missing required headers' }), | ||
}; | ||
} | ||
|
||
try { | ||
const browser = await puppeteer.launch({ | ||
executablePath: await chromium.executablePath(), | ||
headless: chromium.headless, | ||
ignoreHTTPSErrors: true, | ||
args: [...chromium.args, "--hide-scrollbars", "--disable-web-security", '--no-sandbox', '--disable-setuid-sandbox'], | ||
}); | ||
|
||
const page = await browser.newPage(); | ||
await page.setContent(event.body); | ||
const buffer = await page.pdf({ format: 'A4' }); | ||
await browser.close(); | ||
|
||
const fileName = `documents/contracts/${organizationId}/${documentContractId}-${Date.now()}.pdf`; | ||
const s3Params = { | ||
Bucket: S3_BUCKET, | ||
Key: fileName, | ||
Body: buffer, | ||
ContentType: 'application/pdf', | ||
}; | ||
|
||
await s3.send(new PutObjectCommand(s3Params)); | ||
|
||
if (existingContractFilePath) { | ||
try { | ||
await s3.send(new DeleteObjectCommand({ | ||
Bucket: S3_BUCKET, | ||
Key: existingContractFilePath, | ||
})); | ||
} catch (error) { | ||
console.error('Error deleting existing PDF from S3:', error); | ||
// TODO: Log this error to Sentry | ||
} | ||
} | ||
|
||
console.log(`PDF uploaded successfully: https://${s3Params.Bucket}.s3.amazonaws.com/${s3Params.Key}`); | ||
|
||
return { | ||
statusCode: 200, | ||
body: JSON.stringify({ | ||
message: 'PDF successfully uploaded to S3', | ||
url: s3Params.Key, | ||
}), | ||
}; | ||
} catch (error) { | ||
console.error('Error in PDF generation process:', error); | ||
return { | ||
statusCode: 500, | ||
body: JSON.stringify({ message: 'Internal server error during PDF generation' }), | ||
}; | ||
} | ||
}; |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
|
||
app: pdf-generator | ||
# "service" is the name of this project. This will also be added to your AWS resource names. | ||
service: pdf-generator-service | ||
|
||
# package: | ||
# individually: true | ||
# patterns: | ||
# - '!node_modules/@sparticuz/chromium/bin/**' | ||
|
||
provider: | ||
name: aws | ||
runtime: nodejs20.x | ||
region: eu-west-1 | ||
timeout: 30 | ||
|
||
functions: | ||
generatePDF: | ||
handler: handler.generatePDF | ||
events: | ||
- httpApi: 'POST /generate-pdf' | ||
memorySize: 512 | ||
description: 'Generate a PDF from an HTML template' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.