How to open PDF from web using JavaScript?

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

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

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 »

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: 2349
Joined: Wed Jan 18, 2006 12:10 pm

Re: How to open PDF from web using JavaScript?

Post by Willy Van Nuffel »

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: 8440
Joined: Wed Jan 03, 2018 6:52 pm

Re: How to open PDF from web using JavaScript?

Post by TrackerSupp-Daniel »

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,
Dan McIntyre - Support Technician
Tracker Software Products (Canada) LTD

+++++++++++++++++++++++++++++++++++
Our Web site domain and email address has changed as of 26/10/2023.
https://www.pdf-xchange.com
Support@pdf-xchange.com
PiXiE
User
Posts: 7
Joined: Thu Jun 10, 2021 4:47 am

Re: How to open PDF from web using JavaScript?

Post by PiXiE »

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: 461
Joined: Tue Sep 05, 2017 4:56 pm

Re: How to open PDF from web using JavaScript?

Post by Ovg »

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, 10.1.1 (Build 381) / W7 SP1 x64
Willy Van Nuffel
User
Posts: 2349
Joined: Wed Jan 18, 2006 12:10 pm

Re: How to open PDF from web using JavaScript?

Post by Willy Van Nuffel »

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: 461
Joined: Tue Sep 05, 2017 4:56 pm

Re: How to open PDF from web using JavaScript?

Post by Ovg »

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, 10.1.1 (Build 381) / W7 SP1 x64
Willy Van Nuffel
User
Posts: 2349
Joined: Wed Jan 18, 2006 12:10 pm

Re: How to open PDF from web using JavaScript?

Post by Willy Van Nuffel »

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: 461
Joined: Tue Sep 05, 2017 4:56 pm

Re: How to open PDF from web using JavaScript?

Post by Ovg »

I think it's related to https://forum.pdf-xchange.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, 10.1.1 (Build 381) / W7 SP1 x64
Willy Van Nuffel
User
Posts: 2349
Joined: Wed Jan 18, 2006 12:10 pm

Re: How to open PDF from web using JavaScript?

Post by Willy Van Nuffel »

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: 461
Joined: Tue Sep 05, 2017 4:56 pm

Re: How to open PDF from web using JavaScript?

Post by Ovg »

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, 10.1.1 (Build 381) / W7 SP1 x64
Willy Van Nuffel
User
Posts: 2349
Joined: Wed Jan 18, 2006 12:10 pm

Re: How to open PDF from web using JavaScript?

Post by Willy Van Nuffel »

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 »

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 »

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 »

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 »

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");
User avatar
Ivan - Tracker Software
Site Admin
Posts: 3549
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 »

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: 461
Joined: Tue Sep 05, 2017 4:56 pm

Re: How to open PDF from web using JavaScript?

Post by Ovg »

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, 10.1.1 (Build 381) / W7 SP1 x64
PiXiE
User
Posts: 7
Joined: Thu Jun 10, 2021 4:47 am

Re: How to open PDF from web using JavaScript?

Post by PiXiE »

Cool, looking forward to 355.

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

How to open PDF from web using JavaScript?

Post by Tracker Supp-Stefan »

:)
gonevid
User
Posts: 1
Joined: Tue Aug 10, 2021 5:58 am

Re: How to open PDF from web using JavaScript?

Post by gonevid »

js to your application, you'll need to execute the following steps: Download the code. Add the JavaScript code to instantiate the PDF. js library and pull the files.
Render PDFs on the browser using JavaScript
Should work cross-browser.
Should support modern(ish) platforms.
It shouldn't rely on a PDF add-in to be installed. https://vidmate-apk.in/home/ https://instasave.onl/
Last edited by gonevid on Wed Aug 25, 2021 7:34 am, edited 1 time in total.
User avatar
Ovg
User
Posts: 461
Joined: Tue Sep 05, 2017 4:56 pm

Re: How to open PDF from web using JavaScript?

Post by Ovg »

Working fine in 9.1.356 :thumbsup:
It's impossible to lead us astray for we don't care even to choose the way.
PDF-XChange PRO, 10.1.1 (Build 381) / W7 SP1 x64
User avatar
Tracker Supp-Stefan
Site Admin
Posts: 17824
Joined: Mon Jan 12, 2009 8:07 am
Location: London
Contact:

How to open PDF from web using JavaScript?

Post by Tracker Supp-Stefan »

:)
Post Reply