Tuesday, 7 September 2021

Bulk convert Google Docs to PDFs

As of February 2023 this tool is now available as a Google Workspace Marketplace Add-on.

The following Google Apps Script tool is designed to bulk convert all Google Docs within a given Google Drive folder into PDFs. You can choose the destination folder for the PDFs to be put into and also whether you want the original Docs to be deleted. This tool does work on Shared drives.

Bulk convert Google Docs to PDFs using Apps Script
Bulk convert Google Docs to PDFs using Apps Script

Instructions

Use the 'Welcome' sheet to complete the necessary information before proceeding. All of the Google Docs need to be in the same folder. Either use the Start button or the Admin menu to start the process.

Toast notifications will appear at the bottom right of the Google Sheet to inform you of the progress. The Log sheet can be used to diagnose any problems you encounter.

The new PDFs will use the same filename as the Google Docs. You can use the dropdown on the Welcome sheet to decide if you want to delete the original Docs - selecting 'No' or leaving it blank will not delete the Docs.


Download

Bulk convert Google Docs to PDFs download here (please use 'File' > 'Make a copy' for your own version).

As of February 2023 this tool is now available as a Google Workspace Marketplace Add-on


16 comments:

  1. Hi! Thank you for such a fantastic tool! Your video walkthrough helped me troubleshoot locating the file ID! I am currently running an issue where the toast box claims "Tax has successfully completed" yet no files have been converted or placed into the destination file. Do you have any ideas of what I can do to get the files in the destination folder? Thanks again!

    ReplyDelete
    Replies
    1. Hi Carrie

      Sorry to hear you are having difficulties, below are a few suggestions:

      1) There is a 'Log' tab in the Google Sheet, are you able to copy/paste what is being logged in there please (it may highlight where it is getting stuck)?
      2) Have you been able to get it working at all? Have you tried creating a simple Google Drive folder and popping a single Google Doc into it and then using the tool on that?

      Kind regards
      Phil

      Delete
  2. Hi Phil,
    Thanks for the quick reply!
    The tutorial video was very helpful in getting started. I believe I have narrowed down the issue to shared documents. When I convert an unshared document, the script works and the document is successfully converted. When I try to convert a shared document, the textbox then reads, "Tool complete. 0 PDFs created. No errors. PDFs have been created for all Google Docs."

    The log reads as follows:
    Script has started
    Starting 'convertToPdf' Function
    Option to delete original Google Docs is: No
    Starting to convert files...
    Getting Script Properties.
    No Continuation Token exists. Converting Docs...
    Completed converting 0 Docs in folder
    Deleting Script Properties.
    Successfully deleted Script Properties.

    When I successfully convert (unshared) documents, the names are erased. That could be due to the fact that my naming system includes numbers (1.1. Document 1). Everything after the "1." disappears to look like, "1.1.pdf."

    Thanks for the help!

    ReplyDelete
    Replies
    1. Hi

      Thank you for providing further details.

      I'm unable to recreate the error from your suggestion unfortunately - I have a folder of Docs and if I share them individually or the folder as a whole, the conversion works perfectly fine either way.

      With regards to the naming convention - I was able to recreate that error! I have added an extra step in the code to resolve this, all you need to do is download a new copy of this tool (I have updated the link on this page).

      Kind regards
      Phil

      Delete
  3. Great Script!! It works great.

    I tried changing the mimetype from GOOGLE_DOCS to MICROSOFT_WORD and ran the script and got the follwing error: Error during file iteration/conversion: Exception: Conversion from application/vnd.openxmlformats-officedocument.wordprocessingml.document to application/pdf failed.

    Is there a way to make it so that it converts borth docx and google doc files?

    ReplyDelete
    Replies
    1. Thank you Elvy

      I'm not currently aware of a way to directly convert Microsoft Word files to PDF. Work I've done with native Microsoft files has involved needing to convert them to their Google-equivalent first, before working on them.

      My latest blog post (https://www.pbainbridge.co.uk/2022/05/bulk-convert-excel-files-to-google.html) is an example of this with Excel files. My initial thoughts are whether you could tweak this MimeType to GOOGLE_DOCS and then stick the PDF converter on it ... ?

      Kind regards
      Phil

      Delete
  4. What can I say? I am deeply impressed
    and very grateful.
    With kind regards
    Xavier

    ReplyDelete
  5. Hi what should I change in the script so I can convert Google Sheets to PDF?

    ReplyDelete
    Replies
    1. Hi

      Change Line 154 from GOOGLE_DOCS to GOOGLE_SHEETS

      Kind regards
      Phil

      Delete
  6. Will it be possible to bulk convert PDF to Google docs ? with OCR being on it .

    ReplyDelete
    Replies
    1. Hi, that is not something I've done directly to be honest but it looks like it's feasible with the Drive API 'ocr' option (https://developers.google.com/drive/api/v2/reference/files/insert).

      Delete
  7. Hi Phil,

    Awesome script. Would it be possible to it to continue to iterate through all sub-folders? I have a ton of student files that need to be converted to pdfs. So in each student folder, there are sub-folders with the school year (2020-21, 2021-22). Sometimes there are 8 sub folders. And then there are sub folders within each of those folders.

    What I would like to do is to select the file that contains all students and for the script to change all files. Does that make sense?

    ReplyDelete
    Replies
    1. Hi Matt

      Thank you.

      Yes it is possible to iterate through subfolders and it involves getting folders within the folder ... within the folder ... etc. There is a big caveat however, Apps Script can only run for up to 6 minutes. If you try to throw too much at it and it does not complete it will timeout/error and you will struggle to continue where it got to - because it will not do this automatically.

      I have an upgraded version of this blog post (https://www.pbainbridge.co.uk/2021/10/bulk-convert-google-docs-to-pdfs-20.html) where I have built in a timer so it can continue (still only a single level of folders however). I am yet to find a way to apply the same runtime principle to subfolders and for it to be able to resume if it fails to complete within 6 minutes.

      Delete