【发布时间】:2019-01-15 18:05:40
【问题描述】:
我正在尝试使用 puppeteer 登录网站并将 pdf 直接“下载”到我的驱动器。我已经设法使用 puppeteer 到达 pdf 页面,并且我尝试(在其他尝试之间)使用 fetch 和 cookie 来获取 blob 以发送到驱动器。我不能在这里发布登录信息,但如果你能帮我在代码中查找错误(或更多),那就太好了!目前,它转到 pdf 之前的页面,获取链接,使用 cookie 获取并在驱动器中插入 pdf,但 pdf 已损坏 0 kb。
我尝试了 setRequestInterception、getPdf(来自 puppeteer)并使用缓冲区和我在研究中发现的一些东西。
//Page before pdfPage. Here I got the link: urlPdf
//await page.goto(urlPdf);
//await page.waitForNavigation();
//const htmlPdf = await page.content();
const cookies = await page.cookies()
const opts = {
headers: {
cookie: cookies
}
};
let blob = await fetch(urlPdf,opts).then(r => r.blob());
console.log("pegou o blob")
// upload file in specific folder
var file ;
console.log("driveApi upload reached")
function blobToFile(req){
file = req.body.blob
//A Blob() is almost a File() - it's just missing the two properties below which we will add
file.lastModifiedDate = new Date();
file.name = teste.pdf;//req.body.word;
return file;
}
var folderId = myFolderId;
var fileMetadata = {
'name': 'teste.pdf',
parents: [folderId]
};
var media = {
mimeType: 'application/pdf',
body: file
};
drive.files.create({
auth: jwToken,
resource: fileMetadata,
media: media,
fields: 'id'
}, function(err, file) {
if (err) {
// Handle error
console.error(err);
} else {
console.log('File Id: ', file.data.id);
}
});
【问题讨论】:
标签: javascript node.js google-drive-api puppeteer