0

I am know developping a webapp that as to create a pdf document and print it on client side. Here is my problem:

i created the pdf using itext and stored it in a shared folder. i did it on a server side. Now i need to print the created pdf on the client side, the client knows the path of the pdf and can access it. To be on client side, i am trying to print that document using javascript or jquery.

I tried using embed in my html but it didn't work.

Thx for helping,

here is a working code on server side :

FileInputStream fis = new FileInputStream("test.pdf");
    DocFlavor psInFormat = DocFlavor.INPUT_STREAM.AUTOSENSE;
Doc pdfDoc = new SimpleDoc(fis, psInFormat, null);
    PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();  
    PrintService services = PrintServiceLookup.lookupDefaultPrintService(); 
    DocPrintJob job = services.createPrintJob(); 
    job.print(pdfDoc, aset); 

and here is what i tried on client side :

                                   // Grabs the Iframe  
                                    document.write('<embed type="application/pdf" src="\\SN782629\TempPartage\test.pdf" id="PDF" name="PDF" width="100%" height="100%" />');
                                    var ifr = document.getElementById("PDF");
                                    //PDF is completely loaded. (.load() wasn't working properly with PDFs)
                                    ifr.onreadystatechange = function() {
                                        if (ifr.readyState == 'complete') {
                                            ifr.contentWindow.focus();
                                            if ($.browser.msie) {
                                                document.execCommand('print', false, null);
                                            } else {
                                                window.print();
                                            }
                                        }
                                ifr.parentNode.removeChild(ifr);

this second code is on the success section of ajax request, i can put the entire function if needed.

barthelemy
  • 29
  • 6

1 Answers1

0

fyi: Recommended way to embed PDF in HTML?

and another point: you'll meet a lot of restrictions in different browsers when including pdf from local file system into a page loaded through HTTP.

i'll advice to expose your pdf through url on your server. for example http://myhost/getPdf/SN782629/TempPartage/test.pdf instead of ""\SN782629\TempPartage\test.pdf" and use this link in rendered page.

daggett
  • 26,404
  • 3
  • 40
  • 56
  • Thx for you answer, i am using this way to store my pdf only for developpement but it will be on my server at final . i will take a look at your link thx – barthelemy Jun 12 '17 at 14:24