How to add horizontal lines using Google Docs API?

I’m building an application that adds different content types to Google Documents through the API using PHP. Adding regular text works perfectly with this approach:

$updateRequests = [];
$updateRequests[] = new \Google_Service_Docs_Request(
    ['insertText' => ['text' => 'My content here',
                      'location' => ['index' => $position],
                      ],
     ]);
$batchUpdate = new \Google_Service_Docs_BatchUpdateDocumentRequest(['requests' => $updateRequests]);
$service->documents->batchUpdate($docId, $batchUpdate);

Page breaks also work using a similar method:

$updateRequests = [];
$updateRequests[] = new \Google_Service_Docs_Request(
    ['insertPageBreak' => ['location' => ['index' => $position],
                           ],
     ]);
$batchUpdate = new \Google_Service_Docs_BatchUpdateDocumentRequest(['requests' => $updateRequests]);
$service->documents->batchUpdate($docId, $batchUpdate);

Both methods work great when inserting multiple items by processing them in reverse order. However, I need to insert horizontal lines into the document. While Google Docs supports adding them manually and Apps Script has insertHorizontalRule, I cannot find any equivalent in the REST API documentation.

It’s weird that you can read horizontal rules from existing documents but apparently cannot create them through the API. Has anyone found a way to insert horizontal rules? What request type should I use?

My goal is copying content from one Google Doc to another by reading each element and recreating it in the target document. If there’s a better approach than this element-by-element copying method, that would solve my horizontal rule problem too.

I hit this exact issue on a document automation project last year. The Google Docs REST API can’t insert horizontal rules, which is super annoying since you can read them from existing docs. I found two workarounds that actually work. First, use Apps Script as a middleman - write a simple function that handles insertHorizontalRule and call it from your PHP app through the Apps Script Execution API. Second option is creating a custom paragraph with underscores or dashes styled to look like a horizontal line. It’s not exactly the same as a real horizontal rule, but it keeps everything in PHP without extra API headaches. The formatting trick worked better for me in production.

yeah, it’s super frustrating! no direct support for horizontal lines in the REST API. your best bet is def Apps Script with insertHorizontalRule() and maybe call it from your PHP app. hope that helps!

Hit this same issue migrating Google Docs content for a client. The horizontal rule gap in the REST API is a known problem. I tested a bunch of workarounds and found the Apps Script Execution API works best. Just create a simple Apps Script function that wraps insertHorizontalRule, then call it from your PHP app with proper auth. Way cleaner than hacking it with styled text elements and barely any performance hit. For your document copying workflow, try using the Drive API to duplicate the source doc first, then modify what you need instead of rebuilding everything piece by piece. This keeps all the formatting details that get lost when you reconstruct manually, and horizontal rules work fine since they’re already there.