【发布时间】:2021-08-12 09:58:50
【问题描述】:
我将我的电子邮件保存为 PDF。它运作良好。它在过去几天停止工作并出现错误
例外:从 text/html 到 application/pdf 的转换失败
这里也有人举报:
It just stopped working - google-app-script google-drive-api createFile
function saveGmailAsPDF() {
var gmailLabels = "TESTING";
var driveFolder = "TEST2";
var threads = GmailApp.search("in:" + gmailLabels, 0, 5);
if (threads.length > 0) {
/* Google Drive folder where the Files would be saved */
var folders = DriveApp.getFoldersByName(driveFolder);
var folder = folders.hasNext() ?
folders.next() : DriveApp.createFolder(driveFolder);
/* Gmail Label that contains the queue */
var label = GmailApp.getUserLabelByName(gmailLabels) ?
GmailApp.getUserLabelByName(gmailLabels) : GmailApp.createLabel(driveFolder);
for (var t=0; t<threads.length; t++) {
//threads[t].removeLabel(label);
var msgs = threads[t].getMessages();
var html = "";
var attachments = [];
var subject = threads[t].getFirstMessageSubject();
/* Append all the threads in a message in an HTML document */
for (var m=0; m<msgs.length; m++) {
var msg = msgs[m];
html += "From: " + msg.getFrom() + "<br />";
html += "To: " + msg.getTo() + "<br />";
html += "Date: " + msg.getDate() + "<br />";
html += "Subject: " + msg.getSubject() + "<br />";
html += "<hr />";
html += msg.getBody().replace(/<img[^>]*>/g,"");
html += "<hr />";
var atts = msg.getAttachments();
for (var a=0; a<atts.length; a++) {
attachments.push(atts[a]);
}
}
/* Save the attachment files and create links in the document's footer */
if (attachments.length > 0) {
var footer = "<strong>Attachments:</strong><ul>";
for (var z=0; z<attachments.length; z++) {
var file = folder.createFile(attachments[z]);
footer += "<li><a href='" + file.getUrl() + "'>" + file.getName() + "</a></li>";
}
html += footer + "</ul>";
}
/* Convert the Email Thread into a PDF File */
//The PDF craeted is damaged by the below code
//var blob = Utilities.newBlob(html, "application/pdf").setName("mail" + ".pdf");
//var theFolderId = '{FOLDER ID}';
//var parentFolder = DriveApp.getFolderById(theFolderId);
//folder.createFile(blob);
// PDF is damaged by the below code
//var html = "<h1>Hello world</h1>";
//var blob = Utilities.newBlob(html, "application/pdf", "text.pdf");
//DriveApp.createFile(blob);
//the below code was working well.
var tempFile = DriveApp.createFile("temp.html", html, "text/html");
//the bellow line is giving the error for the last few days
//Error : Exception: Conversion from text/html to application/pdf failed
folder.createFile(tempFile.getAs("application/pdf")).setName(subject + ".pdf");
tempFile.setTrashed(true);
}
}
}
【问题讨论】:
标签: html pdf google-apps-script google-sheets