How to open PDF from web using JavaScript?

Forum for the PDF-XChange Editor - Free and Licensed Versions

Moderators: TrackerSupp-Daniel, Tracker Support, Chris - Tracker Supp, Vasyl-Tracker Dev Team, Sean - Tracker, Paul - Tracker Supp, Tracker Supp-Stefan, Ivan - Tracker Software

Post Reply
PiXiE
User
Posts: 7
Joined: Thu Jun 10, 2021 4:47 am

How to open PDF from web using JavaScript?

Post by PiXiE » Thu Jun 10, 2021 4:52 am

Hi,

I've been playing with the JavaScript console, and love it. I've been successfully able to manipulate pages, deleting, inserting and duplicating pages as needed – powerful stuff.

I'm banging my head against the wall trying to do something super basic, though: how to open a PDF file in PXE from the web using JavaScript?

I can open a local PDF without problem, using app.openDoc. I can open a web PDF using app.launchURL, but that opens in the default browser, not PXE. I figure I ought to be able to open a web PDF using Doc.getURL, which should append the pages from the web PDF file to the currently open PDF in PXE, but the following doesn't seem to do anything:

this.getURL("https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf");

When run from the console it returns "undefined", which typically indicates success. All my experimentation has been via the console, so I don't think I'm running into security/privilege issues.

So – how does one open a PDF in PXE from the web, using JavaScript?

Thanks!

-jim.

Willy Van Nuffel
User
Posts: 1706
Joined: Wed Jan 18, 2006 12:10 pm

Re: How to open PDF from web using JavaScript?

Post by Willy Van Nuffel » Thu Jun 10, 2021 12:18 pm

Just out of curiosity I have run some tests.

The following syntax seems to work:

app.openDoc("https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf");

but it takes a while before the document opens (+/- 25 seconds). No idea why.

User avatar
TrackerSupp-Daniel
Site Admin
Posts: 4669
Joined: Wed Jan 03, 2018 6:52 pm

Re: How to open PDF from web using JavaScript?

Post by TrackerSupp-Daniel » Thu Jun 10, 2021 9:21 pm

Hi, Willy Van Nuffel

Thanks for providing the solution for PiXiE here, I tested what you reported about the delay, and could not reproduce it. In my tests, using that JS, it took under 3 seconds to open the file here.

Kind regards,
Daniel McIntyre
Support Technician
Tracker Software Products (Canada) LTD

Support: <Support@tracker-software.com>
Sales: +1 (250) 324-1621
Fax: +1 (250) 324-1623

PiXiE
User
Posts: 7
Joined: Thu Jun 10, 2021 4:47 am

Re: How to open PDF from web using JavaScript?

Post by PiXiE » Thu Jun 10, 2021 9:57 pm

And works for me, too! Delay is only a few seconds.

For some reason I thought that openDoc was for local file system only – the API reference says the cPath parameter "target document must be accessible in the default file system", which I read as local. That along with getURL saying "This method roughly corresponds to the “open a web page” action" made me think one was for local file system, the other for web docs…

Regardless, I'm thrilled to have an answer. I knew there must be a simple solution! Thanks for finding it for me, Willy.

BTW, really impressed with this forum – active users helping each other out, and a lot of Tracker participation. Really great.

-jim.

User avatar
Ovg
User
Posts: 360
Joined: Tue Sep 05, 2017 4:56 pm
Location: Moscow

Re: How to open PDF from web using JavaScript?

Post by Ovg » Fri Jun 11, 2021 4:12 am

Hi All!

I get error message:

Code: Select all

Console:Exec:1: NotAllowedError: NotAllowedError: Security settings prevent access to this property or method.
App.openDoc:1:Console:Exec
Am I missing something?
It's impossible to lead us astray for we don't care even to choose the way.
PDF-XChange PRO, 9.0 (Build 354.0) / W7 x64 SP1

Willy Van Nuffel
User
Posts: 1706
Joined: Wed Jan 18, 2006 12:10 pm

Re: How to open PDF from web using JavaScript?

Post by Willy Van Nuffel » Fri Jun 11, 2021 6:28 am

1) Strange, but today (after a restart of my PC) the PDF from the above post opens immediately.

2) @ovg
The error message you have, seems to be bound to the following Security Option in PDF-XChange Editor:
"When document is trying to open a site"
NOTE: Even after "Apply" and "OK", PDF-XChange Editor needs to be restarted to make the changes to work.

Best regards.

Willy
Attachments
PDF-XChange Editor - Security Options.png

User avatar
Ovg
User
Posts: 360
Joined: Tue Sep 05, 2017 4:56 pm
Location: Moscow

Re: How to open PDF from web using JavaScript?

Post by Ovg » Fri Jun 11, 2021 6:48 am

2Willy Van Nuffel

Thanks, but it seems that in V 9.354.0 this doesn't work at all .... At least for me....

20210611_094537.png
20210611_095044.png
It's impossible to lead us astray for we don't care even to choose the way.
PDF-XChange PRO, 9.0 (Build 354.0) / W7 x64 SP1

Willy Van Nuffel
User
Posts: 1706
Joined: Wed Jan 18, 2006 12:10 pm

Re: How to open PDF from web using JavaScript?

Post by Willy Van Nuffel » Fri Jun 11, 2021 7:18 am

Thanks for the reply.

So, I think we will have to ask for more information @Tracker Software Support.

