Skip to content Skip to sidebar Skip to footer

Converting A Google Doc To A Pdf Results In Blank Pdf, Google Scripts

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. Unfortunately, while the document in d

Solution 1:

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

Try something like this:

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

Reference : https://developers.google.com/apps-script/reference/document/document#saveandclose

Post a Comment for "Converting A Google Doc To A Pdf Results In Blank Pdf, Google Scripts"