Display Word Document in HTML Page using PHP.

You can easily render DOC or DOCX files into HTML webpages programmatically on the cloud. This enables viewing Word documents in any browser without exposing the original files. In this article, we will learn how to display a Word document in an HTML page using a REST API in PHP.

The following topics are covered:

Word to HTML Viewer REST API and PHP SDK

To render Word documents as HTML, we use the PHP SDK of GroupDocs.Viewer Cloud API. It supports rendering and viewing of document and image file formats programmatically. Install the SDK with:

composer require groupdocscloud/groupdocs-viewer-cloud

After installation, load the Composer autoload to access the SDK:

require_once('vendor/autoload.php');

Obtain your Client ID and Secret from the dashboard before proceeding7. Insert them into the code as shown:

Display Word Document in HTML Page using REST API in PHP

Follow these steps to show a Word document as HTML:

  1. Upload the DOCX file to the cloud
  2. Display Word Document in HTML Page
  3. Download the rendered file

Upload the Document

Upload the DOCX file using the sample below:

The file appears in the files section of your cloud dashboard.

Display Word Document in HTML Page using PHP

Render the uploaded Word document to HTML with these steps:

  1. Create an instance of the ViewAPI.
  2. Create a ViewOptions object.
  3. Set the input file path.
  4. Choose HTML as the ViewFormat.
  5. Initialise an HtmlOptions object.
  6. Configure properties such as IsResponsive, ForPrinting, etc.
  7. Build a CreateViewRequest using the ViewOptions.
  8. Call the createView() method to render.

The code below demonstrates how to display a Word file in HTML using a REST API in PHP.

Display Word Document in HTML Page using REST API in PHP

Display Word Document in HTML Page using REST API in PHP.

You can fine‑tune the rendering with these options:

  • Render a specific page range
$renderOptions->setStartPageNumber(1);
$renderOptions->setCountPagesToRender(3);
  • Render selected pages only
$renderOptions->setPagesToRender([1, 3]);
  • View pages in a custom order
$renderOptions->setPagesToRender([2, 1]);
  • Include comments in the output
$renderOptions->setRenderComments(true);

Download HTML Pages

The rendered HTML files are stored on the cloud and can be downloaded with the following code:

Embed Word Document into Existing HTML Page

Embed a Word document into an existing HTML page by following these steps:

  1. Create instances of the ViewAPI and FileAPI.
  2. Initialise a ViewOptions object.
  3. Set the input file path.
  4. Choose HTML as the ViewFormat.
  5. Initialise an HtmlOptions object.
  6. Configure properties such as IsResponsive, ForPrinting, etc.
  7. Build a CreateViewRequest with ViewOptions.
  8. Render with the createView method.
  9. Load an existing HTML file and retrieve the <body> element.
  10. Append each rendered page’s HTML to the body.
  11. Save the updated markup using saveHTML().
  12. Write the final file with file_put_contents().

The sample below shows how to embed a Word document into an existing HTML page using a REST API in PHP.

Embed-Word-Document-into-an-Existing-HTML-Page

Embed a Word Document into an Existing HTML Page.

Display Word Document in HTML with Watermark using PHP

Add a watermark while rendering Word to HTML with these steps:

  1. Create an instance of the ViewAPI.
  2. Initialise a ViewOptions object.
  3. Set the input file path.
  4. Choose HTML as the ViewFormat.
  5. Create and assign a Watermark object.
  6. Define watermark size and text.
  7. Build a CreateViewRequest with ViewOptions.
  8. Render using the createView method.

The code sample demonstrates how to display a Word document in HTML with a watermark using a REST API in PHP.

Display Word Document in HTML with Watermark using PHP.

Display Word Document in HTML with Watermark using PHP.

Try Online

Test the free online DOCX rendering tool built with this API: https://products.groupdocs.app/viewer/docx

Conclusion

In this article, we covered how to:

  • View a Word document in a browser‑compatible HTML page using PHP
  • Customize Word‑to‑HTML rendering options
  • Embed a Word document into an existing HTML page
  • Render Word content with a watermark
  • Programmatically upload DOCX files to the cloud
  • Download rendered HTML files from the cloud

Explore more about GroupDocs.Viewer Cloud API in the documentation. The API Reference lets you interact with the endpoints directly in your browser. For questions, visit the forum.

See Also