6

Is it possible for the script to convert Google Docs to PDF without using the Documents tab?
enter image description here
enter image description here

Here is the code I am writing

function convertDocToPDF() {
  const docID = DocumentApp.getActiveDocument().getId();
  const folder = DriveApp.getFolderById('1TDO4-cF83WeT4oAjFPagl7pYphwgOl4m')
  const file = DriveApp.getFileById(docID)
  const pdfBlob = file.getAs('application/pdf')
  folder.createFile(pdfBlob);
}
2
  • Hi! Can you confirm, what do you mean without without using the Documents tab, can you specify which of its innate functions do you want to get removed through a script along with more information about the file you are using like patterns and how long does each page have? Commented Nov 13, 2024 at 7:58
  • @Babanana Hi, Example like when i run the script to convert my Google Docs template only 2 page but output to PDF showing 3 page with the Documents Tab (attach link example when convert to pdf result : drive.google.com/file/d/1QBUPPQcEY6FMqEaH_8O2bkj57orPTSXr/…) Commented Nov 13, 2024 at 8:15

1 Answer 1

6

The script below creates a PDF from the active document dab without the page with the document tab title. Please note the use of the parameter tab=${tab.getId()}.

function createPDFActiveTab() {
    const doc = DocumentApp.getActiveDocument();
    const tab = doc.getActiveTab();
    const url = `https://docs.google.com/document/d/${doc.getId()}/export?format=pdf&tab=${tab.getId()}`;
    const params = {
        headers: {
            "Authorization": 'Bearer ' + ScriptApp.getOAuthToken()
        }
    };
    const response = UrlFetchApp.fetch(url, params);
    const blob = response.getBlob();
    DriveApp.createFile(blob);
}

Please remember that the document structure has changed due to Document Tabs and the methods used to handle them. The details are explained in Work with tabs.

The above script uses UrlFetchApp because Class DocumentApp and the Advanced Documents Service don't include a method to retrieve a blob from a document tab. It's worth mentioning that there have been reports that this method might fail on some documents for no apparent reason. The first thing to do is to check that the authorization value was correctly set. A common pitfall is to omit the space between Bearer and the token. If you are sure that the script doesn't have any errors, something to try is to make a copy of the document and run the script on the copy.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for sharing this, @Wicket — worked perfectly. In my case I needed to set a specific filename and return the resulting file, so I just added a filename parameter to the function, and changed the last line of your function to return DriveApp.createFile(blog.setName(filename));. (See stackoverflow.com/a/64067788 for details on setName().)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.