Pdf.js: Setting defaultUrl has no effect

Created on 27 Jun 2018  路  12Comments  路  Source: mozilla/pdf.js

Steps to reproduce:

Download 2.0
https://github.com/mozilla/pdf.js/releases/download/2.0.550/pdfjs-2.0.550-dist.zip

Edit viewer.html:

<script>
AppOptions.set('defaultUrl', 'test.pdf');
</script>

Result:
ReferenceError: Can't find variable: AppOptions

Expected Result:
Load test.pdf

All 12 comments

Result:
ReferenceError: Can't find variable: AppOptions

That's hardly surprising, given that AppOptions isn't directly exposed in the global scope. However, as can be observed in the source code, it can be accessed using the PDFViewerApplicationOptions name.

If the above isn't enough to get you going, you'll need provide a complete (and runnable) example!


Slightly off-topic, but please keep in mind that this is first and foremost a bug tracker, and not a support forum!

However, people will usually do their best to answer support questions as well, provided that they are clear and concise (with all the required information included).
Furthermore, the people you're interacting with here aren't (usually) being paid, but help out in their spare time! If you're asking for help it's in your own interest to provide as much information as possible upfront, to make it as quick/easy as possible for someone to help you out (more on that below).

Also, please refrain from spamming the issue tracking with multiple duplicate issues, just because you're not (immediately) receiving a satisfactory answer to your question.

Before opening a new issue, please make sure that you've read https://github.com/mozilla/pdf.js/blob/master/.github/CONTRIBUTING.md. In this case, the following paragraph is of particular importance:

If you are developing a custom solution, first check the examples at https://github.com/mozilla/pdf.js#learning and search existing issues. If this does not help, please prepare a short well-documented example that demonstrates the problem and make it accessible online on your website, JS Bin, GitHub, etc. before opening a new issue or contacting us on the IRC channel -- keep in mind that just code snippets won't help us troubleshoot the problem.

Thus, with only code-snippets provided (or other crucial information missing) it becomes difficult/time-consuming for anyone to help, since the "bigger picture" is missing.
In these cases, any attempt at helping usually turns into a (time-consuming) game of Twenty Questions just to try and extract actionable information.

That's hardly surprising, given that AppOptions isn't directly exposed in the global scope. However, as can be observed in the source code, it can be accessed using the PDFViewerApplicationOptions name.

PDFViewerApplicationOptions.set('defaultUrl', 'test.pdf');

ReferenceError: Can't find variable: PDFViewerApplicationOptions

I already tried this here: https://github.com/mozilla/pdf.js/issues/9812#issuecomment-397469101

For the curious, this would be how to set DEFAULT_URL in the header of viewer.html:

<script>
$(document).ready(function() {
PDFViewerApplicationOptions.set('defaultUrl', '<?=$file;?>');
});
</script>

For the curious, this would be how to set DEFAULT_URL in the header of viewer.html:

<script>
$(document).ready(function() {
PDFViewerApplicationOptions.set('defaultUrl', '<?=$file;?>');
});
</script>

Is there any possibility to include a starting pdf from loading viewer.html?
I need to load automatically and it's placed on the same dir of viewer.html.
No way with method you described, nor with using the string ?=$file.pdf after viewer.html.
I'm in stuck.

you would replace "$file" with the name of your pdf. viewer.html?file=mypdf.pdf

No it's not working.
Here my direct input into crome
file:///C:/Users/fcapp/Desktop/PDF_JS/web/viewer.html?file=test.pdf

test is in the same folder of viewer.html

Other try is:



it's not working...sadly

The URL seems to work for me. Not sure why it would not be working. If you use the javascript method the correct code would be:

You can also dig around in viewer.js and set the default URL from there.

Which version are you using that works?

seems this trick can be integrate in documentation/wiki/faq to help people :+1:

humm this seems this work sometimes. I explain with example :

    <script>
        window.addEventListener('load', function() {
            console.log('there');
            PDFViewerApplicationOptions.set('defaultUrl', '/assets/test.pdf');
        });
    </script>

doesn't work at 100%, sometimes PDFViewer try to load default file compressed.tracemonkey-pldi-09.pdf .
My 2cts, viewer part is rely on dom load event. There is a way to set defaultUrl when all stuff is loaded ? and prevent try load compressed.tracemonkey-pldi-09.pdf ?

After reading through the Issue related to defaultUrl and I know how to solve the problem, it works well!

File viewer.js config

Delete string 'compressed.tracemonkey-pldi-09.pdf' in defaultUrl
We will be like this

defaultUrl: {
    value: '',
    kind: OptionKind.VIEWER
 }

File viewer.html

<script>
        window.addEventListener('load', function() {
            PDFViewerApplication.open('you_custom_file.pdf')
        });
 </script>

After reading through the Issue related to defaultUrl and I know how to solve the problem, it works well!

File viewer.js config

Delete string 'compressed.tracemonkey-pldi-09.pdf' in defaultUrl
We will be like this

defaultUrl: {
    value: '',
    kind: OptionKind.VIEWER
 }

File viewer.html

<script>
        window.addEventListener('load', function() {
            PDFViewerApplication.open('you_custom_file.pdf')
        });
 </script>

It's not working...can you please share all the code for viewer.html and viewer.js?
Thanks.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

PeterNerlich picture PeterNerlich  路  3Comments

SehyunPark picture SehyunPark  路  3Comments

BrennanDuffey picture BrennanDuffey  路  3Comments

brandonros picture brandonros  路  3Comments

smit-modi picture smit-modi  路  3Comments