I'm trying to convert a Docs file to PDF in GAS, using Drive API.
Based on what I found, basically the steps are:
- Get the Docs file
- Convert to PDF
- Upload to Drive
What I did was use the 'export' function to get the Docs and convert it to PDF, then 'create' function to upload. My code below:
function test(){
let service = getOAuthService();
service.reset();
// Get the Docs file
let url = encodeURI("https://www.googleapis.com/drive/v3/files/<id of file>/export?mimeType=application/pdf&supportsAllDrives=true");
let docFile = getGoogleAPI(url);
var fileName = "my-test-file.pdf";
var fileContent = docFile.getBlob();
var fileMetadata = {
name: fileName,
parents: ["<id of parent folder>"],
mimeType: "application/pdf"
};
url = "https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart&supportsAllDrives=true";
var header = {
"Authorization": "Bearer " + service.getAccessToken(),
"Content-Type": "multipart/related; boundary=<BOUNDARY_STRING>"
};
var body = "--<BOUNDARY_STRING>\r\n" +
"Content-Type: application/json; charset=UTF-8\r\n\r\n" +
JSON.stringify(fileMetadata) + "\r\n" +
"--<BOUNDARY_STRING>\r\n" +
"Content-Type: application/pdf\r\n\r\n" +
fileContent + "\r\n" +
"--<BOUNDARY_STRING>--";
let response = UrlFetchApp.fetch(url, {
method: "post",
headers: header,
payload: body,
muteHttpExceptions: true
});
var responseJson = JSON.parse(response.getContentText());
if (response.getResponseCode() == 200) {
Logger.log("File uploaded successfully with ID: " + responseJson.id);
} else {
Logger.log("Error uploading file: " + responseJson.error.message);
}
}
However the generate PDF file is either empty or not in the correct PDF format (cannot preview).
I think the first part (getting the Docs file using export) works, but it's the uploading that has the problem. Maybe an incorrect encoding?
Does anyone know where I am wrong on this?
Thanks in advance!