简体   繁体   中英

Convert a PDF file to Google Doc using Google Drive API v3

I have uploaded a pdf file successfully to drive using a node server. However, I can not find a way to convert it to the google doc format so that it can be downloaded later as a docx document.

here is my actual code:

 const fileMetadata = { 'name': 'new file.pdf', //'mimeType': 'application/vnd.google-apps.document' // trying to create it as a google doc directly does // not work either }; const media = { mimeType: 'application/pdf', body: fs.createReadStream(pdf) }; drive.files.create({ resource: fileMetadata, media, fields: 'id' }, (err, createdFile) => { if (err) { console.error(err); } else { // ------- COPY AS GOOGLE DOC drive.files.copy({ fileId: createdFile.data.id, requestBody:{ mimeType:'application/vnd.google-apps.document' } }, (err, googleDocFiles) => { if (err) { console.log(err) // GaxiosError: Bad Request // error: [ // { domain: 'global', reason: 'badRequest', message: 'Bad Request' } ] } else { console.log(googleDocFiles) } }) }});

What i have tried

using the files.upload method with the same requestBody returns a 200 response, however the pdf is unchanged

I know is possible, I did it before, using the copy method however now it not working and I have no clue why *sigh

EDIT

The mimeType in the request body is responsible for the 400 error, that is very weird since I am following the steps in the documentation here:

Import to Google Docs types section

this section is explicit about importing a CSV, however in the box under that section says that I can convert a pdf to doc using the corresponding mime type

我重新安装了这个包,它工作正常

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM