Storing PDF in S3 Bucket



  • Hi I have installed the jsreport-aws-s3-storage extension but I can't seem to get it working, i'm not sure if I have done it correctly. There doesn't seem to be much documentation around explaining exactly what to do.

    I have added the extension to my config and also changed my blob storage -

    {
      "blobStorage": {
        "provider": "aws-s3-storage"
      },
      "extensions": {
        "authentication": {
          "cookieSession": {
            "secret": "<your strong secret here>"
          },
          "admin": {
            "username": "admin",
            "password": "password"
          },
          "enabled": true
        },
        "scripts": {
          "timeout": 40000,
          "strategy": "http-server",
          "allowedModules": ["request", "nodemailer", "nodemailer-ses-transport", "jsreport-proxy"]
        },
        "sample-template": {
          "createSamples": true
        },
        "aws-s3-storage": {
          "accessKeyId": "key",
          "secretAccessKey": "secret_key",
          "bucket": "bucket_name"
        }
      },
      "httpPort": 3000,
      "store": {
        "provider": "fs"
      },
      "logger": {
        "console": {
          "transport": "console",
          "level": "debug"
        },
        "file": {
          "transport": "file",
          "level": "info",
          "filename": "logs/reporter.log"
        },
        "error": {
          "transport": "file",
          "level": "error",
          "filename": "logs/error.log"
        }
      },
      "allowLocalFilesAccess": true,
      "templatingEngines": {
        "timeout": 10000,
        "strategy": "http-server"
      },
      "chrome": {
        "timeout": 40000
      }
    }
    

    I have added reports: { save: true } into my script, my script also emails the user the PDF reports so sorry some of the code doesnt have anything to do with this.

    const jsreport = require('jsreport-proxy')
    
    async function beforeRender(req, res) {
        if (req.context.isChildRequest) {
          return
        }
        
        var venues = [
            {
                'name': 'Hinckley BID',
                'data': {
                    'total_visits': 237967,
                    'percentage_increase': -26.1,
                    'percentage_increase_bool': false,
                    'average_per_day': 12546,
                }
            }
        ];
    
        var nodemailer = require("nodemailer");
        var sesTransport = require('nodemailer-ses-transport');
    
        var transporter = nodemailer.createTransport(sesTransport({
            accessKeyId: "key",
            secretAccessKey: "secret_key",
            region: 'eu-west-1',
            rateLimit: 5
        }));
    
        for (let venue of venues) {
            const result = await jsreport.render({ 
                template: { 
                    shortid: 'H1xtsYmyWI'
                },
                options: {
                    reports: { 
                        save: true 
                    }
                },
                data: venue
            });
            
            await sendEmail(transporter, result);
        }
    }
    
    async function sendEmail(transporter, result) {
        var email_address = [email1];
    
        email_address.forEach(function(email) {
            var mail = {
                from: "email2",
                to: email,
                subject: "Sending email from node.js",
                text: "See the attached report",
                html: "<b>See the attached report</b>",
                attachments: [
                {  
                    filename: 'Report.pdf',
                    content: result.content
                }],
            }
    
            transporter.sendMail(mail, function(error, response){
                transporter.close();
                if(error){
                    return done(error);
                }
            });
        });
    }
    

    I may be missing something but from what I have read it seems you just need to add the save option to the template render and it should work. No errors are being thrown when I debug but let me know if you would like me to put some error logs somewhere.

    Thank for any help!



  • The reports extension currently skips requests created inside another request.
    We need to think this through before we would change this.
    It means there is no way for you to render an extra template in the custom script which will be automatically stored using reports extension.

    However, what you can do, is require and aws s3 sdk inside your script and store the render result on your own.



  • Ok thank you I will have a go at doing that.


Log in to reply
 

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