Create PDF with images and send it as email attachment

68 views
Skip to first unread message

Arkadiusz Alicki

unread,
Mar 11, 2024, 11:51:09 AM3/11/24
to Google Apps Script Community
Hello,

I created a script which is triggered by Google Form submission. Script creates a PDF from template, and append photos uploaded by Google Form to created PDF. Then PDF is sent to desired user as an attachment via e-mail. 

The problem is that when the user adds high-resolution images, the PDF size exceeds 25Mb and it is impossible to send it via attachment. Below is a snippet of a function that adds photos to a document (I pass a Body object and an array of photo IDs as arguments)

Is it possible to compress images or change it's quality to be able to send it via email? 

Thanks for your help ! 


function appendPhotos(body, allPhotos){

  try{
    for(i in allPhotos){
          if(allPhotos[i] !== ""){
            var imageId = getIdFromUrl(allPhotos[i]);
            var imageToAppend = DriveApp.getFileById(imageId).getBlob();
            var appendedImage = body.appendImage(imageToAppend);
            var originalWidth = appendedImage.getWidth();
            var originalHeight = appendedImage.getHeight();
            var ratio = originalWidth/originalHeight;

          if(originalWidth>640){
            appendedImage.setWidth(640);
            var newWidth = appendedImage.getWidth();
            var newHeight = parseInt(newWidth/ratio);
            appendedImage.setHeight(newHeight);
          }
          }else { continue;}  
      }
  }
  catch(err){
    Logger.log("Error during appendPhotos");
    Logger.log(err);
  }
 
}

DME

unread,
Mar 14, 2024, 4:22:38 AM3/14/24
to google-apps-sc...@googlegroups.com

Yes, it's possible to resize and compress images before appending them to the PDF document. This can help reduce the overall size of the PDF and make it more suitable for email attachments. You can use the getBlob() method to get the image blob and then resize it using the setWidth() and setHeight() methods while maintaining the aspect ratio.

Here's an updated version of your function with image resizing and compression:

javascript
function appendPhotos(body, allPhotos) { try { for (var i = 0; i < allPhotos.length; i++) { if (allPhotos[i] !== "") { var imageId = getIdFromUrl(allPhotos[i]); var imageFile = DriveApp.getFileById(imageId); var imageBlob = imageFile.getBlob(); // Get image dimensions var image = UrlFetchApp.fetch(allPhotos[i]); var blob = image.getBlob(); var img = blob.getAs('image/jpeg'); var width = img.getWidth(); var height = img.getHeight(); // Resize image if necessary var MAX_WIDTH = 640; var MAX_HEIGHT = 480; var ratio = width / height; var newWidth = width; var newHeight = height; if (width > MAX_WIDTH || height > MAX_HEIGHT) { if (ratio > 1) { newWidth = MAX_WIDTH; newHeight = MAX_WIDTH / ratio; } else { newHeight = MAX_HEIGHT; newWidth = MAX_HEIGHT * ratio; } } // Resize image var resizedImage = imageBlob.getScaledBlob(newWidth, newHeight); // Append resized image to the body body.appendImage(resizedImage); } } } catch (err) {
Logger.log("Error during appendPhotos"); Logger.log(err); } }

This code resizes the image to fit within a maximum width and height (640x480 by default) while maintaining the aspect ratio. Additionally, the image is converted to JPEG format to further reduce its size. This should help in reducing the overall size of the PDF attachment. You may adjust the MAX_WIDTH and MAX_HEIGHT constants according to your requirements.


--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/f0424ffd-ceee-40e4-94c9-f60f31a9beadn%40googlegroups.com.


--
Reply all
Reply to author
Forward
0 new messages