Window.open loading localhost PDF as Blank page - javascript

I am running a local Tomcat server within which I am hosting a PDF report. Once my Tomcat server is up and running, if I enter the following URL in my browser:
https://localhost:9000/Report.pdf
Then the page displays just fine. But within my javascript application if I call window.open(https://localhost:9000/Report.pdf), then a page will open with that URL, but it will display as blank. Refreshing or reloading the page won’t help, I need to close the whole tab and paste the URL in order to get it to load properly.
The server is being linked through the Symphony Messaging application, so https is a must. I'm not quite sure if that's what's causing the error or windows.open just doesn't work with a pdf file. I've tested it with other file types (ex. https://localhost:9000/logo.png) and it works just fine.
I've seen some similar questions about passing a byte array into window.open to display a PDF, but this seems kind of redundant: do I really have to convert the PDF to a byte array and then have window.open convert it back to PDF format just to display?
FYI both the HTML page and underlying Javascript from which I am attempting to call window.open are hosted on localhost:9000 as well.

Related

Why does this URL do different things depending on how it's activated?

In a related post (here) I discuss a problem I'm having with a URL action in a SQL Server Reporting Services report. I thought I had solved the problem, but I now realize that I haven't. (I'll update the related post in due course.)
I'm generating a URL using string concatenation, and using Javascript so the URL opens in a new browser tab. Here's the code that generates the URL, which I think is pretty standard:
="javascript:void(window.open('http://www.ic.gc.ca/app/opic-cipo/trdmrks/srch/viewTrademark?id="
+ Fields!applicationNoWithExtn.Value
+ "&lang=eng&tab=reg','_blank'))"
And here's an example of the URL that's generated when a value is inserted:
http://www.ic.gc.ca/app/opic-cipo/trdmrks/srch/viewTrademark?id=159511-00&lang=eng&tab=reg
The generated URL works. It takes you to an online database web page for a Canadian trademark having application number 159511-00 (you don't see the "-00" suffix on the web page, but it's the correct application number).
The problem is that when the user clicks the generated URL, the browser's print dialog opens immediately after the web page opens. That's very distracting; the user has to click Cancel to dismiss the print dialog. If the web page is reloaded or refreshed within the browser, the browser's print dialog opens again. However, if I copy the URL and paste it into a new browser tab, I get the desired web page without the print dialog. I don't get it; it's the identical URL, but the version of the URL generated by my application somehow triggers the print dialog, whereas a copy/pasted version of the identical URL doesn't. I've tried different browsers (Chrome, Edge and IE) and different machines, but I keep getting that print dialog when I click the URL generated by my application. If I change the code to generate a standard URL like http://google.com the generated standard URL works without triggering the print dialog. So the problem seems to be with the particular web site that I am accessing via the concatenated URLs shown above.
Can anyone explain what's going on and how I can suppress that print dialog?

ways to open large-sized pdf without downloading (using open on javascript)

Good day,
I have a system that renders large amount of data through pdf ( 30mb + ). Now I want the user to view pdf first so he can either download it or just print it right away. as of the moment I am forcing the user to download the file since open( 'datauri here' ) wont work with larger files.. the problem with downloading is that files are multiplying and consumes space over time and also its not necessary for me to download all files that that they want to print right away.
I need a functionality that is similar to chrome's preview when using windows.print
can you please suggest any ideas or other things to do this?
I am currently using javascript library to create pdf (pdfmake). I am also using chrome as my main browser
You would have to make sure that the PDF is optimized for fast web view, and that your server is using the byteserving protocol for serving the file.
If that is the case, a useful PDF viewer (such as the web browser component provided by Acrobat/Reader) understands this protocol and requests (after the first page plus overhead of the PDF) only the data for the pages which are to be displayed.
A quick search did, however, not reveal whether the Chrome PDF viewing component is smart enough to understand the byteserving protocol.

PhoneGap show downloaded image in iframe

I have a PhoneGap application in which I need to download certain images for offline usage and show those inside an iframe. Is this possible and do I need something like CorHTTPD (https://github.com/floatinghotpot/cordova-httpd) to serve the assets locally?
I have been trying to store the files on file system but when I try to show those (even without being inside iframe), those doesn't show. They seem to be loaded (can be seen in network console in remote debugging), though, but (of course) without any headers.
After spending more and more time on this and settings GapDebug correctly to remote debug my application, I was finally able to solve my problem by giving
{responseType: "arraybuffer"}
to AngularJS's $http.get method as config parameter as described here. Now I am able to get the images to ArrayBuffer correctly and from there to base64 encode them to be added inside HTML stored offline. Suitable solution for my case at least..

Cache external embed pdf with javascript or html

I have the following external pdf embedded as shown below. The pdf displays fine, but how do I cache the pdf so that the page doesn't redownload the pdf each time I visit the page? Should I use an object tag instead?
<embed src="http://samplepdf.com/sample.pdf#toolbar=0&navpanes=0&scrollbar=0" width="500" height="375">
From my understanding, you have a pdf on your server that you would like to cache onto the client side so that the pdf is not redownloaded each time the user refreshes the page.
By sending the proper headers, you can enforce cache rules (or at least try to enforce them as any browser can overrule your rules).
Php or in your case, Django, can send the appropriate headers to tell the browser to cache this pdf. My recommendation would be that you link that you provide in the embed tag links to a script instead of the pdf diriectly. This django script would sent out 2 sets of headers. One tells the browser to cache this content and other tells the browser it is sending a PDF file. This ensures that the pdf gets cached. When you load a webpage, the typical headers' scope by not encompass all external files/pdfs. As each is a separate request.
The method that I prescribed adds extra work than simply embedding the pdf but it should get the job done.

Precache file with Javascript in the browser?

How can I do this:
the page loads
javascript loads a remote PDF file into local memory
the user clicks a button/link
the system launches the PDF reader or starts a download dialog with the PDF file already in memory
In other words, it's a regular file download in the browser EXCEPT that the file has already been loading in the background in order to speed up its receipt when/if the user decides to download the file.
You would have to encode the file (perhaps via a servlet), then you could get it through an XHR, and write it into a data uri, which you could then attach to a button or link.
This technique would probably only work on small files and very recent browsers.
StackOverflow won't let me post an example link as a link, so to test the concept, you'll have to copy the following line into an html file and see if you can load the link:
pdf link
This worked perfectly in Chrome when I tested it just now, and worked partially in Firefox. It didn't work at all in my version of IE.
Another potential solution is to make absolutely sure that the pdf is being cached, and then try to load it in a hidden iframe. Whether this works or not will depend on how the user has their browser set up.
You should consider not doing it at all, given the difficulties.

Categories