Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Could not be cloned #8429

Closed
KTPenrose opened this issue May 19, 2017 · 1 comment
Closed

Could not be cloned #8429

KTPenrose opened this issue May 19, 2017 · 1 comment

Comments

@KTPenrose
Copy link

KTPenrose commented May 19, 2017

I unfortunately can't upload the PDF because I don't have authorization to do so in my massive bureaucracy.

Web browser: Chrome 58.0.3029.110 (64-bit)
Windows 10
PDF.js version 1.8.188 (although main fails as well)

Background; I am using the library to extract the contents of form fields (getAnnotations()), and NOT to display the document in the browser. The gist of the problem is that a call to page.getAnnotation() fails when there is a form field (such as in my case a signature block) that can't be serialized. It throws an exception deep in util.js and abandons the whole call to page.getAnnotation() as below;

:8888/src/shared/util.js:1301 

Uncaught (in promise) DataCloneError: Failed to execute 'postMessage' on 'DedicatedWorkerGlobalScope': function NetworkManager_getXhr() {
		return new XMLHttpRequest();
	  } could not be cloned.

I tried catching the exception in my code by wrapping the call to page.getAnnotations() in a try catch, but it doesn't seem to work. It does work to comment out that block of code.

If someone could tell me how to just ignore problematic fields/annotations this would be a wonderful solution.

'use strict';

// In production, the bundled pdf.js shall be used instead of SystemJS.
Promise.all([SystemJS.import('pdfjs/display/api'),
			 SystemJS.import('pdfjs/display/global')])
	   .then(function (modules) {
  var api = modules[0], global = modules[1];
  // In production, change this to point to the built `pdf.worker.js` file.
  global.PDFJS.verbosity=global.PDFJS.VERBOSITY_LEVELS.infos;
  global.PDFJS.workerSrc = '../../src/worker_loader.js';

  // Fetch the PDF document from the URL using promises.
  api.getDocument('myDocument.pdf').then(function (pdf) {
	// Fetch the page.
	var numPages = pdf.numPages;
	var myResult="";
	function myCallback(pageNumber, text) {
		//alert("processing page "+pageNumber+", text="+text);
		document.getElementById("myFormDiv").innerHTML+=text;
	}
	for (var pageNum = 1; pageNum<=numPages; pageNum++) {
		function doit(currentPageNumber) {
			//alert("processing page "+currentPageNumber);
			var response="<h1>Page "+currentPageNumber+"</h1>\n";
			pdf.getPage(currentPageNumber).then(function (page) {
					  
				function escapeHtml(unsafe) {
					if (typeof unsafe !== 'string') { return ""; }
					return unsafe.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&#039;");
				}

				//attempt to read the data!!!!
				page.getAnnotations().then(function(items) {
					console.log(items); //Dump this into the console to see what properties are available for each field				
					items.forEach(function(item) {
						response+="<div>"+escapeHtml(item.alternativeText)+":"+escapeHtml(item.fieldValue)+"<div>\n";
					});
					myCallback(currentPageNumber, response);			
				}); //end getAnnotations()
					
			}); //end getPage()
			
			return response;
		} //end doit()
		doit(pageNum);
	}//end iterating pages
  }); //end getDocument()
});
@KTPenrose
Copy link
Author

Problem solved with the following added line... I just had to state my intention.

			var parameters = { intent: 'display' };
			page.getAnnotations(parameters).then(function(items) {

andrenarchy added a commit to paperhive/frontend that referenced this issue Oct 22, 2017
Failed to render 10.1007/978-3-662-54809-7 otherwise.
Could fix it quickly thanks to mozilla/pdf.js#8429.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant