These are chat archives for jsreport/jsreport

15th
May 2018
Jan Blaha
@pofider
May 15 09:31
yes, probably use rest call directly or use lower cased props ResourceName -> resourceName. This is ugly bug, I will try to release v2 version at least of the client to fix this in the next days
Saran Ahluwalia
@ahlusar1989
May 15 19:05
Hello: This is my second day using jsreport. @pofider has been very helpful in guidance. I was wondering if anyone could provide with feedback on my reply earlier today? https://forum.jsreport.net/topic/533/how-to-iterate-over-assets-directory-in-jsreport/5
BJR Matos
@bjrmatos
May 15 19:15

hi! we usually wait some time before answering forum questions, that give us time to balance between our activities and community support.

after checking quickly what you shared it seems to me that you did not tried what @pofider suggested with jsreport-proxy inside a script, did you tried? if yes, what was the issue that you faced? did not work?. checking the code that you are sharing looks to me that you are trying to do much more for being your second day with jsreport, i mean, your code is using jsreport-core which is something we consider for advanced cases, the best for you, if you are just starting is to try main jsreport package (which comes with studio, etc).

Saran Ahluwalia
@ahlusar1989
May 15 19:20
Hello - thanks for the reply
I did try to do something similar but this failed.
There was an issue with installation of the jsreport-proxy, as decribed here: https://jsreport.net/learn/scripts#query-an-entity-from-script
I could not figure out the issue and then moved onto a working version
I am trying to iterate over the assets directory in the studio and render the images that end with .png
I thought this would be straightforward but I am not at all familiar with handlebars
BJR Matos
@bjrmatos
May 15 19:25

There was an issue with installation of the jsreport-proxy, as decribed here: https://jsreport.net/learn/scripts#query-an-entity-from-script

hmm wait, did you tried to install it? it is something that should work without installing anything inside an script (of course if you have jsreport v2), it is like a mock package that is built-in inside an script

Saran Ahluwalia
@ahlusar1989
May 15 19:25
Yes, I thought so as well
when I tried to add this to config (permissible modules) an exception was raised
BJR Matos
@bjrmatos
May 15 19:36
i just tried locally and it worked
using the jsreport-proxy
Saran Ahluwalia
@ahlusar1989
May 15 19:37
How did you so this?
Using my route and setup with express?
BJR Matos
@bjrmatos
May 15 19:38
of course my example is simple, i’m just getting the names and printing, but this is the base for future updates (like calling the asset, filtering to just png images)
Captura de pantalla 2018-05-15 a las 2.37.08 p.m..png

Using my route and setup with express?

no, using main jsreport

Captura de pantalla 2018-05-15 a las 2.38.29 p.m..png
this is the script:
const jsreport = require('jsreport-proxy')

async function beforeRender(req, res) {
  const assets = await jsreport.documentStore.collection('assets').find()
  req.data.assets = assets.map(a => a.name)
  console.log(req.data.assets)
}
as i said, using the jsreport-proxy in normal jsreport installation will work normally, maybe you are just getting problems when trying to use it with jsreport-core
Saran Ahluwalia
@ahlusar1989
May 15 19:41
Interesting @bjrmatos
I will see if I can replicate this locally
Thank you for your feedback
BJR Matos
@bjrmatos
May 15 19:42
good luck :ok_hand:
Saran Ahluwalia
@ahlusar1989
May 15 19:51
@bjrmatos I had a followup
    <head>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <style>
            {#asset Invoice styles.css @encoding=utf8}
        </style>     
    </head>
  <body>
    {{#if assets.length}}
      {{#each assets}}
            <td>
            <img src="{./this  @encoding=dataURI}" style="width:100%; max-width:300px;" />
            </td>
        asset name: {{./this}}
        </br>            
      {{/each}}
    {{else}}
      <h1> No map images </h1>
    {{/if}}
  </body>
</html>
this is my handlebars template
I am curious to know why the images are not being rendered for each of the assets that are indeed png images
Saran Ahluwalia
@ahlusar1989
May 15 19:57
I am not quite sure as to how to add a screenshot here
BJR Matos
@bjrmatos
May 15 21:02
you need to do something like this
{{#each assets}}
    asset name: {{./this}}
    <br/>
    <img src="{#asset {{./this}} @encoding=dataURI}" width="200px" />
    <br/>
{{/each}}
Captura de pantalla 2018-05-15 a las 4.02.56 p.m..png
the updated script that also makes sure to only show png images is this:
const jsreport = require('jsreport-proxy')

async function beforeRender(req, res) {
  const assets = await jsreport.documentStore.collection('assets').find()
  req.data.assets = assets.map(a => a.name).filter(a => /.png$/.test(a))
}

I am not quite sure as to how to add a screenshot here

just FYI, i think you can drag and drop images in the gitter chat windows and it will be uploaded automatically

Saran Ahluwalia
@ahlusar1989
May 15 21:47
Oh wow - that was way easier than what I was trying to do :smile:
Saran Ahluwalia
@ahlusar1989
May 15 21:53
@bjrmatos I tried resorting to manually encoding each image.....
async function beforeRender(req, res) {
    const assets = await jsreport.documentStore.collection('assets').find();
    let images = [];
    let tempObj;
    let uri;
    let mediaType = 'PNG';

    for(i = 0; i < assets.length; i++){
        tempObj = assets[i];
        if(tempObj.name.endsWith('png')){
             // RETURNS :: image data URI :: 'data:image/png;base64,PNGDATAURI/wD/'
            uri = imageDataURI.encode(tempObj.content, mediaType);
            images.push(uri);
        }
    }
    req.data.assets = images;
    // req.data.assets = assets.map(a => a.name)
    console.log(req.data.assets.map(uri => uri))
}
You saved me hours of banging my head sir
Thank you so much
BJR Matos
@bjrmatos
May 15 21:55
no problem :ok_hand: happy coding!