I'm trying to upload files to Google Drive but with the attribute convert=true to allow a automatic conversion of upload file to Google Docs format...but it does not work:
$drive_service = new Google_Service_Drive($client);
$file = new Google_Service_Drive_DriveFile();
$file->setTitle("Informe-imaxbd-alfa");
$result = $drive_service->files->insert($file, array(
'data' => file_get_contents("notas-gdrive.docx"),
'mimeType' => 'application/octet-stream',
'uploadType' => 'multipart',
'convert' => FALSE,
));
The file is properly uploaded but without any conversion, why?
You are correct, that the proper mime type Google Drive API accepts is application/msword. I also found out as of writing this, Google Drive API does not support Word .docx format conversion for some reason.
So make sure that not only your extension is in .doc format, but actual document content is also older .doc (Word 97 - 2004) format.
Related
I have used google drive api to upload files. Here in the code of file upload, a mime type is mentioned.
$result = $service->files->create(
$file,
array(
'data' => file_get_contents($file_path),
'mimeType' => 'application/octet-stream',
)
);
i want the mime-type to be pdf and doc/docx, how can I make this happen?
I am trying to convert a local Excel.xlsx file with all existent design, format and formulas existent in my local Excel file. How can I do that using Google API with PHP?
What I was doing but not working was :
$client = new \Google_Client();
$client->setApplicationName('Name');
$client->setScopes([\Google_Service_Drive::DRIVE]);
$client->setAccessType('offline');
$client->setAuthConfig($_SERVER['DOCUMENT_ROOT'] . '/credentials.json');
$service = new Google_Service_Drive($client);
$fileID = '';
$path = $_SERVER['DOCUMENT_ROOT'] . '/includes/files/';
$fileName = 'MAIN.xlsx';//this is the file I want to convert to Google sheet
$filePathName = $path.$fileName;
$mimeType = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
$file->setMimeType($mimeType);
$createdFile = $service->files->copy($file, array(
'data' => $filePathName,
'mimeType' => $mimeType,
'convert' => true,
));
But that is not working. How should I correct?
I believe your goal as follows.
You want to upload a XLSX file from the local PC to Google Document.
When the XLSX file is uploaded, you want to convert it to Google Spreadsheet.
You want to achieve this using googleapis for PHP.
Modification points:
In your script, the method of "Files: copy" is used. This method copies the file on Google Drive. So I think that this cannot be used for achieving your goal. I think that this is the reason of your issue.
From your error message, I thought that you might be using Drive API v3. I guess that this might be also the reason of your issue.
From above points, when your script is modified, it becomes as follows.
Modified script:
$service = new Google_Service_Drive($client); // Please use your $client here.
$path = $_SERVER['DOCUMENT_ROOT'] . '/includes/files/';
$fileName = 'MAIN.xlsx';//this is the file I want to convert to Google sheet
$filePathName = $path.$fileName;
$file = new Google_Service_Drive_DriveFile();
$file->setName('MAIN.xlsx');
$file->setMimeType('application/vnd.google-apps.spreadsheet');
$data = file_get_contents($filePathName);
$createdFile = $service->files->create($file, array(
'data' => $data,
'uploadType' => 'multipart'
));
printf("%s\n", $createdFile->getId());
Note:
In this modified script, it supposes that you have already been able to get and out values for Google Drive using Drive API. Please be careful this.
In this method, the maximum file size is 5 MB. Please be careful this. When you want to upload the large file, please use the resumable upload. Ref
References:
Upload file data
Create files
I need convert html or docx use grive api.
My code
$client = new \Google_Client();
$client->setAccessToken($_SESSION['access_token']);
$service = new \Google_Service_Drive($client);
$content = file_get_contents('template.html');//template.docx
$fileMetadata = new \Google_Service_Drive_DriveFile(array(
'name' => 'test', // name file
'mimeType' => 'application/pdf', // mime type for save in gdrive
//'parents' => array($id_folder)
));
$file = $service->files->create($fileMetadata, array(
'data' => $content, // file
'mimeType' => 'text/html', // application/vnd.openxmlformats-officedocument.wordprocessingml.document
//'uploadType' => 'multipart',
'fields' => 'id')); //
File upload to dgrive normal but when I open it test.pdf i see this error
Failed to load other pages
But my file have only one page.
How can I solve this problem?
See the examples here. In Particular I dont think you need to have fields element in there which i cant find in documentation. When you upload file as text/html make sure you have correct meta data and enable multi-part as you are doing both content and meta data in one go.
I would also suggest doing it in small steps and first let it be very simple html content and try that if that works it might be the case with your content that is not letting pdf come out as normal.
If you work through some of these examples then it would be easy to debug if the problem is in meta data, content or anything else.
I am using Google Drive SDK to upload files by PHP. now I want to get the Link as string so that I can store it in my Database. But I don't have any idea on how to get it.. this is the code in creating the file in google drive using PHP.
$file = new Google_DriveFile();
$file->setTitle( 'new_pdf' );
$file->setMimeType( 'application/pdf' );
$createdFile = $service->files->insert( $file, array(
'data' => $stringContent,
));
You want one of the properties of $createdFile.
The list of properties is at https://developers.google.com/drive/v2/reference/files
You probably want webContentLink, but it depends on how it is you intend to use the URL.
Here is my code to upload a file:
private function addFile(File $file, $folderProject, User $user) {
$fileToUpload = new Google_DriveFile();
$fileToUpload->setTitle($file->getName());
//set mime type
$fileToUpload->setMimeType($file->getMimeType());
//build parent and set id
$parent = new Google_ParentReference();
$parent->setId($folderProject->getId());
//set parent to file
$fileToUpload->setParents(array($parent));
$data = file_get_contents($file->getPath());
$createdFile = $this->service->files->insert($fileToUpload, array(
'data' => $data,
'mimeType' => $file->getMimeType(),
));
}
When I upload for example a docx using this PHP code the file append to be unreadable on the Google Drive website (it shows up the XML structure of the docx) and it is showing up like this in my Google Drive folder:
And when i try to upload a docx directly through Google Drive UI it showing up like this (what i want):
I'm pretty sure that the problem is coming from the fact the Google
Drive UI tries to open the file in a different way because i uploaded
it through my app, but i don't want that :)
(i have the same problem with other kind of files)
I tried to set the parameter 'convert' = true in the array but then i got this error during the upload:
..convert=true&uploadType=multipart&key=AI39si49a86oqv9fhb9yzXp7FCWL-cSqwFiClHZqS3Y3r9Hw8ujtSNlZEWZyFKBp_id7LUyFaZTJ97pMeb0XqHznycK7JJ9o_Q:
(500) Internal Error
Documentation: https://developers.google.com/drive/v2/reference/files/insert
=> But anyway i don't think to convert is the right solution, i just want to be able to see the content as if i uploaded the file through the Google Drive UI
If i open the docx from my Google Drive Sync folders and I edit and save it, I will be able to see it through the Google Drive UI (showing with the W logo)
I also getting the same problem.
But the difference is that , I am uploading pdf and able to view pdf in google drive UI.
I need to automatically convert it to google doc format by api code after adding new pdf.
If I change $mimeType = 'application/vnd.google-apps.document'; then got below error
https://www.googleapis.com/upload/drive/v2/files?convert=true&uploadType=multipart: (400) Bad Request
if I add optional parameter
$createdFile = $service->files->insert($file, array(
'data' => $data,
'mimeType' => $mimeType,
'convert' => TRUE
));
Then no change , only pdf upload no conversion takes place.
do u have any suggestion , where I need to exactly add for convert parameter ?