Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

676
Views
How to export Google Sheets to PDF in a custom format with Google Api v3?

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",
    [...]
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

You can use link to download the file.

https://docs.google.com/spreadsheets/d/[YOUR-ID]/export?format=pdf&portrait=false&size=A4

over 4 years ago · Santiago Trujillo Report

0

If you check the documentation for Files.export

enter image description here

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.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!