'PDF-kit show images side by side with forEach loop

I have API that can add picture to pdf after sending request to database. I managed to make it work if I send one picture, but how can I show images side by side? Is it even possible? Because currently I have images stack one on second and so on...

Here is my code:

const photosToPdf = [];
for(let j=0;j<uploadsPath.length;j++){
  photosToPdf.push(uploadsFileNames[j]);
}

photosToPdf.forEach(v => {
  doc.image(`uploads/${v}`, 30, 350, {width: 100});
});

It will take empty array and fill it with Images name uploaded by user. Then It should loop through that array and use file name as img source. It is working, but It is looking like this, one on second.

I have idea, that I will use another loop to generate X and Y position and increase it every time, but maybe there is more simple solution.

Current status



Solution 1:[1]

Fixed this problem by incrementing position every iteration.

const photosToPdf = [];
let positionX = 30;
let positionY = 240;

for(let j=0;j<uploadsPath.length;j++){
   photosToPdf.push(uploadsFileNames[j]);
}

photosToPdf.forEach(img => {
   if(positionX == 570){
      positionX = 30
      positionY = positionY + 160
   }
   doc.image(`uploads/${img}`, positionX, positionY, {width: 80});
   positionX = positionX + 90
   });

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Marek