PDF files corrupted - Can't open

555 views
Skip to first unread message

Alan Wells

unread,
Feb 11, 2020, 4:30:12 PM2/11/20
to Google Apps Script Community
Many of my users are reporting an issue with PDF files.  The Google Drive PDF preview service to open PDF files will often fail to open the PDF file with the error:

There was a problem previewing this document. 

Or a message that the PDF file may be corrupted.

One user reports that previously, they could not open the PDF file in MS Word, but now they can!  So, now the PDF can be opened in MS Word, but there is an error in the preview from Drive, but before the problems, the PDF could be previewed from Drive, but couldn't be opened in MS Word.

I'm wondering if something in Apps Script changed internally? 

Martin Hawksey

unread,
Feb 11, 2020, 5:03:48 PM2/11/20
to google-apps-sc...@googlegroups.com
Are you a couple of ASCII characters off in the exports? This was noted by Drew White on Twitter :

@AppsScriptInfo
 I've just had a PDF exported from Sheets (using longstanding, unchanged GAS) that came out garbled.
Each ASCII char offset by a few from the intended one.
Tried several times - same result.
Left it for 10 minutes.. No code change. All back to normal!
Wonder why?
 
image.png
image.png

Alan Wells

unread,
Feb 11, 2020, 8:47:11 PM2/11/20
to Google Apps Script Community
I don't know, but something to check out.

Alan Wells

unread,
Feb 12, 2020, 9:08:14 PM2/12/20
to Google Apps Script Community
It seems like there has been a change internally to how the mime type of files is determined.  I had a blob that returned a mime type of application/pdf and a file that was already an application/pdf, but got an error with Drive.Files.update, that it couldn't convert a "text/html" file to an application/pdf.  But, it didn't need to convert it.  They were both already application/pdf types according to the code.   I think the problem was that the utilities service was not evaluating HTML tags, and leaving them in the content, and so the Drive.Files.update thought that the source blob was html even though it had a mime type of application/pdf.
The old code that worked probably had to many steps in it anyway.
I thought it might have been the V8, but changing back to Rhino didn't fix anything.
Basically I changed from:

//This worked previously but doesn't seem to any more
blob = HtmlService.createHtmlOutput(html).getBlob;//To test if the HTML is bad
pdfBlob = blob.getAs('application/pdf')

to:
pdfBlob = Utilities.newBlob(html, "application/pdf");


Christopher Webster

unread,
Nov 29, 2023, 6:02:36 PM11/29/23
to Google Apps Script Community
So I remember dealing with this issue before and Alan's suggestion working.  Lately, however,  the script that our company has used for years to convert many daily emails into PDF files placed in drive folders has been randomly producing corrupted files again.  The script has not been changed for quite a while.  These corrupted PDFS are strange in that while they cannot be downloaded or accessed, Google does allow a preview of one page but under the preview states "Couldn't preview file. There was a problem loading more pages."

Anyone else having these issues or might have a solution to them?

Tanaike

unread,
Nov 29, 2023, 7:26:45 PM11/29/23
to Google Apps Script Community
I have to apologize for my poor English skill. Unfortunately, from your post, I cannot imagine the flow for correctly replicating your current issue. In order to correctly understand your current issue, can you provide the detailed flow for correctly replicating your current issue by including your current script? First, I would like to correctly understand your current issue.

Christopher Webster

unread,
Nov 30, 2023, 12:48:14 PM11/30/23
to Google Apps Script Community
Thank you for taking interest in my post Tanaike.

Here are the core lines of the script:

function makeFileFromEmail(title, message){
var html = HtmlService.createHtmlOutput(message.getBody())
var blob = Utilities.newBlob(html.getContent(), "application/pdf", title + ".pdf")
return proccessFile(blob, title);
}
function proccessFile(blob,name){
if(!JOB_NUMBER_RE.test(name)) return;
var file = createFile(blob,name)
var md5=Drive.Files.get(file.getId()).md5Checksum;
var jobNumber = ((JOB_NUMBER_RE.exec(name))[0]).toUpperCase();
if(MD5.check(jobNumber,md5)) return file.setTrashed(true);
return file;
}

function createFile(blob,name){
var file = DriveApp.getFolderById(FILE_SORTER_FOLDER_ID).createFile(blob);
var ext = mime.getExtension(file.getMimeType());
file.setName(name+'.'+ext);
return file;
}

This script has been running for years and has created thousands of pdfs from Gmail messages.  As I mentioned before, some but not all of the pdfs have lately been created as corrupted files.  

I suspect there is a bug on Googles side so I am asking if others are having issues.

Tanaike

unread,
Nov 30, 2023, 10:00:09 PM11/30/23
to Google Apps Script Community
Thank you for replying. I would like to support you. But, I have to apologize for my poor English skill, again. Unfortunately, from your reply, I cannot still understand your question. But I would like to try to understand it. When I could correctly understand it, I would like to think of a solution. I would be grateful if you could forgive my poor English skill.
Reply all
Reply to author
Forward
0 new messages