Я пытаюсь отобразить PDF-файл в пользовательском веб-представлении в моем приложении xamarin.forms.
Как работает программа прямо сейчас:
1. PDF загружается в каталог PDF на телефоне
2. Файл pdf.js viewer.html загружается в пользовательское веб-представление с помощью пользовательского средства визуализации с параметром? File = http://localhost:8000/filepath
3. Ошибки выдаются
Я добавил http://localhost:8000 в HOSTED_VIEWER_ORIGINS в viewer.js и прокомментировал код, который проверяет, находятся ли файл viwer.html и pdf с того же хоста, что и было предложено в моем исследовании. Это устранило большинство ошибок, но ошибка в названии сохраняется.
Код для загрузки PDF-файла в viewer.html, а также в пользовательский веб-просмотр (находится здесь: https://github.com/xamarin/docs-archive/tree/master/Recipes/xamarin-forms/Controls/display-pdf):
var customWebView = Element as CustomWebView;
Control.Settings.AllowUniversalAccessFromFileURLs = true;
//, string.Format("file:///android_asset/Content/{0}"), WebUtility.UrlEncode(customWebView.Uri)
Control.LoadUrl(string.Format("file:///android_asset/pdfjs/web/viewer.html?file={0}", string.Format("http://localhost:8000/{0}", WebUtility.UrlEncode(customWebView.Uri))));
Слегка отредактированный метод validateFileUrl:
var HOSTED_VIEWER_ORIGINS = ['null', 'http://mozilla.github.io', 'https://mozilla.github.io', 'http://localhost:8000'];
validateFileURL = function validateFileURL(file) {
if (file === undefined) {
return;
}
try {
var viewerOrigin = new _pdfjsLib.URL(window.location.href).origin || 'null';
if (HOSTED_VIEWER_ORIGINS.includes(viewerOrigin)) {
return;
}
var _ref14 = new _pdfjsLib.URL(file, window.location.href),
origin = _ref14.origin,
protocol = _ref14.protocol;
//if (origin !== viewerOrigin && protocol !== 'blob:') {
// throw new Error('file origin does not match viewer\'s');
//}
} catch (ex) {
var message = ex && ex.message;
PDFViewerApplication.l10n.get('loading_error', null, 'An error occurred while loading the PDF.').then(function (loadingErrorMessage) {
PDFViewerApplication.error(loadingErrorMessage, { message: message });
});
throw ex;
}
};
In theory, the pdf should load into the pdf viewer. However, in practice, I get this error message:
"Uncaught (in promise) DataCloneError: Failed to execute 'postMessage' on 'Worker': TypeError: Failed to fetch could not be cloned.", source: file:///android_asset/pdfjs/build/pdf.js (13127)
[INFO:CONSOLE(985)] "Uncaught (in promise) Error: An error occurred while loading the PDF.", source: file:///android_asset/pdfjs/web/viewer.js (985)06-25 10:48:12.320 I/chromium( 9820): [INFO:CONSOLE(13127)] "Uncaught (in promise) DataCloneError: Failed to execute 'postMessage' on 'Worker': TypeError: Failed to fetch could not be cloned.", source: file:///android_asset/pdfjs/build/pdf.js (13127)
06-25 10:48:12.320 I/chromium( 9820): [INFO:CONSOLE(985)] "Uncaught (in promise) Error: An error occurred while loading the PDF.", source: file:///android_asset/pdfjs/web/viewer.js (985)