使用DriveApp.createFile创建PDF文件,其结果为UrlFetchApp.fetch

时间:2015-05-13 06:28:16

标签: pdf google-apps-script

我无法使用UrlFetchApp.fetch检索的内容在Google云端硬盘中创建PDF文件

如果我通过电子邮件发送附带的相同文件它正确到达。 这是我的代码:

var codigo ="1100016795766";
var url = "http://do.convertapi.com/Web2Pdf";
var headers = {"Content-Type":"multipart/form-data"
        };

var payload=  {"ApiKey":"xxxxxxxx",
             "CUrl":"http://www.correos.cl/SitePages/seguimiento/seguimiento.aspx?envio="+codigo,
             "LowQuality":true
            }
var options = { "method":"POST",
  "headers": headers,
  "payload":payload,
  "muteHttpExceptions":false
  };
var       pdf0=UrlFetchApp.fetch(url,options);

//trying several ways
var        pdf=pdf0.getContent();
var       pdf1=pdf0.getAs('application/pdf');
var       pdf2=pdf0.getAs("application/pdf");
var       pdf3=pdf0.getContentText();


//Trying several ways, none of them get a valid PDF file

DriveApp.createFile("8track_correos.pdf", pdf, 'application/pdf');
DriveApp.createFile("9track_correos.pdf", pdf0,'application/pdf');
DriveApp.createFile("1track_correos.pdf", pdf1, MimeType.PDF);
DriveApp.createFile("2track_correos.pdf", pdf1, MimeType.PDF);
DriveApp.createFile("4track_correos.pdf", pdf3);
DriveApp.createFile("7track_correos.pdf", pdf2);
DriveApp.createFile("6track_correos.pdf", pdf1);


//The attached file arrives as a good PDF file

MailApp.sendEmail("user@gmail.com", "prueba de enviar PDF", "k", {attachments:[{fileName:prefijo+p_nombre+p_apellido+"track_correos.pdf", content:pdf, mimeType:"application//pdf"}]}); 

1 个答案:

答案 0 :(得分:1)

允许blob作为文件源的唯一createFile()配置是:

DriveApp.createFile(BLOB);

所以,你需要使用:

var pdf0 = UrlFetchApp.fetch(url,options);
var pdf1 = pdf0.getAs('application/pdf');
var myNewFile = DriveApp.createFile(pdf1);
myNewFile.setName("track_correos.pdf");