8

I'm try to adapt pdf.js (complete version) to my needs, I just want to set the initial scale to 'page-fit' whatever the previous scale. So I try to modify viewer.js to achieve this ...

First the DEFAULT_SCALE global (line 27) is ignored, it seems that the initial scale is defined by the loading sequence according to the previous scale.

The document is loaded by the load function (line 832) wich is called below, but the 'scale' argument doesn't set the initial scale ...

load: function pdfViewLoad(pdfDocument, scale) {
   ...
}

Attempting to set the scale in the firstPagePromise.then ... function (line 903) has no effect either :

firstPagePromise.then(function(pdfPage) {
   scale = 'page-fit';
   ...
}

There's also a PDFView.currentScale property, I have tried to set it in different places but this doesn't affect either :

this.currentScale = 'page-fit';

Finally I can set this scale by calling the setScale function into onePageRendered.then ... function (line 920) :

onePageRendered.then(function () {
   PDFView.setScale('page-fit', true);
   ...
}

It works, but the setScale function is then called twice, because it seems to be called a first time by onePageRendered (I don't know where) with the previous scale (?). This solution make two visible renders and is not really satisfying ...

How can I do that properly please ?

Community
  • 1
  • 1
jeum
  • 1,048
  • 3
  • 13
  • 29

2 Answers2

1

To set default view in 'page-fit' whatever the previous selection, you need to simply change two line in viewer.js file.

  • Step 1: change var DEFAULT_SCALE_VALUE = 'auto' to 'page-fit';
  • Step 2: change DEFAULT_PREFERENCES object 'showPreviousViewOnLoad' property value:
var DEFAULT_PREFERENCES = {
  //if you set false view didn't load view by previous scale. Always load by default scale defined by step 1.
  showPreviousViewOnLoad: true to false,
  defaultZoomValue: '',
  sidebarViewOnLoad: 0,
  enableHandToolOnLoad: false,
  enableWebGL: false,
  pdfBugEnabled: false,
  disableRange: false,
  disableStream: false,
  disableAutoFetch: false,
  disableFontFace: false,
  disableTextLayer: false,
  useOnlyCssZoom: false,
  externalLinkTarget: 0,
};
ssuperczynski
  • 3,190
  • 3
  • 44
  • 61
0

The solution is here, thank you Snuffleupagus :

Since the initial scale could be set in a number of ways (from the browser history, by a preference, by the view history or by specifying the scale with a hash parameter), I think that the only way to always force a particular scale on load would something along these lines:

  1. Change DEFAULT_SCALE to what you want: https://github.com/mozilla/pdf.js/blob/master/web/viewer.js#L27.
  2. Replace: https://github.com/mozilla/pdf.js/blob/master/web/viewer.js#L1117-L1121, with: this.setScale(DEFAULT_SCALE, true);
jeum
  • 1,048
  • 3
  • 13
  • 29
  • Please edit the soloution into your answer. Link only answers tend to get deleted. – apaul Feb 19 '14 at 01:18
  • The lines the user is directed to in the links of the answer doesn't seem to relate to a solution for the problem anymore. Please update. – jagc Mar 31 '16 at 06:20
  • I had a look at the code ... the (obsolete) lines point the setInitialView() function (among line 1070 currently), but even the variables names have changed... It seems that you can declare a DEFAULT_SCALE_VALUE variable ? Have you tried that ? – jeum Apr 02 '16 at 15:30