Download pdf or xlsx response so that I can email it.



  • Quick background. I'm using jsreportonline. I'm creating a bunch of custom templates on the fly based off various user data. I'm doing all of this in JavaScript as the whole program is client-side. So, all of the solutions that use NodeJS are no good. I am currently viewing the pdf in a new window where the user can download it. Now, if the user wants to email the file I think my best bet is to download the file temporarily then attach that file and email it over sendgrid like I do normally. But I can't find a way to save the file to a specific path instead of downloading it to the computer in the browser.

    This is my current code to view the pdf. Any direction on where to go from here on downloading the pdf so I can email it.

    var request = {
        template: {
            content: header + contentString,
            name: "invoice-main",
            engine: engine,
            recipe: recipe,
            helpers: helpers,
            chrome: {
                landscape: isLand,
                marginTop: '20px',
                marginRight: '20px',
                marginBottom: '20px',
                marginLeft: '20px'
            },
            reports: {
                save: true
            }
        },
        data: aa
    };
    jsreport.renderAsync(request).then(function(res) {
        console.log( "in async...." );
        var html = '<html>' +
            '<style>html,body {padding:0;margin:0;} iframe {width:100%;height:100%;border:0}</style>' +
            '<body>' +
            '<iframe type="application/pdf" src="' +  res.toDataURI() + '"></iframe>' +
            '</body></html>';
    
        var a = window.open( "" , "", "wReport', top=2, left=2, " +
            "toolbar=no, menubar=no, location=1, height=900, " +
            "width=960, scrollbars=yes" );
    
        a.document.write(html);
        a.document.title = title;
        a.onload = function()
        {
            parent.close();
        }
        a.document.close();
    })

  • administrators

    hi! hmm i think saving the file to specific path using just client-side javascript is not possible, maybe the answer to this problem is in some sendgrid docs, how do you communicate with sendgrid from your client-side app? do they have some kind of browser sdk? (i can't find it) if yes, please share a link to their docs, if they support some kind of browser sdk i guess there must be some demos about sending attachments and with that, i can give you an idea how to extract the report file from the jsreport response to be included in the sendgrid http call.



  • I figured it out.

    var file = res.toDataURI();

    That gives me the base64 encoded pdf in a string. So, once I put that in the variable I just did an ajax call in javascript where I then handle the rest in .php.

    $f = str_replace('data:application/pdf;base64,', '', $f);
    file_put_contents( EMAIL_ATTACHMENTS .$file, $f);
    $fp = fopen(EMAIL_ATTACHMENTS . $file, 'wb');
    fwrite($fp, base64_decode($f));
    fclose($fp);

    That saves the pdf to the attachments file where I temporarily store the file to attach to emails using sendgrid.



  • This post is deleted!

Log in to reply
 

Looks like your connection to jsreport forum was lost, please wait while we try to reconnect.