max-mapper / elementary-electron

NodeSchool workshop for learning Electron
228 stars 44 forks source link

Works with this code, but still get fn undefined error #36

Open Julix91 opened 4 years ago

Julix91 commented 4 years ago

I had issues every step of the way. Needed to add node support to app.js and made changes to index.js too. I don't know why replacing the callback way of doing it with a promise way worked, but with the following code getting the cat pic, annotating and printing on 'p' press all work.

However, still getting error messages in the console, similar to https://github.com/maxogden/elementary-electron/issues/32

Screen Shot 2020-02-08 at 3 26 30 PM

Tracked them to here: https://github.com/visionmedia/superagent/issues/676 https://github.com/visionmedia/superagent/issues/714

but I couldn't figure out where it's being included from to test if upgrading the version fixes it or such.


app.js

var electron = require('electron');

electron.app.on('ready', function () {
    var mainWindow = new electron.BrowserWindow({
        width: 600,
        height: 800,
        webPreferences: {
            nodeIntegration: true
        }});
    mainWindow.loadURL('file://' + __dirname + '/index.html');
})

index.js


// Get cat pic src
var catpicture = require('cat-picture');
var src = catpicture.src;
catpicture.remove();

// Get annotation
var image = require('lightning-image-poly');
var viz = new image('#visualization', null, [src], {hullAlgorithm: 'convex'});

// Save pdf
var fs = require('fs');
var remote = require('electron').remote;

function save () {

    remote.getCurrentWebContents().printToPDF({portrait: true})
        .then( data => {
            fs.writeFile('annotation.pdf', data, function (err) {
                if (err) {
                    alert('error generating pdf! ' + err.message);
                } else {
                    alert('pdf saved!');
                }
            })
        })
        .catch (err => {
            console.log('test');
        })
}

window.addEventListener('keydown', function (e) {
    if (e.keyCode == 80) save()
})
nsmedira commented 4 years ago

Hi @Julix91 ,

Your code helped me get the save-to-pdf functionality working. How did you know to rewrite it in this way? From your post, it sounds like what you did was replace "the callback way of doing it with a promise way". Why did the callback way fail and the promise way succeed?

index.js

// FUNCTION TO SAVE CURRENT WINDOW TO PDF (PER elementary-electron)
// function save () {
//     alert('save function called')
//     remote.getCurrentWebContents().printToPDF({
//         portrait: true
//     }, function (err, data){
//         alert('callback function called')
//         fs.writeFile('annotation.pdf', data, function (err){
//             if (err) alert('error generating pdf! ' + err.message)
//             else alert('pdf saved!')
//         })
//     })
// }

// FUNCTION TO SAVE CURRENT WINDOW TO PDF (PER Julix91)
function save () {
    remote.getCurrentWebContents().printToPDF({portrait: true})
        .then( data => {
            fs.writeFile('annotation.pdf', data, function (err) {
                if (err) {
                    alert('error generating pdf! ' + err.message) ;
                } else {
                    alert('pdf saved!') ;
                }
            })
        })
        .catch ( err => {
            console.log('test');
        })
}
Julix91 commented 4 years ago

Don't know, it's been a while... sorry.

On Wed., Jun. 10, 2020, 05:24 Nick Smedira, notifications@github.com wrote:

Hi @Julix91 https://github.com/Julix91 ,

Your code helped me get the save-to-pdf functionality working. How did you know to rewrite it in this way? From your post, it sounds like what you did was replace "the callback way of doing it with a promise way". Why did the callback way fail and the promise way succeed?

index.js

// FUNCTION TO SAVE CURRENT WINDOW TO PDF (PER elementary-electron) // function save () { // alert('save function called') // remote.getCurrentWebContents().printToPDF({ // portrait: true // }, function (err, data){ // alert('callback function called') // fs.writeFile('annotation.pdf', data, function (err){ // if (err) alert('error generating pdf! ' + err.message) // else alert('pdf saved!') // }) // }) // }

// FUNCTION TO SAVE CURRENT WINDOW TO PDF (PER Julix91) function save () { remote.getCurrentWebContents().printToPDF({portrait: true}) .then( data => { fs.writeFile('annotation.pdf', data, function (err) { if (err) { alert('error generating pdf! ' + err.message) ; } else { alert('pdf saved!') ; } }) }) .catch ( err => { console.log('test'); }) }

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/maxogden/elementary-electron/issues/36#issuecomment-641968364, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADCH3Z36ZOQM7NPO22XTQELRV53PZANCNFSM4KR5X6AQ .