[英]Converting a Google Docs file to PDF in Apps Script using Google Drive API
I'm trying to convert a Docs file to PDF in GAS, using Drive API.我正在尝试使用驱动器 API 将文档文件转换为 GAS 中的 PDF。
Based on what I found, basically the steps are:根据我的发现,基本上步骤是:
What I did was use the 'export' function to get the Docs and convert it to PDF, then 'create' function to upload.我所做的是使用“导出”function 获取文档并将其转换为 PDF,然后“创建”function 进行上传。 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).然而,生成的 PDF 文件要么是空的,要么不是正确的 PDF 格式(无法预览)。
I think the first part (getting the Docs file using export) works, but it's the uploading that has the problem.我认为第一部分(使用导出获取 Docs 文件)有效,但上传有问题。 Maybe an incorrect encoding?也许编码不正确?
Does anyone know where I am wrong on this?有谁知道我错在哪里?
Thanks in advance!提前致谢!
In your script, fileContent
is Blob.在您的脚本中, fileContent
是 Blob。 Unfortunately, Blob cannot be directly used in this request body.不幸的是,Blob 不能直接用于此请求正文中。 I think that this is the reason for your current issue.我认为这就是您当前问题的原因。 In this case, how about the following modification?在这种情况下,如何进行以下修改?
When your request body is modified, please modify it as follows.当您的请求正文被修改时,请修改如下。
"Content-Type: application/pdf\r\n\r\n" +
fileContent + "\r\n" +
"Content-Transfer-Encoding: base64\r\n\r\n" +
Utilities.base64Encode(fileContent.getBytes()) + "\r\n" +
As another pattern, when your fileContent
of Blob is directly used in the request body, please modify it as follows.作为另一种模式,当你的 Blob 的fileContent
直接在请求体中使用时,请修改如下。
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
});
let response = UrlFetchApp.fetch(url, {
payload: {
metadata: Utilities.newBlob(JSON.stringify(fileMetadata), "application/json"),
file: fileContent,
},
headers: { authorization: "Bearer " + service.getAccessToken() },
muteHttpExceptions: true
});
multipart/form-data
is automatically created on the internal server side.在这种情况下, multipart/form-data
的结构是在内部服务器端自动创建的。fileContent
is the correct blob, and also, it supposes that your access token of "Bearer " + service.getAccessToken()
can be used for uploading the file to Google Drive.在此修改中,它假定您的fileContent
值是正确的 blob,并且还假定您的访问令牌"Bearer " + service.getAccessToken()
可用于将文件上传到 Google 云端硬盘。 Please be careful about this.请注意这一点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.