Kind regards.

Willy.

User avatar
Ovg
User
Posts: 360
Joined: Tue Sep 05, 2017 4:56 pm
Location: Moscow

Re: How to open PDF from web using JavaScript?

Post by Ovg » Fri Jun 11, 2021 8:15 am

I think it's related to https://forum.tracker-software.com/viewtopic.php?f=62&t=36502&p=151055&hilit=print+all#p150625
It's impossible to lead us astray for we don't care even to choose the way.
PDF-XChange PRO, 9.0 (Build 354.0) / W7 x64 SP1

Willy Van Nuffel
User
Posts: 1706
Joined: Wed Jan 18, 2006 12:10 pm

Re: How to open PDF from web using JavaScript?

Post by Willy Van Nuffel » Fri Jun 11, 2021 9:28 am

Thanks for the information @Ovg, but I am also using build 354 and at my side it works with the given security option.

Like stated in the other topic, the JavaScript handling should still work from within the JavaScript Console.

I suppose the Mozilla JavaScript engine is incorporated in PDF-XChange Editor itself, so there is no external influence ?

User avatar
Ovg
User
Posts: 360
Joined: Tue Sep 05, 2017 4:56 pm
Location: Moscow

Re: How to open PDF from web using JavaScript?

Post by Ovg » Fri Jun 11, 2021 11:44 am

May be OS/Bitness? What is your OS?
It's impossible to lead us astray for we don't care even to choose the way.
PDF-XChange PRO, 9.0 (Build 354.0) / W7 x64 SP1

Willy Van Nuffel
User
Posts: 1706
Joined: Wed Jan 18, 2006 12:10 pm

Re: How to open PDF from web using JavaScript?

Post by Willy Van Nuffel » Fri Jun 11, 2021 1:26 pm

Windows 10 Home edition, version 20H2 - 32-bit.

PiXiE
User
Posts: 7
Joined: Thu Jun 10, 2021 4:47 am

Re: How to open PDF from web using JavaScript?

Post by PiXiE » Fri Jun 11, 2021 7:29 pm

Working for me with Windows 10 Enterprise 21H1 64-bit with PXE Plus v9.0.354.0.

PiXiE
User
Posts: 7
Joined: Thu Jun 10, 2021 4:47 am

Re: How to open PDF from web using JavaScript?

Post by PiXiE » Fri Jun 11, 2021 9:23 pm

Aha! Works for me, so long as no other PDFs are open in PXE. Does not work if other files are open first (except for dummy.pdf) – I get the same error as Ovg.

That explains why I passed over app.openDoc when I was searching for a way to open web PDFs – I must have tested it with other files open, then written it off when I got the error message.

PiXiE
User
Posts: 7
Joined: Thu Jun 10, 2021 4:47 am

Re: How to open PDF from web using JavaScript?

Post by PiXiE » Fri Jun 11, 2021 9:31 pm

Hmm, only affected by previously opened files that were opened manually, e.g. via the PXE UI, or double clicking in Windows Explorer. Files that were opened programmatically (i.e. by previous app.openDoc calls) do not prevent future such calls. Weird.

PiXiE
User
Posts: 7
Joined: Thu Jun 10, 2021 4:47 am

Re: How to open PDF from web using JavaScript?

Post by PiXiE » Fri Jun 11, 2021 9:47 pm

If you have multiple files open in PXE, some which were opened manually and some programmatically, then app.openDoc will work so long as a programmatically-opened file is selected first. It fails if a manually-opened file has focus within PXE at time JS is run.

So:
  • No open files in PXE: app.openDoc works;
  • Open files in PXE, and programmatically-opened file tab is selected: app.openDoc works;
  • Open files in PXE, and manually-opened file tab is selected: app.openDoc fails.
JavaScript being run in the console each time is:

Code: Select all

app.openDoc("https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf");

Ivan - Tracker Software
Site Admin
Posts: 3645
Joined: Thu Jul 08, 2004 10:36 pm
Location: Vancouver Island - Canada
Contact:

Re: How to open PDF from web using JavaScript?

Post by Ivan - Tracker Software » Sat Jun 12, 2021 3:14 am

confirm, will be fixed in the next build.
The issue is caused not by documents opened programmatically, but by documents opened from URL.
Tracker Software (Project Director)

When attaching files to any message - please ensure they are archived and posted as a .ZIP, .RAR or .7z format - or they will not be posted - thanks.

User avatar
Ovg
User
Posts: 360
Joined: Tue Sep 05, 2017 4:56 pm
Location: Moscow

Re: How to open PDF from web using JavaScript?

Post by Ovg » Sat Jun 12, 2021 5:22 am

Yes, for local files working fine!
It's impossible to lead us astray for we don't care even to choose the way.
PDF-XChange PRO, 9.0 (Build 354.0) / W7 x64 SP1

PiXiE
User
Posts: 7
Joined: Thu Jun 10, 2021 4:47 am

Re: How to open PDF from web using JavaScript?

Post by PiXiE » Sat Jun 12, 2021 5:58 am

Cool, looking forward to 355.

-jim.

User avatar
Tracker Supp-Stefan
Site Admin
Posts: 14582
Joined: Mon Jan 12, 2009 8:07 am
Location: London
Contact:

How to open PDF from web using JavaScript?

Post by Tracker Supp-Stefan » Mon Jun 14, 2021 10:33 am

:)

Post Reply