DocsList.createFile有时会创建空白或损坏的PDF [英] DocsList.createFile sometimes creates blank or corrupted PDF

查看:42
本文介绍了DocsList.createFile有时会创建空白或损坏的PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试创建一个google文档(如果可以那样做,我希望将其创建为HTML),然后将该文档导出为pdf,以作为电子邮件附件发送.这是我的基本结构

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');

但是这很奇怪.有时会创建一个空白的pdf,有时会损坏一个无法打开的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."

任何帮助将不胜感激.

推荐答案

在将文件转换为pdf之前,保存并关闭文件很重要.如果您不调用 saveAndClose(),则您的更改仍将处于待处理状态.只需

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);

以我的经验,使用blob(而不是字节数组)处理文档要干净得多.因此,这应该可以正常工作并按预期进行.

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.

这篇关于DocsList.createFile有时会创建空白或损坏的PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