DocsList.createFile sometimes creates blank or corrupted PDF

淺唱寂寞╮ 提交于 2021-02-05 09:43:44

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!