Skip to content

Commit b906242

Browse files
committed
feat: fix linkedin pdf
1 parent 7a82798 commit b906242

File tree

1 file changed

+35
-8
lines changed

1 file changed

+35
-8
lines changed

libraries/nestjs-libraries/src/integrations/social/linkedin.provider.ts

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,16 @@ export class LinkedinProvider extends SocialAbstract implements SocialProvider {
4040
maxLength() {
4141
return 3000;
4242
}
43+
44+
public override handleErrors(body: string):
45+
| {
46+
type: 'refresh-token' | 'bad-body' | 'retry';
47+
value: string;
48+
}
49+
| undefined {
50+
51+
return undefined;
52+
}
4353
async refreshToken(refresh_token: string): Promise<AuthTokenDetails> {
4454
const {
4555
access_token: accessToken,
@@ -380,17 +390,34 @@ export class LinkedinProvider extends SocialAbstract implements SocialProvider {
380390
})
381391
);
382392

383-
// Use the dimensions of the first image for the PDF page size
384-
// You could also use the largest dimensions if you prefer
385-
const firstImageDimensions = imageData[0];
386-
const pageSize = [firstImageDimensions.width, firstImageDimensions.height];
393+
// Find the maximum dimensions across all images to use as page size
394+
const maxWidth = Math.max(...imageData.map((data) => data.width));
395+
const maxHeight = Math.max(...imageData.map((data) => data.height));
396+
const pageSize = [maxWidth, maxHeight];
397+
398+
// Resize all images to fit within the page size while maintaining aspect ratio
399+
// and centering them on a white background
400+
const resizedImageBuffers = await Promise.all(
401+
imageData.map(async (data) => {
402+
// If image already matches page size, return as-is but convert to JPEG for consistency
403+
if (data.width === maxWidth && data.height === maxHeight) {
404+
return await sharp(data.buffer).jpeg({ quality: 95 }).toBuffer();
405+
}
387406

388-
// Convert images to PDF with exact image dimensions
389-
const pdfStream = imageToPDF(
390-
imageData.map((data) => data.buffer),
391-
pageSize
407+
// Resize image to fit within page dimensions, then extend with white background to fill page
408+
return await sharp(data.buffer)
409+
.resize(maxWidth, maxHeight, {
410+
fit: 'contain',
411+
background: { r: 255, g: 255, b: 255, alpha: 1 },
412+
})
413+
.jpeg({ quality: 95 })
414+
.toBuffer();
415+
})
392416
);
393417

418+
// Convert images to PDF with consistent page dimensions
419+
const pdfStream = imageToPDF(resizedImageBuffers, pageSize);
420+
394421
// Convert stream to buffer
395422
const pdfBuffer = await this.streamToBuffer(pdfStream);
396423

0 commit comments

Comments
 (0)