6

I'm using google slides API to create powerpoint presentation. Where additional required functionalities are- Export to PDF Export to HTML Export to Doc/Image format.

Is there a way given by Slides API?

Sangram Jagtap
  • 79
  • 1
  • 2
  • 7

3 Answers3

5

The Slides API doesn't support exports directly, but that functionality is available from the Drive API:

https://developers.google.com/drive/v3/reference/files/export

Just use the Slides presentationId as the Drive fileId.

Maurice Codik
  • 598
  • 2
  • 6
  • 2
    If I click on 'export to pdf' in my website, I want .pptx to be converted and downloaded as .pdf file. following above answer I received only response with encoded data – Sangram Jagtap Feb 16 '17 at 09:33
5

@Sangram

Assuming that you already have created the slide deck, the process to convert Google Slides to PDF would be as follows:

import apiclient.http as client_methods

# exporting the slide deck and specifying the desired final file type
data = drive_client.files().export(fileId=slide_id, mimeType='application/pdf').execute()

# request body to be send together the upload method
 body = {'name':file_name, 'mimeType':'application/pdf'}

# wrapping the binary (data) file with BytesIO class 
fh = client_methods.BytesIO(data)

# creating the Media Io upload class for the file (note that our original slide data is of binary type)
media_body = client_methods.MediaIoBaseUpload(fh, 
                                 mimetype='application/pdf')

# drive API v3 - .create | drive API v2 - .insert
pdf_file_id = drive_client.files().create(body=body, media_body=media_body).execute()['id']

# extra step: moving to desirable folder destination with the function method

def move_files(drive_client, id_, folder_id):
    file_ = drive_client.files().get(fileId=id_, fields='parents').execute()
    drive_client.files().update(fileId=id_, addParents=folder_id,
                           removeParents=file_['parents'][0],
                           fields='id,parents').execute()

# calling the move_files function.
# drive_client is authorized client, folder_id = desired destination directory (from url).

move_files(drive_client, pdf_file_id, folder_id)

Hope this helps.

Resources:

Drive API v3 export method

Drive API v3 create method

Drive API v3 move files between folders

Simas
  • 642
  • 8
  • 15
1

As @Codik mentioned, Google Slides API doesn't support exports directly but you can use Google Drive API for exporting.

Here is my solution. I am replacing some text in the sides as well before exporting.

$substitutions = [
        'text_block_1' => "My First Text",
        'text_block_2' => "My Second Text"
    ];

// You can fond the google slide Id in the URL
$templateId = "Here comes the Google Slide Id";

$client = $certificationService->getGoogleClient();

$driveService = new \Google_Service_Drive($client);
$slidesService = new \Google_Service_Slides($client);


$copy = new \Google_Service_Drive_DriveFile(array(
    'name' => 'Pdf Name' . ' presentation'
));

$driveResponse = $driveService->files->copy($templateId, $copy);
$fileId = $driveResponse->id;

// replaceable text should be covered with {{}} 
// example = {{text_block_1}}
$requests = [];
    foreach ($substitutions as $key => $value) {
        $requests[] = new \Google_Service_Slides_Request(
            [
                'replaceAllText' => [
                    'containsText' => [
                        'text' => '{{'.$key.'}}',
                        'matchCase' => true
                    ],
                    'replaceText' => $value
                ]
            ]
        );
    }

$batchUpdateRequest = new \Google_Service_Slides_BatchUpdatePresentationRequest(array(
        'requests' => $requests
    ));


$updateResponse = $slidesService->presentations->batchUpdate($fileId, $batchUpdateRequest);


// here you can specify the file type you want to export as
$response = $driveService->files->export($fileId, 'application/pdf', array('alt' => 'media'));


$content = $response->getBody()->getContents();

// delete the exported file from the Google Drive
$driveService->files->delete($fileId);

Note:- In order for this code to work, the google slides should be shared with the Google Service Account email.

Dula
  • 1,276
  • 5
  • 14
  • 23