> For the complete documentation index, see [llms.txt](https://docs.apryse.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.apryse.com/web/get-started/samples/webviewer-generate-docx.md).

# Generate DOCX from a Blob with WebViewer

This JavaScript sample creates DOCX documents and displays them in WebViewer.  Does not require other software.

This sample shows how to easily create DOCX documents from a Blob in JavaScript, view them in the browser, and convert and download as a PDF.

WebViewer provides a slick out-of-the-box responsive UI that enables you to view, annotate and manipulate PDFs and other document types inside any web project.

Click the button below to view the full project in GitHub.

{% tabs %}
{% tab title="App.js" %}
{% code title="App.js" lineNumbers="true" %}

```js
import './App.css';
import { useEffect, useState } from 'react';
import { Document, Packer, Paragraph, TextRun } from 'docx';
import Viewer from './components/Viewer';
import WebViewerContext from './context/webviewer.js';

function App() {
  const [instance, setInstance] = useState();

  // generate DOCX document
  const generateDocx = async () => {
    const doc = new Document({
      sections: [
        {
          properties: {},
          children: [
            new Paragraph({
              children: [
                new TextRun('Hello World'),
                new TextRun({
                  text: 'Foo Bar',
                  bold: true,
                }),
                new TextRun({
                  text: '\tGithub is the best',
                  bold: true,
                }),
              ],
            }),
          ],
        },
      ],
    });

    const blob = await Packer.toBlob(doc);

    return blob;
  };

  useEffect(() => {
    const generateAndLoadDocument = async () => {
      const docBlob = await generateDocx();
      await instance.Core.documentViewer.loadDocument(docBlob, {
        extension: 'docx',
        enableOfficeEditing: true, // Enable Office Editing for the loaded document
        filename: 'blob_generated.docx', // Set the filename for the document viewer
      });
    };
    if (instance) {
      generateAndLoadDocument();


    }
  }, [instance]);

  return (
    <WebViewerContext.Provider value={{ instance, setInstance }}>
      <div className='App'>
        <Viewer />
      </div>
    </WebViewerContext.Provider>
  );
}

export default App;

```

{% endcode %}
{% endtab %}

{% tab title="Viewer.js" %}
{% code title="Viewer.js" lineNumbers="true" %}

```js
import React, { useRef, useEffect, useContext } from 'react';
import WebViewer from '@pdftron/webviewer';
import WebViewerContext from '../context/webviewer.js';

const Viewer = () => {
  const viewer = useRef(null);
  const { setInstance } = useContext(WebViewerContext);

  // IMPORTANT:
  // Add this property in App.js:  
  // `enableOfficeEditing: true,`
  // This enables Office Editing when loading each blob
  useEffect(() => {
    WebViewer(
      {
        path: '/webviewer/lib',
      },
      viewer.current
    ).then((instance) => {
      setInstance(instance);
    });
  }, []);

  return <div className='webviewer' ref={viewer} style={{height: "100vh"}}></div>;
};

export default Viewer;

```

{% endcode %}
{% endtab %}
{% endtabs %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.apryse.com/web/get-started/samples/webviewer-generate-docx.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
