Convert a gdoc into image

前端 未结 1 1085
梦毁少年i
梦毁少年i 2021-01-24 01:32

is there a way to create an image (e.g. a png) from a google document? I really mean an image, not just a pdf. GetAS only creates pdf, but returns an error if contentType is set

相关标签:
1条回答
  • 2021-01-24 02:11

    How about this answer?

    Reason of error:

    makeCopy() returns File object. getAs() cannot be used for this. By this, the error occurs.

    Workaround:

    Unfortunately, in the current stage, Google Document cannot be directly exported as PNG images. So it is required to think of workarounds. Google Document can be converted to PDF. This answer uses this. As a workaround, I would like to propose to use an external API which is ConvertAPI. I thought that using the external API, the script becomes simple. This a method (PDF to PNG API) of API can be converted from PDF data to PNG data.

    When you try this, for example, you can also test this using "Free Package". When you try using "Free Package", please Sign Up at "Free Package" and retrieve your Secret key.

    Sample script:

    Before you run this script, please retrieve your Secret key and set it.

    var secretkey = "###";  // Please set your secret key.
    
    var SOURCE_TEMPLATE = "1HvqYidpUpihzo_HDAQ3zE5ScMVsHG9NNlwPkN80GHK0";
    var TARGET_FOLDER = "1Eue-3tJpE8sBML0qo6Z25G0D_uuXZjHZ";
    var url = "https://v2.convertapi.com/convert/pdf/to/png?Secret=" + secretkey;
    var options = {
      method: "post",
      payload: {File: DriveApp.getFileById(SOURCE_TEMPLATE).getBlob()},
    }
    var res = UrlFetchApp.fetch(url, options);
    res = JSON.parse(res.getContentText());
    res.Files.forEach(function(e) {
      var blob = Utilities.newBlob(Utilities.base64Decode(e.FileData), "image/png", e.FileName);
      DriveApp.getFolderById(TARGET_FOLDER).createFile(blob);
    });
    

    References:

    • makeCopy()
    • getAs()
    • ConvertAPI
    • PDF to PNG API of ConvertAPI
    0 讨论(0)
提交回复
热议问题