Giter Club home page Giter Club logo

Comments (15)

CARodriguezG avatar CARodriguezG commented on June 18, 2024 1

@ram-you solution really works! But we have to make something before, because it seems it can't print until PDF gets render (and that has sense).

This is what I got:

// We define an interval to check until the PDF is ready to print
var interval = window.setInterval(function() {
    var pdf = pdfjsWebLibs.pdfjsWebApp.PDFViewerApplication;

    // Checks if PDF view is render and ready to continue, otherwise, it returns
    if(!pdf.pdfViewer.pageViewsReady) return;

    // Clears this interval to avoid to keep checking and running
    clearInterval(interval);

    // Finally, executes the click function on the print button
    document.getElementById("print").click();
}, 500); // Defines at how much ms it will check

And about @wujingshi question:

How do I close the hidden window?
Too many hidden windows can cause CPU to use too much.
I mean, how to monitor the print end before closing the window.

I found a good post here How to detect window.print() finish.
So using the same method as before (by executing window.webContents.executeJavaScript(code);) we have something like this:

(function() { 
    var beforePrint = function() { 
        // Do stuff before print
    }; 
    
    var afterPrint = function() { 
        // Do stuff after print
        // This will close the window after 500ms because just closing it will close it immediately and the print will not work, you can modify the value if needed
        setTimeout(function() { window.close(); }, 500);
    }; 
             
    if (window.matchMedia) { 
        var mediaQueryList = window.matchMedia("print"); 
        mediaQueryList.addListener(function(mql) { 
            if (mql.matches) { 
                beforePrint(); 
            } else { 
                afterPrint(); 
            } 
        }); 
    } 
             
    window.onbeforeprint = beforePrint; 
    window.onafterprint = afterPrint; 
}());

This is the way I used it on my Electron application and until now is working nice.

Finally it would be nice if this module could improve and handle those events for us. Greets.

from electron-pdf-window.

ram-you avatar ram-you commented on June 18, 2024

@patrickrobbins
Hi try my solution, it works

let code ="var button = document.getElementById('print'); button.click()";

win.webContents.executeJavaScript(code);

from electron-pdf-window.

wujingshi avatar wujingshi commented on June 18, 2024

How do I close the hidden window?
Too many hidden windows can cause CPU to use too much.
I mean, how to monitor the print end before closing the window.

from electron-pdf-window.

guilhermebruzzi avatar guilhermebruzzi commented on June 18, 2024

If you are interested on print direct and don't even show the window, this did the trick for me:

const PDFWindow = require('electron-pdf-window')

const win = new BrowserWindow({ show: false })
PDFWindow.addSupport(win)
win.loadURL('file://PATH_TO_FILE.pdf')

win.webContents.on('did-finish-load', () => {
    setTimeout(() => {
        win.webContents.print(options || {}, (success) => {
            if (success) {
              console.log('Finished printing with success')
            } else {
              console.error('Finished printing with error')
            }
            win.close()
        })
    }, 2000) // A time to load and render PDF
})

from electron-pdf-window.

mralbertchen avatar mralbertchen commented on June 18, 2024

If you are interested on print direct and don't even show the window, this did the trick for me:

const PDFWindow = require('electron-pdf-window')

const win = new BrowserWindow({ show: false })
PDFWindow.addSupport(win)
win.loadURL('file://PATH_TO_FILE.pdf')

win.webContents.on('did-finish-load', () => {
    setTimeout(() => {
        win.webContents.print(options || {}, (success) => {
            if (success) {
              console.log('Finished printing with success')
            } else {
              console.error('Finished printing with error')
            }
            win.close()
        })
    }, 2000) // A time to load and render PDF
})

This prints a blank page for me. It seems like webContents is blank when you use PDF? How did you get it to work?

from electron-pdf-window.

wujingshi avatar wujingshi commented on June 18, 2024

Sorry, this plug-in is not perfect. I've abandoned it.
I've developed another better way to print PDF silently.
If you're willing to wait, I'll write a new plug-in this Saturday.

from electron-pdf-window.

mralbertchen avatar mralbertchen commented on June 18, 2024

Sorry, this plug-in is not perfect. I've abandoned it.
I've developed another better way to print PDF silently.
If you're willing to wait, I'll write a new plug-in this Saturday.

Sure I'll wait. Let me know when you do.

from electron-pdf-window.

arslanmughal99 avatar arslanmughal99 commented on June 18, 2024

@wujingshi any updates ? Please provide link for your plugin , Thanks !

from electron-pdf-window.

wujingshi avatar wujingshi commented on June 18, 2024

@arslanmughal99
I'm sorry to hear from you so long
I have finished writing the plug-in, but there is an error when packing, so I am still solving this problem.

from electron-pdf-window.

arslanmughal99 avatar arslanmughal99 commented on June 18, 2024

@wujingshi if you can push repo on github maybe i can help fixing . Also are you facing error on macOs ?

from electron-pdf-window.

arslanmughal99 avatar arslanmughal99 commented on June 18, 2024

By the way i fix me issue using Sumatra PDF .
By calling using child_process.exec.
SumatraPDF.exe label.pdf -print-dialog -silent -print-settings "fit,portrait" -exit-when-done

from electron-pdf-window.

wujingshi avatar wujingshi commented on June 18, 2024

@arslanmughal99
I used this to solve problems.
But there is a version limit, the latest version of Sumatra PDF. The data will be enlarged during transmission. For example, 100kb PDF will become 100MB data and transmitted to the printer.
But use Sumatra PDF version 2.2.0. It can solve this problem, but it is not as powerful as the latest version.
However, it is impossible to package when packing.

from electron-pdf-window.

arslanmughal99 avatar arslanmughal99 commented on June 18, 2024

I didn't encounter the first issue you have mentioned , but as for packaging issue it is totally possible simple add binary in your resources folder . electron-builder will package it with app.

from electron-pdf-window.

wujingshi avatar wujingshi commented on June 18, 2024

@arslanmughal99
What version of Sumatra PDF do you use?

from electron-pdf-window.

arslanmughal99 avatar arslanmughal99 commented on June 18, 2024

3.2 64bit-build

from electron-pdf-window.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.