将google doc转换为pdf会导致空白pdf,google脚本 [英] Converting a google doc to a pdf results in blank pdf, google scripts

查看:96
本文介绍了将google doc转换为pdf会导致空白pdf,google脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码,用于创建Google文档(最终从电子表格中)粘贴一些信息,然后将其转换为pdf并通过电子邮件发送给我自己.

Here is my code to create a google doc paste some information in (eventually from a spreadsheet), convert it to a pdf and email it to myself.

不幸的是,虽然驱动器中的文档具有此文档是由Google Apps脚本创建的".在其中评论,电子邮件中的pdf则没有.它具有正确的标题,但是页面的内容丢失了.我已经尝试了几个有关堆栈溢出的示例,但到目前为止还没有一个可用.

Unfortunately, while the document in drive has the 'This document was created by Google Apps Script.' comment in it, The pdf in the email does not. It has the right title but the content of the page is lost. I have tried several examples on stack overflow and have not got one so far to work.

var ss = SpreadsheetApp.getActive();

function onOpen(){
var ui = SpreadsheetApp.getUi(); 
ui.createMenu('TEST MENU') //creates main menu tab 
   .addItem('pdf', 'pdf') 
   .addToUi(); 
}

function pdf() {
  // Create a new Google Doc named 'Hello, world!'
  var doc = DocumentApp.create('Hello, world!');

  // Access the body of the document, then add a paragraph.
  doc.getBody().appendParagraph('This document was created by Google Apps Script.');

 var pdfContent = doc.getAs('application/pdf');
 var draftMail = GmailApp.createDraft('will@exampleEmail.co.uk',
                                   'Email title', 'Pls see attached', 
                                   {
                                     attachments: [pdfContent.getAs(MimeType.PDF)],
                                     name: 'Converted doc content'
                                   });
// Now send the mail
draftMail.send();
}

推荐答案

您没有在将文档转换为pdf之前保存并关闭该文档,这也许就是您所做的更改未清除的原因.

You're not saving and closing the doc before converting it to pdf, maybe that's why your changed is not flushed out.

尝试这样的事情:

var ss = SpreadsheetApp.getActive();

function onOpen() {
    var ui = SpreadsheetApp.getUi();
    ui.createMenu('TEST MENU') //creates main menu tab 
        .addItem('pdf', 'pdf')
        .addToUi();
}

function pdf() {
    var doc = DocumentApp.create('Hello, world!');
    doc.getBody().appendParagraph('This document was created by Google Apps Script.');
    doc.saveAndClose()

    var pdfContent = doc.getAs('application/pdf');
    var draftMail = GmailApp.createDraft('will@exampleEmail.co.uk',
        'Email title', 'Pls see attached', {
            attachments: [pdfContent.getAs(MimeType.PDF)],
            name: 'Converted doc content'
        });
    draftMail.send();
}

参考文献: https://developers.google.com/apps-脚本/参考/文档/文档#saveandclose

这篇关于将google doc转换为pdf会导致空白pdf,google脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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