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

getDocument is not a function #9075

Closed
catbadger opened this issue Oct 27, 2017 · 6 comments
Closed

getDocument is not a function #9075

catbadger opened this issue Oct 27, 2017 · 6 comments

Comments

@catbadger
Copy link

catbadger commented Oct 27, 2017

Hi guys, we love PDFJS here. It has been working great for awhile, but we're upgrading all our js libraries and I just upgraded to the newest version yesterday.

Now I'm stuck.

I'm getting an error "PDFJS.getDocument is not a function". I've tried setting the worker path and disabling the worker as suggested in other issues and neither helps. I'd rather not disable the worker if i don't have to.

here is the failing code.

function load() {
	PDFJS.disableWorker = true;
	// Load PDFs one after another
	PDFJS.getDocument(urls[loadedCount]).then(function ( pdfDoc_ ) {
		pdfDocs = [];
		pdfDocs.push( pdfDoc_);
		totalPageCount = getTotalPageCount();
		form_item_input_div.append( $('<div id="page_container_'+ loadedCount +'"></div>').hide() );
		renderPage( 1 );
	}).catch( function (resp) {
		form_item_input_div.html($('<span type="text" class="t-text"/>').text( $.i18n._('ERROR: Unable to preview document...') ));
	} );
}

It's probably worth mentioning that the documentation should have a section covering how to use pdfjs with require as digging through issues that are years old is not super helpful.

@yurydelendik
Copy link
Contributor

Please provide complete example.

@catbadger
Copy link
Author

Where can i find an example of the latest code being used with require?

@yurydelendik
Copy link
Contributor

Where can i find an example of the latest code being used with require?

I don't understand this question. require() is not a native JavaScript API method and shall be used in some context (with webpack or node.js).

Examples can be found at https://github.com/mozilla/pdf.js/tree/master/examples . You might need to check webpack or node examples.

Closing as answered.

@catbadger
Copy link
Author

thanks anyway....

@Snuffleupagus
Copy link
Collaborator

It has been working great for awhile, but we're upgrading all our js libraries and I just upgraded to the newest version yesterday.

Did you by any chance use the PDF.js source files, e.g. the ones in https://github.com/mozilla/pdf.js/tree/master/src, as-is previously?
If so, then please note that while that may have "worked" with earlier versions of PDF.js, it was never a supported configuration. For all non-development usage of PDF.js, the source files need to be built first; please refer to e.g. https://github.com/mozilla/pdf.js/wiki/Frequently-Asked-Questions#gh-pages or https://github.com/mozilla/pdf.js#building-pdfjs for more information about downloading (or creating) built versions of PDF.js that's meant for production use.

@mkpatel1978
Copy link

The below code returns me the same error.

file name: index.js

const pdfjs = require("pdfjs");

const pdfjsimage = async (url) => {
var pageNum = 1;
// pdfjs.GlobalWorkerOptions.workerSrc = 'node_modules/pdfjs-dist/build/pdf.worker.js';
pdfjs.workerSrc='node_modules/pdfjs-dist/build/pdf.worker.js'

// pdfjs.GlobalWorkerOptions.workerSrc = require('node_modules/pdfjs-dist/build/pdf.worker.entry');
// PDFJS.disableWorker = true;

// Asynchronous download PDF

pdfjs.getDocument(url).then(function (pdf1) {
pageCount = pdf1.numPages;
document.getElementById("page_num").innerHTML = "" + pageNum;
document.getElementById("page_count").innerHTML = "" + pageCount;
return pdf1.getPage(number);
})
.then(function (page) {
// Set scale (zoom) level
var scale = 1.5;

  // Get viewport (dimensions)
  var viewport = page.getViewport(scale);

  // Get canvas#the-canvas
  var canvas = document.getElementById('the-canvas');

  // Fetch canvas' 2d context
  var context = canvas.getContext('2d');

  // Set dimensions to Canvas
  canvas.height = viewport.height;
  canvas.width = viewport.width;

  // Prepare object needed by render method
  var renderContext = {
    canvasContext: context,
    viewport: viewport
  };

  // Render PDF page
  page.render(renderContext);

  getImages(page);
});
// return await pdfjs(dataBuffer)
return null

}

function getImages(page) {

var test = [];
page.getOperatorList().then(function (ops) {
for (var i = 0; i < ops.fnArray.length; i++) {
if (ops.fnArray[i] == PDFJS.OPS.paintJpegXObject) {
console.log(ops.argsArray[i][0]);
document.getElementById("images").innerHTML = "";
test.push(ops.argsArray[i][0])
}
}
});

console.log(test);

if (pageNum === 1) {
console.log("IMAGES");
console.log(test[0]);
document.getElementById("images").innerHTML = "";

}

}

module.exports = (on, config) => {
on('file:preprocessor', cucumber())

on('task', {
getImage(url) {
return (pdfjsimage(url))
}
})

}

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

5 participants