Skip to main content

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.

The document object is an instance accessible from the main MuPDFWebViewer instance as follows:
const document = webViewer.document;
This assumes you have returned your instance name as webViewer from the initMuPDFWebViewer() promise!

document

The document object has the following methods:

open

open(config: { url: string; filename?: string }): Promise<undefined>;
Opens a PDF document.

Parameters

config
object
required
The configuration object.

Returns

result
Promise
required
A Promise.
Example
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"});
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!

download

download(config?: { fileName?: string; includeAnnotations?: boolean }): Promise<undefined>;
Downloads the PDF file.

Parameters

config
object
Optional configuration. If omitted, the original document filename is used and annotations are included.

Returns

result
Promise
required
A Promise.
Example
webViewer.document.download({fileName:"my-file.pdf", includeAnnotations:false});

getPages

getPages(config?: { pageRange?: string }): Promise<{ pages: PageInfo[] }>;
Gets PDF page information.

Parameters

config
object
Optional configuration. If omitted, all pages are returned.

Returns

result
Promise<{ pages: PageInfo[]; }>
required
A Promise that resolves to page information.
Example
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

getPageCount(): Promise<{ pageCount: number }>;
Gets the total number of pages in the PDF.

Returns

result
Promise<{ pageCount: number; }>
required
A Promise that resolves to the page count.
Example
webViewer.document.getPageCount();

// returns e.g

{ pageCount: number }

close

close(): Promise<undefined>;
Closes the currently opened PDF document.

Returns

result
Promise
required
A Promise.
Example
webViewer.document.close();

print

print(config?: { pageRange: string }): Promise<PrintResult>;
Prints the PDF.

Parameters

config
object
Optional configuration. If omitted, the whole document is printed.

Returns

result
Promise<PrintResult>
required
A Promise that resolves to the print result.
Example
webViewer.document.print({pageRange:"0-3"});

// returns e.g.

{ status: 'PRINTED' }

rotatePage

rotatePage(config: { pageRange: string; degree: 0 | 90 | 180 | 270 | 360 }): Promise<undefined>;
Rotates pages.

Parameters

config
object
required
The configuration object.

Returns

result
Promise
required
A Promise.
Example
webViewer.document.rotatePage({pageRange:"0", degree:webViewer.refs.degree.DEG_90});

getText

getText(config?: { pageRange?: string }): Promise<{ pageIndex: number; text: string }[]>;
Extracts text from the PDF.

Parameters

config
object
Optional configuration. If omitted, the whole document is used.

Returns

result
Promise<{ pageIndex: number; text: string; }[]>
required
A Promise that resolves to extracted text for each page.
Example
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

export(config?: { includeAnnotations?: boolean }): Promise<Uint8Array>;
Exports the PDF. Returns the PDF data in Uint8Array format.

Parameters

config
object
Optional configuration. If omitted, the document will be exported with annotations.

Returns

result
Promise<Uint8Array>
required
A Promise that resolves to the exported PDF bytes.
Example
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()

Types Used By document

PageInfo

export interface PageInfo {
  pageIndex: number;
  read: boolean;
  isVisible: boolean;
  bbox: BBox;
}

BBox

export interface BBox {
  width: number;
  height: number;
  x: number;
  y: number;
}

PrintResult

export type PrintResult = {
  status: 'PRINTED' | 'CANCELLED';
}