SHARE
TWEET

Untitled

a guest Jul 19th, 2019 61 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. private static final String OUTPUT_FILE = "report.pdf";
  2. private static final String UTF_8 = "UTF-8";
  3.  
  4. @Autowired
  5. private AgfService service;
  6.  
  7. @Autowired
  8. private DateUteisComponent dataUtil;
  9.  
  10. public OutputStream generatePdf(Object dataReport, String template) throws Exception {
  11.  
  12.     String renderedHtmlContent = generateHtml(dataReport, template);
  13.     String xHtml = convertToXhtml(renderedHtmlContent);
  14.  
  15.     ITextRenderer renderer = new ITextRenderer();
  16.     renderer.getFontResolver().addFont("template/Code39.ttf", IDENTITY_H, EMBEDDED);
  17.  
  18.     String baseUrl = FileSystems.getDefault().getPath("src", "main", "resources").toUri().toURL().toString();
  19.     renderer.setDocumentFromString(xHtml, baseUrl);
  20.     renderer.layout();
  21.     ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  22.     renderer.createPDF(outputStream);
  23.     outputStream.close();
  24.     return outputStream;
  25. }
  26.  
  27. private String convertToXhtml(String html) throws UnsupportedEncodingException {
  28.     Tidy tidy = new Tidy();
  29.     tidy.setInputEncoding(UTF_8);
  30.     tidy.setOutputEncoding(UTF_8);
  31.     tidy.setXHTML(true);
  32.     ByteArrayInputStream inputStream = new ByteArrayInputStream(html.getBytes(UTF_8));
  33.     ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  34.     tidy.parseDOM(inputStream, outputStream);
  35.     return outputStream.toString(UTF_8);
  36. }
  37.  
  38. public String generateHtml(Object dataReport, String template) {
  39.     ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
  40.     templateResolver.setPrefix("/");
  41.     templateResolver.setSuffix(".html");
  42.     templateResolver.setTemplateMode(HTML);
  43.     templateResolver.setCharacterEncoding(UTF_8);
  44.  
  45.     TemplateEngine templateEngine = new TemplateEngine();
  46.     templateEngine.setTemplateResolver(templateResolver);
  47.  
  48.     Date instanteAtual = new Date();
  49.     String[] instanteAtualF = dataUtil.formatDateInstant(instanteAtual);
  50.  
  51.     Context context = new Context();
  52.     context.setVariable("instante", instanteAtualF);
  53.     context.setVariable("data", dataReport);
  54.  
  55.     String renderedHtmlContent = templateEngine.process(template, context);
  56.  
  57.     return renderedHtmlContent;
  58. }`
  59.      
  60. public OutputStream gerarRelatorioPrePostagemPorEstadoPdf(String estado) throws Exception {
  61.     List<PrePostagemBean> prePostagensBeans = generateDataForReportForUf(estado);
  62.     ByteArrayOutputStream outputStream = (ByteArrayOutputStream) this.generatePdfService.generatePdf(prePostagensBeans, "template/templateE");
  63.     this.sendConfirmationHtmlEmail(outputStream.toByteArray());
  64.     return outputStream;
  65. }
  66.      
  67. @GetMapping(value = "agfs/{estado}/pdf", produces = "application/pdf")
  68. public ResponseEntity<?> prepostReportEstadoPdf(@PathVariable("estado") String estado) throws Exception {
  69.     ByteArrayOutputStream os = (ByteArrayOutputStream) this.agfService.gerarRelatorioPrePostagemPorEstadoPdf(estado);
  70.     byte[] content = os.toByteArray();
  71.     HttpHeaders headers = new HttpHeaders();
  72.     headers.setContentType(MediaType.APPLICATION_PDF);
  73.     ResponseEntity<byte[]> response = new ResponseEntity<byte[]>(content, headers, HttpStatus.OK);
  74.     return response;
  75. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top