I’m working on a Laravel project where I need to create a Google Document and insert several images. The main issue I’m facing is that I can’t control where the images appear in the document. When I try to use ‘offsetX’ and ‘offsetY’ properties to set custom positions for the images, I get an error saying these fields don’t exist.
The error message I’m getting:
Invalid JSON payload received. Unknown name "offsetX" at 'requests[0].insert_inline_image.object_size': Cannot find field
Here’s my current code:
public function generateDocumentWithImages(Request $request)
{
$googleClient = new Google_Client();
$googleClient->setAuthConfig('../config/google/credentials.json');
$googleClient->setApplicationName('Document Creator');
$googleClient->setScopes([\Google_Service_Docs::DOCUMENTS,\Google_Service_Drive::DRIVE]);
$docsService = new \Google_Service_Docs($googleClient);
$newDocument = new Google_Service_Docs_Document(array(
'title' => "Sample Document"
));
$createdDoc = $docsService->documents->create($newDocument);
$docId = $createdDoc->documentId;
$imageRequests[] = new Google_Service_Docs_Request(array(
'insertInlineImage' => array(
'uri' => 'https://example.com/sample-image.png',
'location' => array(
'index' => 1,
),
'objectSize' => array(
'height' => array(
'magnitude' => 100,
'unit' => 'PT',
),
'width' => array(
'magnitude' => 100,
'unit' => 'PT',
)
)
)
));
$batchRequest = new Google_Service_Docs_BatchUpdateDocumentRequest(array(
'requests' => $imageRequests
));
$result = $docsService->documents->batchUpdate($docId, $batchRequest);
return response()->json([
'success' => true,
'document_id' => $docId,
]);
}
What’s the correct way to position images when inserting them into Google Docs? Are there alternative methods to control image placement?