|
| 1 | +/* |
| 2 | + * To change this license header, choose License Headers in Project Properties. |
| 3 | + * To change this template file, choose Tools | Templates |
| 4 | + * and open the template in the editor. |
| 5 | + */ |
| 6 | +package com.baeldung.yarg; |
| 7 | + |
| 8 | +import com.haulmont.yarg.formatters.factory.DefaultFormatterFactory; |
| 9 | +import com.haulmont.yarg.loaders.factory.DefaultLoaderFactory; |
| 10 | +import com.haulmont.yarg.loaders.impl.JsonDataLoader; |
| 11 | +import com.haulmont.yarg.reporting.Reporting; |
| 12 | +import com.haulmont.yarg.reporting.RunParams; |
| 13 | +import com.haulmont.yarg.structure.Report; |
| 14 | +import com.haulmont.yarg.structure.ReportBand; |
| 15 | +import com.haulmont.yarg.structure.ReportOutputType; |
| 16 | +import com.haulmont.yarg.structure.impl.BandBuilder; |
| 17 | +import com.haulmont.yarg.structure.impl.ReportBuilder; |
| 18 | +import com.haulmont.yarg.structure.impl.ReportTemplateBuilder; |
| 19 | +import java.io.File; |
| 20 | +import java.io.IOException; |
| 21 | +import javax.servlet.http.HttpServletResponse; |
| 22 | +import org.apache.commons.io.FileUtils; |
| 23 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 24 | +import org.springframework.web.bind.annotation.RequestMethod; |
| 25 | +import org.springframework.web.bind.annotation.RestController; |
| 26 | + |
| 27 | +@RestController |
| 28 | +public class DocumentController { |
| 29 | + |
| 30 | + @RequestMapping(path = "/generate/doc", method = RequestMethod.GET) |
| 31 | + public void generateDocument(HttpServletResponse response) throws IOException { |
| 32 | + ReportBuilder reportBuilder = new ReportBuilder(); |
| 33 | + ReportTemplateBuilder reportTemplateBuilder = new ReportTemplateBuilder() |
| 34 | + .documentPath("./src/main/resources/Letter.docx") |
| 35 | + .documentName("Letter.docx") |
| 36 | + .outputType(ReportOutputType.docx) |
| 37 | + .readFileFromPath(); |
| 38 | + reportBuilder.template(reportTemplateBuilder.build()); |
| 39 | + BandBuilder bandBuilder = new BandBuilder(); |
| 40 | + String json = FileUtils.readFileToString(new File("./src/main/resources/Data.json")); |
| 41 | + ReportBand main = bandBuilder.name("Main") |
| 42 | + .query("Main", "parameter=param1 $.main", "json") |
| 43 | + .build(); |
| 44 | + reportBuilder.band(main); |
| 45 | + Report report = reportBuilder.build(); |
| 46 | + |
| 47 | + Reporting reporting = new Reporting(); |
| 48 | + reporting.setFormatterFactory(new DefaultFormatterFactory()); |
| 49 | + reporting.setLoaderFactory( |
| 50 | + new DefaultLoaderFactory() |
| 51 | + .setJsonDataLoader(new JsonDataLoader())); |
| 52 | + response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document"); |
| 53 | + reporting.runReport(new RunParams(report).param("param1", json), response.getOutputStream()); |
| 54 | + } |
| 55 | + |
| 56 | +} |
0 commit comments