I have a Form where a user can put in pictures. These pictures are added to a temp document using google drive. This temp document is then transferred to a PDF file. After that the PDF is sent in an e-mail.
Sometimes this attachment is too big for the email. Is there a way to compress the PDF file ( blob) in size? Or compress the image file sizes (the image is also a blob)?
image:
var blob = DriveApp.getFileById(splitText[0]).getBlob();
body.appendImage(blob).setWidth(sizes).setHeight(sizes);
const blobPDF = newTempFile.getAs(MimeType.PDF);
const pdfFile = pdfFolder.createFile(blobPDF).setName( info[2] + "---" + "Opmeting");
From
Image: just adds an image from a google drive url. The image is added on a new line in a temp document. the PDF makes from that temp doc a blob. Then a PDF en renames it., unfortunately, I cannot correctly imagine your whole script. But, if your showing script is included in your actual script, please test the following modification.From:
To:
From
var blob = DriveApp.getFileById(splitText[0]).getBlob();, it supposes thatsplitText[0]is the file ID of your image file.In this case, it supposes that the variable
sizeshas already been declared elsewhere.Or, how about the following modification?
splitText[0]is changed and put into the Document withappendImage(blob). In this modification, the size of the image for putting into the Document is changed (reduced). I guessed that this might lead to a reduction in the size of the exported PDF file.Note:
Sample case:
As a test case, I tested the following situation.
The sample script for testing is as follows.
In this case, the exported PDF file size was 6,413,647 bytes.
In this case, the exported PDF file size was 706,321 bytes.
It is found that when the original image size is reduced, the exported PDF file size is also reduced. When
getAs(MimeType.JPEG)is modified togetBlob(), the exported PDF file size is 725,079 bytes. If the original image format is JPEG format, the exported PDF file size was the same betweengetAs(MimeType.JPEG)andgetBlob(). Because the mimeType of the image is not changed from JPEG.I think that about
sample2, whensizeis large, the exported PDF file size is also large.