0
votes

I'm trying to copy a document I have in my drive, save it as a PDF, and then email the PDF as an attachment. However, even though the PDF is always copied and sent correctly as an attachment, and is properly readable form the gmail "preview" when viewing the email chain, it is unreadable when downloaded (filetype is simply "file"). Both the original document, when viewed from my drive, and the PDF (when the document is saved as a PDF and downloaded from the drive) are readable.

To me this indicates that there is either a problem with the way I convert the document into a PDF, or the way I attach it.

Here is how I convert it to a PDF:

var newFile = DriveApp.createFile(copyFile.getAs('application/pdf'));

And here is how I attach it:

MailApp.sendEmail(email, subject, message, {
    name : "senderEmail",
    attachments : [invoicePdf]
});

I know that using the Document Google Scripts class it is possible to get a Document as a pdf using the application/pdf keyword.

I have been sending emails with attached PDFs in this manner for a while now to several people; I find it unlikely, but possible, that none of them brought this issue to my attention until recently. The attachments from both recent emails (from people who have confirmed to me that the attachments are unreadable) and older emails are unreadable for my when I go to those conversations in my gmail and download the attached PDFs.

1
You create a pdf as newFile and send a different file called invoicePdf in the above code. So wondering if there are intermediate steps that modify the file?Jack Brown
The PDF created as newFile is returned in one function, and invoicePdf is, in a separate function, set as the value returned by the function that returns newFile.Leonardo the Vinchi
I tried the above code and it worked just fine for me!Jack Brown
Try modifying your this line as follows ::: var newFile = DriveApp.createFile(copyFile.getAs('application/pdf')).setName("fileName.pdf");Umair Mohammad
@Umair: Tried that, and it works. It seems the key is ensuring that the .pdf file has ".pdf" at the end of its name.Leonardo the Vinchi

1 Answers

0
votes

Kindly change your this line

var newFile = DriveApp.createFile(copyFile.getAs('application/pdf'));

as follows

var newFile = DriveApp.createFile(copyFile.getAs('application/pdf')).setNa‌​me("fileName.pdf");

So that drive name it with .pdf extension, so that it can be opened properly.