I am using the Google drive v3 and try to export a Google Spreadsheet file to PDF. Almost everything works like expected.
Exception:
the generated PDF is always in format LETTER and PORTRAIT, but i need the pdf in DIN A4 and landscape. I am not able to find any settings/parameters to submit to v3 to generate PDFs in other formats.
I have found solutions for Google drive v2 but not for Google drive v3.
Passing format or portrait as optional parameter results in an error :-(
/**
* Downloads a Google file in a specified mime type.
*
* @param string $googleDriveFileId id of the drive file which should be downloaded
* @param string $mimeType mime type
* @param string[] $optParams
*
* @return Response
*
* @link https://developers.google.com/drive/api/v3/reference/files/export
* @link https://developers.google.com/drive/api/v3/ref-export-formats
*/
public function download(string $googleDriveFileId, string $mimeType, array $optParams= []): Response
{
/** @var Response $resonse */
return $this->googleServiceProvider->getGoogleServiceDrive()->files->export(
$googleDriveFileId,
$mimeType,
$optParams
);
}
[...]
"name": "google/apiclient",
"version": "v2.11.0",
[...]
You can use link to download the file.
https://docs.google.com/spreadsheets/d/[YOUR-ID]/export?format=pdf&portrait=false&size=A4
If you check the documentation for Files.export
You will find that it takes fileId as a path parameter and mimeType as a required parameter. Then their is an option of adding fields which is a standard optional parameter for this API.
To call the endpoint you would use.
var result = service.files.export(fileId, "application/pdf");
Google will then export the file as is. To export it within the the google sheets api itself is done via the print menu it reformats it at that time.
If you add the following to the url then it will change the format
var url_ext = 'export?exportFormat=pdf&format=pdf&portrait=false'
The issue is that im not sure if this is something you can add with the PHP client library or if this is something you may have to code yourself.
If you do decide to go this route just get the access token from the $client and then you can send that as a bearer token with your request. It may be worth asking a question on the Google api php client lbiary git hub page they will know if its posable to add something to the request.