UNPKG

@pdftron/webviewer-react-toolkit

Version:

A React component library for integrating with PDFTron WebViewer API.

141 lines (140 loc) 5.95 kB
import { __awaiter, __generator } from "tslib"; export var globalLicense = (function () { var l = undefined; return { set: function (newLicense) { l = newLicense; }, get: function () { return l; }, }; })(); /** * Convert a WebViewer Core Document into a Blob. * @param documentObj A WebViewer Core Document, or promise to get it. */ export function documentToBlob(documentObj) { return __awaiter(this, void 0, void 0, function () { var fetchedDocument, data, arr; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, documentObj]; case 1: fetchedDocument = (_a.sent()); return [4 /*yield*/, fetchedDocument.getFileData()]; case 2: data = _a.sent(); arr = new Uint8Array(data); return [2 /*return*/, new Blob([arr], { type: 'application/pdf' })]; } }); }); } /** * Convert a Blob and extension into a WebViewer Core Document. * @param blob A Blob, or promise to get it. * @param extension The file extension of the provided Blob. * @param l License key. If not provided, will try to use global license. */ export function blobToDocument(blob, extension, l) { return __awaiter(this, void 0, void 0, function () { var coreControls, fetchedBlob, document_1, e_1; return __generator(this, function (_a) { switch (_a.label) { case 0: coreControls = window.Core; return [4 /*yield*/, blob]; case 1: fetchedBlob = _a.sent(); _a.label = 2; case 2: _a.trys.push([2, 4, , 5]); return [4 /*yield*/, coreControls.createDocument(fetchedBlob, { extension: extension, l: l || globalLicense.get() })]; case 3: document_1 = _a.sent(); return [2 /*return*/, document_1]; case 4: e_1 = _a.sent(); console.warn(e_1); return [2 /*return*/, undefined]; case 5: return [2 /*return*/]; } }); }); } /** * Rotate a document 90 degrees. * @param documentObj A WebViewer Core Document, or promise to get it. * @param counterclockwise If provided, will rotate counterclockwise instead of * the default clockwise. */ export function getRotatedDocument(documentObj, counterclockwise) { return __awaiter(this, void 0, void 0, function () { var coreControls, fetchedDocument, pageNumbers, rotation; return __generator(this, function (_a) { switch (_a.label) { case 0: coreControls = window.Core; if (!documentObj) return [3 /*break*/, 3]; return [4 /*yield*/, documentObj]; case 1: fetchedDocument = (_a.sent()); pageNumbers = Array.from({ length: fetchedDocument.getPageCount() }, function (_, k) { return k + 1; }); rotation = counterclockwise ? coreControls.PageRotation.E_270 : coreControls.PageRotation.E_90; return [4 /*yield*/, fetchedDocument.rotatePages(pageNumbers, rotation)]; case 2: _a.sent(); return [2 /*return*/, fetchedDocument]; case 3: return [2 /*return*/, undefined]; } }); }); } /** * Gets the thumbnail for a document. * @param documentObj A WebViewer Core Document, or promise to get it. * @param options Additional options for the function. */ export function getThumbnail(documentObj, options) { var _a; if (options === void 0) { options = {}; } return __awaiter(this, void 0, void 0, function () { var extension, _b, pageNumber, supportedFiles, url; var _this = this; return __generator(this, function (_c) { extension = options.extension, _b = options.pageNumber, pageNumber = _b === void 0 ? 1 : _b; if (extension) { supportedFiles = (_a = window.Core) === null || _a === void 0 ? void 0 : _a.SupportedFileFormats.CLIENT; if (supportedFiles && !supportedFiles.includes(extension)) { throw new Error("Unsupported file type. Unable to get thumbnail for file extension " + extension); } } url = function () { return __awaiter(_this, void 0, void 0, function () { var fetchedDocument, canvas, url; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, documentObj]; case 1: fetchedDocument = (_a.sent()); return [4 /*yield*/, new Promise(function (resolve, reject) { var callback = function (result) { if (!result) return reject(result); resolve(result); }; fetchedDocument.loadThumbnailAsync(pageNumber, callback); })]; case 2: canvas = _a.sent(); url = canvas.toDataURL(); if (!url) throw new Error('Unable to get data url.'); return [2 /*return*/, url]; } }); }); }; return [2 /*return*/, url()]; }); }); }