Skip to content
Advertisement

Center an image in header with api google doc

i tried to insert an image in my header. it’s ok. And now i want to center the picture but i’m just stuck.

$requests[] = new Google_Service_Docs_Request(array(
        'insertInlineImage' => array(
            'uri' => 'https://myPicture.png',
            'location' => array(
                'segmentId' => $document->getDocumentStyle()->getDefaultHeaderId(),
                'index' => 0
            ),
        ),

    ));

Advertisement

Answer

  • You want to insert an inline image at the center of the header in Google Document.
  • You want to achieve this using google-api-php-client with PHP.
  • You have already been able to get and put values for Google Document using Google Docs API.

If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.

Modification point:

  • In order to put the image at the center, please add UpdateParagraphStyleRequest to the request body of the batchUpdate, because InsertInlineImageRequest has no propaty of alignment.

Modified script:

$documentId = '###';  // Please set the Document ID.
$segmentId = $document->getDocumentStyle()->getDefaultHeaderId();

$service = new Google_Service_Docs($client);
$requests = [
    new Google_Service_Docs_Request([
        'insertInlineImage' => [
            'location' => ['index' => 0, 'segmentId' => $segmentId],
            'uri' => 'https://icir.int.demedicis.fr/img/logo/veolia.png'
        ]
    ]),
    new Google_Service_Docs_Request([
        'updateParagraphStyle' => [
            'range' => ['startIndex' => 0, 'endIndex' => 1, 'segmentId' => $segmentId],
            'paragraphStyle' => ['alignment' => 'CENTER'],
            'fields' => 'alignment'
        ]
    ])
];
$batchUpdateRequest = new Google_Service_Docs_BatchUpdateDocumentRequest(['requests' => $requests]);
$result = $service->documents->batchUpdate($documentId, $batchUpdateRequest);

Note:

  • When $document->getDocumentStyle()->getDefaultHeaderId() cannot be used, please set the header ID like kix.### as the string value.
  • This is a simple modification. So please reflect this to your actual script.

References:

If I misunderstood your question and this was not the direction you want, I apologize.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement