问题
I'm trying to create a google document (I would love to create it as HTML if this could work that way) then export that document as a pdf to send as an email attachment. Here is the basic structure that I have
var docName = "test";
var doc = DocumentApp.create(docName);
doc.appendParagraph("Hello World");
DocsList.createFile(docName, doc.getAs('application/pdf').getBytes(), 'application/pdf');
But this behaves oddly. Sometimes it creates a blank pdf, sometimes a corrupted one that doesn't open with the message "Sorry, we are unable to generate a view of the document at this time. Please try again later."
Any help would be appreciated.
回答1:
It is important that you save and close the file before converting it to pdf. If you don't call saveAndClose(), your changes will still be pending. Just do
doc.saveAndClose();
DocsList.createFile(doc.getAs('application/pdf')).rename(docName);
In my experience, it has been much cleaner to handle documents in blobs instead of byte arrays. So this should work cleanly and as expected.
来源:https://stackoverflow.com/questions/14970184/docslist-createfile-sometimes-creates-blank-or-corrupted-pdf