Display Word Document in HTML Page using PHP.

We can easily render DOC or DOCX files in HTML webpages programmatically on the cloud. It can be useful in viewing Word documents in any browser without sharing 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 shall be covered in this article:

Word to HTML Viewer REST API and PHP SDK

For rendering Word documents in HTML, we will be using the PHP SDK of GroupDocs.Viewer Cloud API. It allows rendering and viewing of supported document and image file formats programmatically. Please install it using the following command in the console:

composer require groupdocscloud/groupdocs-viewer-cloud

After installation, please use the Composers’ autoload to use the SDK as shown below:

require_once('vendor/autoload.php');

Please get your Client ID and Secret from the dashboard before following the mentioned steps. Once you have your ID and secret, add in the code as shown below:

Display Word Document in HTML Page using REST API in PHP

We can display the content of a Word document in HTML by following the simple steps given below:

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

Upload the Document

Firstly, we will upload the DOCX file to the cloud using the code sample given below:

As a result, the uploaded file will be available in the files section of the dashboard on the cloud.

Display Word Document in HTML Page using PHP

Now, we will render or display the content of an uploaded Word document on HTML pages by following the steps given below:

  1. Firstly, create an instance of the ViewAPI.
  2. Next, create an instance of the ViewOptions.
  3. Then, provide the input file path.
  4. Also, set the ViewFormat as “HTML”.
  5. Next, initialize HtmlOptions object.
  6. Then, set various properties such as IsResponsive, ForPrinting, etc.
  7. After that, create CreateViewRequest with ViewOptions as argument.
  8. Finally, render Word to HTML using the **createView()** method.

The following code sample shows 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.

We can customize the rendering of Word to HTML by applying the following options:

  • Render specific range of pages
// Pass specific range of page numbers to render. 
// This will render all pages from starting from page 1 to 3.
$renderOptions->setStartPageNumber(1);
$renderOptions->setCountPagesToRender(3);
  • Render selected pages only
// Pass specific page numbers to render. 
// This will render only page number 1 and 3.
$renderOptions->setPagesToRender([1, 3]);
  • View pages in a specific order
// Pass page numbers in the order you want to render them
$renderOptions->setPagesToRender([2, 1]);
  • Render document with comments
$renderOptions->setRenderComments(true);

Download HTML Pages

The above code sample will save the rendered HTML page(s) on the cloud. It can be downloaded using the following code sample:

Embed Word Document into Existing HTML Page

We can also embed a Word Document into an existing HTML page by following the steps given below:

  1. Firstly, create instances of the ViewAPI and FileAPI.
  2. Next, create an instance of the ViewOptions.
  3. Then, provide the input file path.
  4. Also, set the ViewFormat as “HTML”.
  5. Next, initialize HtmlOptions object.
  6. Then, set various properties such as IsResponsive, ForPrinting, etc.
  7. After that, create CreateViewRequest with ViewOptions as argument.
  8. Then, render Word to HTML using the createView() method.
  9. Next, Load an existing HTML file and get elements of body tag
  10. Then, read HTML of each page and append into body tag
  11. After that, save the updated HTML using saveHTML() method.
  12. Finally, save the HTML file using file_put_contents() method.

The following code sample 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

We can add a watermark text while rendering Word documents to HTML pages programmatically by following the steps given below:

  1. Firstly, create an instance of the ViewAPI.
  2. Next, create an instance of the ViewOptions.
  3. Then, provide the input file path.
  4. Also, set the ViewFormat as “HTML”.
  5. Next, create and assign an instance of the Watermark.
  6. Then, set watermark size and text.
  7. After that, create CreateViewRequest with ViewOptions as argument.
  8. Finally, render Word to HTML using the createView() method.

The following code sample shows 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

Please try the following free online DOCX rendering tool, which is developed using the above API. https://products.groupdocs.app/viewer/docx

Conclusion

In this article, we have learned how to:

  • view Word document in a browser supported HTML webpage using PHP;
  • customize the rendering of Word to HTML;
  • embed Word document into an existing HTML webpage;
  • view the content of a Word file in HTML with watermark;
  • programmatically upload the DOCX file to the cloud;
  • download the rendered HTML files from the cloud.

Besides, you can learn more about GroupDocs.Viewer Cloud API using the documentation. We also provide an API Reference section that lets you visualize and interact with our APIs directly through the browser. In case of any ambiguity, please feel free to contact us on the forum.

See Also