如何在src / main / resources中的项目目录结构中保存生成的pdf。 。在春季靴子?

时间:2018-01-06 08:17:32

标签: spring spring-boot pdf-generation

       **Please Guide me how to save the generated pdf in src/main/resources 
        folder .**



        @Component
        public class PdfGenaratorUtil {
        @Autowired
        private TemplateEngine templateEngine;

        public String createPdf(String templateName, Map map)
            throws Exception {
        Assert.notNull(templateName, "The templateName can not be null");
        Context ctx = new Context();
        if (map != null) {
            Iterator> itMap = map.entrySet().iterator();
            while (itMap.hasNext()) {
                Map.Entry pair = itMap.next();
                ctx.setVariable(pair.getKey().toString(), pair.getValue());
            }
        }
         String processedHtml = templateEngine.process(templateName, ctx);
         FileOutputStream os = null;
         String fileName = "POLIST ";
         try {
            final File outputFile = File.createTempFile(fileName, ".pdf",
                    new File("C:\\Users\\ES001\\Desktop\\")); // Here I am 
             saving in direct path
            os = new FileOutputStream(outputFile);
            ITextRenderer renderer = new ITextRenderer();
            renderer.setDocumentFromString(processedHtml);
            renderer.layout();
            renderer.createPDF(os, false);
            renderer.finishPDF();
            System.out.println("PDF created successfully");
            return outputFile.toString();
            } finally {
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) { 
                }
             }
            }
           }
         }

0 个答案:

没有答案