Exporting individual Google Docs tabs as PDF (PDF of a large multi-tab file) - Stack Overflow

I am trying to export an entire multi-tab Google Docs as a pdf.My document includes 15-20 tabs (NB: th

I am trying to export an entire multi-tab Google Docs as a pdf. My document includes 15-20 tabs (NB: they are not currently nested, which makes things potentially a little bit easier).

Initially, I have followed the following: This worked well, until my file grew too large ('Message details - Exception: File main.pdf exceeds the maximum file size.') - the threshold seems to be around 50 MB. My file is not gigantic, so I was quite suprised by this (it is currently about 120 pages, amounting to about 65 MB, due to the number of images).

I thought that maybe I should change my approach to only print one tab at a time, into a single pdf file and then combine them using a pdf tool (outside of GSuite). However, I am struggling to find a method to export a pdf for one tab at a time.

I am totally new to Apps Scripts / javascript; I checked the official documentation, and the guide on the new tabs feature:

I have tried expanding on from: Create PDF file from google doc app script and a few other approaches (albeit most predating the tabs feature), but so far none of them seem to work.

I am aware that there is a solution via API request (e.g. How to convert Google Docs to PDF, when using tabs?), but this is rather cumbersome for me, as I hit the 429 error (too many requests) and the only way around it was to add sleep time, which then hit the maximum script execution time limit (360 seconds, it seems), necessitating me to split the task into 3 separate parts, which is not ideal (but I guess not completely terrible)...

Is there any way of converting individual tabs to pdf, in an automated way (or alternatively in 'one go'), ideally without using API access?

I thought that, as a workaround, maybe there is a way of setting the tab to be a an entirety of a new (temporary) Google Docs and then using the method I started with? But I am not sure how to convert a tab into a doc?

This is the script (with the commented out approaches I have tried unsuccessfully):

function onOpen() {
  var ui = DocumentApp.getUi();
  ui.createMenu('Extra Menu')
    .addItem('docToPDF', 'docToPDF')
    .addToUi();
}

function docToPDF(){
  const doc = DocumentApp.getActiveDocument(); // Get the active document.
  const docID = doc.getId(); // Get ID of the active Doc file.
  const folderID = DriveApp.getFileById(docID).getParents().next().getId(); // Get ID of the folder the Doc is stored in.
  const folder = DriveApp.getFolderById(folderID); // Get the folder by ID.
  
  // --- WHOLE DOCUMENT LEVEL ---
  
  //folder.createFile(doc.getBlob()); // Worked until the file got too big
  
  //const pdfcont = doc.getAs('aplication/pdf') // THIS DOES NOT WORK (at all) - Exception: Converting from application/vnd.google-apps.document to aplication/pdf is not supported.

  // --- ONE TAB ONLY ---
  
  const ftab = doc.getTabs()[0]; // Trial with one tab only
  const ftabId = ftab.getId();

  //const ftabblob = ftab.getBlob(); // TypeError: ftab.getBlob is not a function
  //folder.createFile(ftab.asDocumentTab().getBlob()) // TypeError: ftab.asDocumentTab(...).getBlob is not a function
  //const ftabpdf = ftab.asDocumentTab().getAs('application/pdf'); // TypeError: ftab.asDocumentTab(...).getAs is not a function
  

  const ftabdoc = ftab.asDocumentTab();

  //folder.createFile(ftabdoc.getBlob()) // TypeError: ftabdoc.getBlob is not a function
  //const ftblob = ftabdoc.getBlob(); // TypeError: ftabdoc.getBlob is not a function
  //const pdfdoc = ftabdoc.getAs('application/pdf'); // TypeError: ftabdoc.getAs is not a function

  // SOME CONSOLE LOGS IN CASE OF SUCCESSFUL EXECUTION
  console.log(ftabId); 
  console.log(ftab.getTitle())

}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744123131a4559493.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信