Skip to main content

Getting user documents

There are three ways to get a list of documents the user belongs to.

The main, recommended way to fetch documents is with the User.getDocumentPaginator function.

You can also get all a user's documents with the User.getAllDocuments function.

Lastly, you can get a specific document with the User.getDocument API.

getDocumentPaginator#

The User.getDocumentPaginator function is used to fetch the user's documents in a paginated manner. This is the recommended way to fetch documents.

getDocumentPaginator(options): Paginator

  • options (PaginateParams) options to pass to the paginator
    • limit (number) how many entities to fetch with each request. This property is required.
    • orderBy ('updatedAt' | 'createdAt') how the entities should be ordered when querying. Defaults to 'updatedAt', meaning documents that were most recently updated will be fetched first
    • orderDirection ('ASC' | 'DESC') which direction the entities should be ordered when querying. Defaults to 'DESC' meaning newer documents are fetched first.
    • before (number - timestamp in MS) Sets the query to only fetch items that were updated/created at before this date. Used to fetch older documents. Defaults to the current time.

Returns a Paginator instance.

Example

import { CollabClient } from '@pdftron/collab-client'
const client = new CollabClient({...options})
// Create an array to store the documents we have fetched
const documents = [];
const user = await client.loginAnonymously('Joe');
// Create a paginator that fetches 10 documents at a time
const paginator = user.getDocumentPaginator({
limit: 10
});
// Get the first ten documents and set them in our `documents` state
const firstTen = await paginator.next();
documents.push(...firstTen);
// Add an event listener to a 'Load More' button.
// This function fetches the next 10 documents
// and pushes them onto our state
document.getElementById('load-more-button').onclick = async () => {
const nextTen = await paginator.next();
documents.push(...nextTen);
}

getPublicDocumentPaginator#

This API can be used to fetch all public documents in a paginated manner. Note that it will also return documents the user is already a member of.

getPublicDocumentPaginator(options): Paginator

  • options (PaginateParams) options to pass to the paginator
    • limit (number) how many entities to fetch with each request. This property is required.
    • orderBy ('updatedAt' | 'createdAt') how the entities should be ordered when querying. Defaults to 'updatedAt', meaning documents that were most recently updated will be fetched first
    • orderDirection ('ASC' | 'DESC') which direction the entities should be ordered when querying. Defaults to 'DESC' meaning newer documents are fetched first.
    • before (number - timestamp in MS) Sets the query to only fetch items that were updated/created at before this date. Used to fetch older documents. Defaults to the current time.

Returns a Paginator instance.

Example

import { CollabClient } from '@pdftron/collab-client'
const client = new CollabClient({...options})
// Create an array to store the documents we have fetched
const documents = [];
const user = await client.loginAnonymously('Joe');
// Create a paginator that fetches 10 documents at a time
const paginator = user.getPublicDocumentPaginator({
limit: 10
});
// Get the first ten documents
const firstTen = await paginator.next();
const firstDocument = firstTen[0];
// Join the latest public document if they can
if(await firstDocument.canJoin()) {
await firstDocument.join()
}

getAllDocuments#

getAllDocuments(): Promise<Document[]>

The getAllDocuments returns an array of all the users documents. This API should not be used in large scale applications, as it does not scale up with the number of documents a user belongs to.

Resolves to an array of Documents.

Example

import { CollabClient } from '@pdftron/collab-client'
const client = new CollabClient({...options})
const user = await client.loginAnonymously('Joe');
const documents = await user.getAllDocuments();

getDocument#

The getDocument API gets a single Document by its ID.

getDocument(id): Promise<Document>

  • id (string) The ID of the document to fetch

Example

import { CollabClient } from '@pdftron/collab-client'
const client = new CollabClient({...options})
const user = await client.loginAnonymously('Joe');
const document = await user.getDocument('abc');
// View the document
await document.view(`http://mywebsite.com/files/${document.id}.pdf`)