jsreport / jsreport-core

The minimalist jsreport rendering core
GNU Lesser General Public License v3.0
85 stars 24 forks source link

Auto-discovery extensions with jsreport-core #55

Closed PierreFichet closed 3 years ago

PierreFichet commented 3 years ago

Hi,

I am testing jsreport-core in order to integrate it to an larger app. I installed a few extensions, among them jsreport-fs-store. When I launch it, the output is the following:

info: Initializing jsreport@2.10.1 in development mode
info: Searching for available extensions in /home/pierre/Documentos/Proyectos/tip/jsreportServer/jsreportStandalone/
info: Extensions location cache contains up to date information, skipping crawling in /home/pierre/Documentos/Proyectos/tip/jsreportServer/jsreportStandalone/
info: Found 4 extensions
info: Setting dedicated-process (process based) strategy for rendering. Please visit http://jsreport.net/learn/configuration for information how to get more performance.
info: Using extension handlebars@2.1.0
info: Using extension templates@2.4.0
info: Using extension chrome-pdf@1.10.0
info: Using extension fs-store@2.9.2
info: Using memory provider for template store. The saved templates will be lost after restart
info: reporter initialized
info: Starting rendering request 1 (user: null)
info: Rendering anonymous template { recipe: chrome-pdf, engine: handlebars }
info: Rendering request 1 finished in 552 ms

The script is the following:

const winston = require('winston')
const jsreport = require('jsreport-core')()
jsreport.logger.add(winston.transports.Console, { level: 'info' })
const fs = require('fs')
jsreport.init().then(() => {
    return jsreport.render({
        template: {
            content: '<h1>Hello {{foo}}</h1>',
            engine: 'handlebars',
            recipe: 'chrome-pdf'
        },
        data:{
            foo: 'truc'
        }
    }).then((resp) => {
        resp.result.pipe(fs.createWriteStream('helloWorld.pdf'))
    });
}).catch((e) => {
    console.error(e)
})

jsreport.config.json:

{
  "httpPort": 5488,
  "allowLocalFilesAccess": true,
  "store": {
    "provider": "fs"
  },
  "blobStorage": {
    "provider": "fs"
  },
  "reportTimeout": 60000,
  "extensions":{
    "fs-store":{
        "syncModifications": true
    }
  }

}

When I manually import the extension, i.e :

jsreport.use(require('jsreport-fs-store')())

the output remains the same (except that it says 'Handlebars' is not defined but it's another subject ;)

I don't see why jsreport is still using the default memory provider. How can I force jsreport to use it ?

Thanks in advance.

bjrmatos commented 3 years ago

jsreport-core by default does not load the config file (it is mentioned here), so the configuration in your file is not getting applied, you need to set loadConfig:true in the options of the core instance.

const jsreport = require('jsreport-core')({ loadConfig: true })
PierreFichet commented 3 years ago

Thanks ! I read that part too quickly and missed the loadConfig: false. Tank you for the quick reply. BTW, great job for this amazing project !