> ## Documentation Index
> Fetch the complete documentation index at: https://webviewer-docs.mupdf.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Document API

The [`document`](#document) object is an instance accessible from the main [MuPDFWebViewer](/api-reference/introduction#mupdfwebviewer) instance as follows:

```javascript theme={null}
const document = webViewer.document;
```

<Note>
  This assumes you have returned your instance name as `webViewer` from the [initMuPDFWebViewer()](/api-reference/introduction#initmupdfwebviewer) promise!
</Note>

## document

The `document` object has the following methods:

### open

```typescript theme={null}
open(config: { url: string; filename?: string }): Promise<undefined>;
```

Opens a PDF document.

#### Parameters

<ParamField body="config" type="object" required>
  The configuration object.
</ParamField>

<Expandable title="config properties">
  <ParamField body="config.url" type="string" required>
    File path (or remote URL) to open.
  </ParamField>

  <ParamField body="config.filename" type="string">
    The file name to use for the file.
  </ParamField>
</Expandable>

#### Returns

<ResponseField name="result" type="Promise" required>
  A Promise.
</ResponseField>

<div id="open-warning" />

**Example**

```javascript theme={null}
webViewer.document.open({url:"my-file.pdf", filename:"My File"});
// remote URL
webViewer.document.open({url:"https://example.com/my-file.pdf", filename:"My File"});
```

<Warning>
  Please note: when loading a remote PDF then ensure that you have the correct CORS & CSP settings in place to provide the PDF.

  CORS (Cross-Origin Resource Sharing) and CSP (Content Security Policy) are both web security mechanisms.

  * CORS is about server-to-server communication from browsers
  * CSP is about what content your own webpage can load and execute

  If a remote PDF fails to load in **MuPDF WebViewer** then it is very possible that this might be related to one of these security mechanisms - ensure to check your console logs for any errors!
</Warning>

### download

```typescript theme={null}
download(config?: { fileName?: string; includeAnnotations?: boolean }): Promise<undefined>;
```

Downloads the PDF file.

#### Parameters

<ParamField body="config" type="object">
  Optional configuration. If omitted, the original document filename is used and annotations are included.
</ParamField>

<Expandable title="config properties">
  <ParamField body="config.fileName" type="string">
    Name of the file to download.
  </ParamField>

  <ParamField body="config.includeAnnotations" type="boolean">
    Default: `true`. Whether to include annotations.
  </ParamField>
</Expandable>

#### Returns

<ResponseField name="result" type="Promise" required>
  A Promise.
</ResponseField>

**Example**

```javascript theme={null}
webViewer.document.download({fileName:"my-file.pdf", includeAnnotations:false});
```

### getPages

```typescript theme={null}
getPages(config?: { pageRange?: string }): Promise<{ pages: PageInfo[] }>;
```

Gets PDF page information.

#### Parameters

<ParamField body="config" type="object">
  Optional configuration. If omitted, all pages are returned.
</ParamField>

<Expandable title="config properties">
  <ParamField body="config.pageRange" type="string">
    Default: `"all"`. Page index range (e.g., `"0-5, 7, 9-12"`, `"all"`). Page references are zero-indexed.
  </ParamField>
</Expandable>

#### Returns

<ResponseField name="result" type="Promise<{ pages: PageInfo[]; }>" required>
  A Promise that resolves to page information.
</ResponseField>

**Example**

```javascript theme={null}
webViewer.document.getPages({pageRange:"0-3"});

// returns e.g.

{
  pages: [
    {
      pageIndex: 0,
      read: true,
      isVisible: true,
      bbox: {
        width: 595,
        height: 842,
        x: 0,
        y: 0
      }
    }
  ]
}
```

### getPageCount

```typescript theme={null}
getPageCount(): Promise<{ pageCount: number }>;
```

Gets the total number of pages in the PDF.

#### Returns

<ResponseField name="result" type="Promise<{ pageCount: number; }>" required>
  A Promise that resolves to the page count.
</ResponseField>

**Example**

```javascript theme={null}
webViewer.document.getPageCount();

// returns e.g

{ pageCount: number }
```

### close

```typescript theme={null}
close(): Promise<undefined>;
```

Closes the currently opened PDF document.

#### Returns

<ResponseField name="result" type="Promise" required>
  A Promise.
</ResponseField>

**Example**

```javascript theme={null}
webViewer.document.close();
```

### print

```typescript theme={null}
print(config?: { pageRange: string }): Promise<PrintResult>;
```

Prints the PDF.

#### Parameters

<ParamField body="config" type="object">
  Optional configuration. If omitted, the whole document is printed.
</ParamField>

<Expandable title="config properties">
  <ParamField body="config.pageRange" type="string" required>
    Page index range (e.g., `"0-5, 7, 9-12"`, `"all"`). Page references are zero-indexed.
  </ParamField>
</Expandable>

#### Returns

<ResponseField name="result" type="Promise<PrintResult>" required>
  A Promise that resolves to the print result.
</ResponseField>

**Example**

```javascript theme={null}
webViewer.document.print({pageRange:"0-3"});

// returns e.g.

{ status: 'PRINTED' }
```

### rotatePage

```typescript theme={null}
rotatePage(config: { pageRange: string; degree: 0 | 90 | 180 | 270 | 360 }): Promise<undefined>;
```

Rotates pages.

#### Parameters

<ParamField body="config" type="object" required>
  The configuration object.
</ParamField>

<Expandable title="config properties">
  <ParamField body="config.pageRange" type="string" required>
    Page index range (e.g., `"0-5, 7, 9-12"`, `"all"`). Page references are zero-indexed.
  </ParamField>

  <ParamField body="config.degree" type="webViewer.refs.degree" required>
    Rotation degree.
  </ParamField>
</Expandable>

#### Returns

<ResponseField name="result" type="Promise" required>
  A Promise.
</ResponseField>

**Example**

```javascript theme={null}
webViewer.document.rotatePage({pageRange:"0", degree:webViewer.refs.degree.DEG_90});
```

### getText

```typescript theme={null}
getText(config?: { pageRange?: string }): Promise<{ pageIndex: number; text: string }[]>;
```

Extracts text from the PDF.

#### Parameters

<ParamField body="config" type="object">
  Optional configuration. If omitted, the whole document is used.
</ParamField>

<Expandable title="config properties">
  <ParamField body="config.pageRange" type="string">
    Default: `"all"`. Page index range (e.g., `"0-5, 7, 9-12"`, `"all"`). Page references are zero-indexed.
  </ParamField>
</Expandable>

#### Returns

<ResponseField name="result" type="Promise<{ pageIndex: number; text: string; }[]>" required>
  A Promise that resolves to extracted text for each page.
</ResponseField>

**Example**

```javascript theme={null}
async function getText() {
    return webViewer.document.getText({pageRange:"0"});
}

function successCallbackGetText(data) {
    console.log(`Text Data is: ${data[0].text}`);
}

function failureCallbackGetText(error) {
    console.error(`Error getting text: ${error}`);
}

getText().then(successCallbackGetText, failureCallbackGetText);
```

### export

```typescript theme={null}
export(config?: { includeAnnotations?: boolean }): Promise<Uint8Array>;
```

Exports the PDF. Returns the PDF data in `Uint8Array` format.

#### Parameters

<ParamField body="config" type="object">
  Optional configuration. If omitted, the document will be exported with annotations.
</ParamField>

<Expandable title="config properties">
  <ParamField body="config.includeAnnotations" type="boolean">
    Default: `true`. Whether to include annotations.
  </ParamField>
</Expandable>

#### Returns

<ResponseField name="result" type="Promise<Uint8Array>" required>
  A Promise that resolves to the exported PDF bytes.
</ResponseField>

**Example**

```javascript theme={null}
async function exportDoc() {
    webViewer.toast.show({ type: 'notification', content: 'Exporting ... please wait' });
    webViewer.document.export({includeAnnotations:true}).then(exportSuccess, exportFailure)
};

async function exportSuccess(data) {
    webViewer.toast.show({ type: 'success', content: 'Exporting complete' });
    // do what you need with the `data`
}

async function exportFailure(error) {
    webViewer.toast.show({ type: 'fail', content: 'Export error' });
    console.error(`error: ${error}`);
}

exportDoc()
```

<Note>
  See [exporting troubleshooting](/troubleshooting#how-can-mupdf-webviewer-load-a-large-document-no-problem-but-have-problems-exporting-it%3F)
</Note>

## Types Used By `document`

### `PageInfo`

```typescript theme={null}
export interface PageInfo {
  pageIndex: number;
  read: boolean;
  isVisible: boolean;
  bbox: BBox;
}
```

### `BBox`

```typescript theme={null}
export interface BBox {
  width: number;
  height: number;
  x: number;
  y: number;
}
```

### `PrintResult`

```typescript theme={null}
export type PrintResult = {
  status: 'PRINTED' | 'CANCELLED';
}
```
