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.
var blob = DriveApp.getFileById(splitText[0]).getBlob();
body.appendImage(blob).setWidth(sizes).setHeight(sizes);
From var blob = DriveApp.getFileById(splitText[0]).getBlob();
, it supposes that splitText[0]
is the file ID of your image file.
var blob = UrlFetchApp.fetch(`https://drive.google.com/thumbnail?sz=w512&id=${splitText[0]}`, { headers: { authorization: "Bearer " + ScriptApp.getOAuthToken() } }).getBlob(); // The endpoint is from https://stackoverflow.com/a/31504086
body.appendImage(blob).setWidth(sizes).setHeight(sizes);
In this case, it supposes that the variable sizes
has already been declared elsewhere.
Or, how about the following modification?
var blob = UrlFetchApp.fetch(`https://drive.google.com/thumbnail?sz=w${sizes}&id=${splitText[0]}`, { headers: { authorization: "Bearer " + ScriptApp.getOAuthToken() } }).getBlob(); // The endpoint is from https://stackoverflow.com/a/31504086
body.appendImage(blob);
splitText[0]
is changed and put into the Document with appendImage(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.As a test case, I tested the following situation.
The sample script for testing is as follows.
function sample1() {
const fileId = "###"; // PNG image file of 4,705,077 bytes and 1536 x 1536 pixels
var size = 512;
const doc = DocumentApp.getActiveDocument();
const body = doc.getBody();
const blob = DriveApp.getFileById(fileId).getBlob();
body.appendImage(blob).setWidth(size).setHeight(size);
doc.saveAndClose();
const pdfFileSize = DriveApp.getFileById(doc.getId()).getBlob().getBytes().length;
console.log(pdfFileSize); // 6413647
}
In this case, the exported PDF file size was 6,413,647 bytes.
function sample2() {
const fileId = "###"; // PNG image file of 4,705,077 bytes and 1536 x 1536 pixels
var size = 512;
const doc = DocumentApp.getActiveDocument();
const body = doc.getBody();
var blob = UrlFetchApp.fetch(`https://drive.google.com/thumbnail?sz=w${size}&id=${fileId}`, { headers: { authorization: "Bearer " + ScriptApp.getOAuthToken() } }).getAs(MimeType.JPEG);
body.appendImage(blob);
doc.saveAndClose();
const pdfFileSize = DriveApp.getFileById(doc.getId()).getBlob().getBytes().length;
console.log(pdfFileSize); // 706321
}
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 to getBlob()
, 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 between getAs(MimeType.JPEG)
and getBlob()
. Because the mimeType of the image is not changed from JPEG.
I think that about sample2
, when size
is large, the exported PDF file size is also large.