YARG JSON data loader with DOCX template

Hi!
Could you please share a simple example, where we load template from a DOCX file, get data from JSON, and create a PDF report?
I get an error like “com.haulmont.yarg.exception.DataLoadingException: Query string doesn’t contain link to parameter”.
I’ve seen examples with DOCX template reading and REST data loader separately, but can’t get them connected.
Please help.
Thanks.
My files are attched.

data.zip (12.2K)

Hello.

Please see the invoice-json example.
You can run the report using sample.invoice.InvoiceTest#testInvoiceJsonReport method.

Please let me know if you have any questions about the example.

Thank you,

Thanks for pointing me to this example, I’ve seen it already and succeeded to run it. This example by the way uses Groovy Data Loader, which works fine by me. My question was namely the JSON data loader example, to clarify usage of parameters (see error message in my question).
By the way, for now I have a workaround: I use Groove Data Loader, which actually reads json data from a json file (emulates JSON dataloader), but I suppose it’s a kind of a KOSTYLI (from your name I suppose you know the meaning of this word :-))

Hello.

If you look at the file, you will see an example of how to use the JSON loader.
I’m not sure that you have seen it previously, because I have added it yesterday.

Anyway, I have changed your code a bit, to make it work. Let me showit to you.


package temp;

import com.haulmont.yarg.formatters.factory.DefaultFormatterFactory;
import com.haulmont.yarg.loaders.factory.DefaultLoaderFactory;
import com.haulmont.yarg.loaders.impl.JsonDataLoader;
import com.haulmont.yarg.reporting.ReportOutputDocument;
import com.haulmont.yarg.reporting.Reporting;
import com.haulmont.yarg.reporting.RunParams;
import com.haulmont.yarg.structure.Report;
import com.haulmont.yarg.structure.ReportBand;
import com.haulmont.yarg.structure.ReportOutputType;
import com.haulmont.yarg.structure.impl.BandBuilder;
import com.haulmont.yarg.structure.impl.ReportBuilder;
import com.haulmont.yarg.structure.impl.ReportTemplateBuilder;
import org.junit.Test;

import java.io.FileOutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;

public class Fragment {

    @Test
    public void test() throws Exception {
        String templateFileName = "template.docx";
        String templateFullFileName = "/home/u1/" + templateFileName;
        String reportFullFileName = templateFullFileName + ".pdf";
        String bandName = "A";

        ReportBuilder reportBuilder = new ReportBuilder();
        ReportTemplateBuilder reportTemplateBuilder = new ReportTemplateBuilder()
                .documentPath(templateFullFileName)
                .documentName(templateFileName)
                .outputType(ReportOutputType.pdf)
                .readFileFromPath();

        reportBuilder.template(reportTemplateBuilder.build());

        BandBuilder bandBuilder = new BandBuilder();
        /*here you build band with json data loader, which uses "parameter=param1 $" script. Please note the arguments order for the query method!
        You don't have to call JsonDataLoader manually*/
        ReportBand bandA = bandBuilder.name(bandName).query(bandName, "parameter=param1 $", "json", null).build();
        /*You don't have to create root band manually, because it has been already created by the builder
        the only thing you need to do is to put band to the report builder*/
        reportBuilder.band(bandA);

        Report report = reportBuilder.build();

        Reporting reporting = new Reporting();
        reporting.setFormatterFactory(new DefaultFormatterFactory());
        reporting.setLoaderFactory(new DefaultLoaderFactory().setJsonDataLoader(new JsonDataLoader()));

        /* load data from json as string*/
        String json = new String(Files.readAllBytes(Paths.get("/home/u1/data.json")));

        Map<String, Object> params = new HashMap<String, Object>();
        params.put("param1", json);

        /*Don't forget to put your params to the RunParams object*/
        ReportOutputDocument reportOutputDocument = reporting.runReport(new RunParams(report).params(params),
                new FileOutputStream(reportFullFileName));
    }
}

Please let me know if you have any questions.

Thank you.

Thank you very much, everything worked like a charm! :slight_smile:

A post was split to a new topic: Trying to create pdf using doc template with json data