• If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Announcement

Collapse
No announcement yet.

Directly Print a PDF from i5 without PDF client/browser extension open first

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Directly Print a PDF from i5 without PDF client/browser extension open first

    Hi Guys,

    I'm super close here but can't figure out how to call print using my hidden iframe instead of the trying to print the extjs app tab itself. window.print() does not take parameters, is there another technique I'm missing to get that iframe to the browser print function directly (last screen shot below)?

    The ajax call is returning the url to PDF stored on server. I'm successfully getting the pdf into the iframe. But can't figure out the print piece for the iframe.

    If I use this code with window.open(data.PDFLINK), it opens in pdf client perfectly. But window.print has no parameter to point at pdf/iframe.

    Thanks!

    image.png
    image.png??

  • #2
    Give this a try:
    Code:
    var iframe = Ext.DomHelper.append(document.body, {
        tag: 'iframe',
        cls: 'x-hidden',
        name: 'printIframe',
        src: data.PDFLINK
    });
    
    var iframeEl = Ext.get(iframe);
    
    // Add a 'load' event listener to ensure the PDF is fully loaded before printing
    iframeEl.on('load', function() {
        try {
            // Access the iframe's window object
            var iframeWindow = iframe.contentWindow || iframe;
    
            // Focus on the iframe's window (optional but can help with some browsers)
            iframeWindow.focus();
    
            // Trigger the print dialog
            iframeWindow.print();
        } catch (e) {
            console.error('Error printing iframe:', e);
        } finally {
            // Remove the iframe after printing to clean up the DOM
            Ext.fly(iframe).destroy();
        }
    });

    Comment


    • #3
      Johnny, thanks so much. I never would have figured that out. I think this is going to work but I have to change a HTTP config option which I can't do right this minute because have to restart the server.

      I believe I need to un-comment out the "Header always append x-frame-options SAMEORIGIN" to make this work. At least based on error in browser debug. Chrome browser.

      image.png?

      image.png?

      Comment


      • #4
        Johnny, that configuration option did not prevent that error. Do you have any insight how I prevent browser from seeing this as cross origin? You can turn off CORS when starting Chrome browser but that not feasible solution for every user/client.

        I suspect it thinks it is cross origin, even though from same i5 Valence runs on, because I use another HTTP server listening on port 80 which actually provides the access to the PDF doc. Is easiest solution just to try and get the Valence http server (on port 7060) to allow this access to that location? That would not be difficult, the old port 80 server is legacy back before we used Valence. I served PDF ISO docs with that server from free MS Sharepoint back in mid 2000s. That is long gone now.

        Comment


        • #5
          Ugh. Just changing to same HTTP server/port is not enough. It must be complaining it is not inside the "path" of the Extjs app? Where would I have to put that PDF for it to be seen as same origin? My apps run from that VALDEV folder, the PDF is currently in the FA folder. Is as simple as some location inside of that VALDEV shared folder?

          image.png

          image.png



          image.png???

          Comment


          • #6
            What is the URL for the Portal and what is the value of data.PDFLINK?

            Comment


            • #7
              Hi Johnny, I thought you may have been onto something here. I was using the IP address to access Valence portal (10.29.231.57:7060) to run app but using the DNS name to send PDF link (atnsr520). From the security error in browser, the port is definitely part of what CORS evaluates. So just changing to PDF link to IP address does not work because of using port 80 and different HTTP server to provide access. It still sees that as cross origin.

              I've attempted to allow the Valence HTTP server on port 7060 to allow access to that PDF location but must not have done something right, getting a 404. Troubleshooting that now.

              image.png?

              Comment


              • #8
                OK, I found and fixed the problem allowing access to that PDF location from the Valence HTTP server (port 7060). And the cross site error went away!

                Unfortunately though, it still doesn't work. Absolutely no errors, but the print dialog never comes up. I added that console.log right before your print() command to verify it was getting there.

                Note the size of the PDF shows 0 KB but the PDF displays just fine if I click on LAB_333132.pdf.

                Johnny, I don't want to waste a lot of your time here on something clearly not a Valence issue. Am I chasing something here the browsers really don't want to happen? I get the security exposure of xframes and cross site. The browser doesn't know this is internal LAN based site. But you have any idea to try at this point, just let me know. Thanks for getting me this far, learned a lot here!


                image.png?

                Comment

                Working...
                X