2
votes

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

1
I think that your script works. And also, in my environment, I could confirm that your script worked. Unfortunately, I couldn't replicate your situation. I apologize for this. So for example, what version of googleapis are you using? If you use older one, how about updating it? - Tanaike
I am using version 6.13.6, I also tried to delete the JSON token and re-authenticate... no solution yet, the frustrating part is that my script worked yesterday - Luis
so... i copy the code and run it in another computer and it worked... does anyone may know why ? - Luis
Thank you for replying. About I am using version 6.13.6, I cannot understand it. Because in the current stage, the latest version of googleapis for Node.js is v61.0.0. Ref But I'm glad your issue was resolved. - Tanaike

1 Answers

0
votes

I re-installed the package and it work