<commit_before>package de.braintags.netrelay.controller.template;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.Properties;

import org.xhtmlrenderer.pdf.ITextRenderer;

import com.itextpdf.text.DocumentException;

import de.braintags.io.vertx.util.exception.NoSuchFileException;
import de.braintags.io.vertx.util.file.FileSystemUtil;
import de.braintags.netrelay.controller.AbstractController;
import de.braintags.netrelay.controller.ThymeleafTemplateController;
import io.vertx.core.AsyncResult;
import io.vertx.core.Future;
import io.vertx.core.Handler;
import io.vertx.core.Vertx;
import io.vertx.core.buffer.Buffer;
import io.vertx.ext.web.RoutingContext;
import io.vertx.ext.web.templ.ThymeleafTemplateEngine;

/**
 * Html2PdfController creates a pdf document from an html source, which was generated by a template by Thymeleaf. The
 * template, which shall be used can be defined on two different ways:
 * <UL>
 * <LI>by property {@value #TEMPLATE_NAME_PROP}<br/>
 * if this property is defined, then for each request this template is taken
 * <LI>by the requested path<br/>
 * if the above property is not set, then the controller will use the current request path as template path. He will
 * exchange the file extension against ".html" to find a corresponding template. Thus a request to "/testpath/test.pdf"
 * will lead to a template with the path /testpath/test.html"
 * </UL>
 * <br>
 * <br>
 * <br/>
 * Config-Parameter:<br/>
 * possible parameters, which are read from the configuration
 * <UL>
 * <LI>{@value #TEMPLATE_NAME_PROP}
 * <LI>{@value #FONT_PATH_PROP}
 * <LI/>all parameters like defined in {@link ThymeleafTemplateController}
 * </UL>
 * <br>
 * 
 * Request-Parameter:<br/>
 * none
 * <br/>
 * 
 * Result-Parameter:<br/>
 * none
 * <br/>
 * 
 * further infoprmation on how to create the templates:
 * http://flyingsaucerproject.github.io/flyingsaucer/r8/guide/users-guide-R8.html#xil_20
 * 
 * Copyright: Copyright (c) 09.03.2016 <br>
 * Company: Braintags GmbH <br>
 * 
 * @author mremme
 * 
 */

public class Html2PdfController extends AbstractController {

  /**
   * If the content shall be generated by one specific template, this is defined with this property. If this property is
   * undefined, the template will be used by using the path of the current request.
   */
  public static final String TEMPLATE_NAME_PROP = "template";

  /**
   * The property defines the path to a directory, where font definitions are stored as ttf or otf files. All found
   * definitions are added to the resulting pdf
   */
  public static final String FONT_PATH_PROP = "fonts";

  private String template;
  private ThymeleafTemplateEngine templateEngine;
  private String templateDirectory;
  private String fontsDir;

  /**
   * 
   */
  public Html2PdfController() {
  }

  /*
   * (non-Javadoc)
   * 
   * @see io.vertx.core.Handler#handle(java.lang.Object)
   */
  @Override
  public void handle(RoutingContext context) {
    createTemplateContent(context, tc -> {
      if (succeeded(context, tc)) {
        convert(tc.result(), cr -> {<commit_msg>adaption to new final method in super class<commit_after>package de.braintags.netrelay.controller.template;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.Properties;

import org.xhtmlrenderer.pdf.ITextRenderer;

import com.itextpdf.text.DocumentException;

import de.braintags.io.vertx.util.exception.NoSuchFileException;
import de.braintags.io.vertx.util.file.FileSystemUtil;
import de.braintags.netrelay.controller.AbstractController;
import de.braintags.netrelay.controller.ThymeleafTemplateController;
import io.vertx.core.AsyncResult;
import io.vertx.core.Future;
import io.vertx.core.Handler;
import io.vertx.core.Vertx;
import io.vertx.core.buffer.Buffer;
import io.vertx.ext.web.RoutingContext;
import io.vertx.ext.web.templ.ThymeleafTemplateEngine;

/**
 * Html2PdfController creates a pdf document from an html source, which was generated by a template by Thymeleaf. The
 * template, which shall be used can be defined on two different ways:
 * <UL>
 * <LI>by property {@value #TEMPLATE_NAME_PROP}<br/>
 * if this property is defined, then for each request this template is taken
 * <LI>by the requested path<br/>
 * if the above property is not set, then the controller will use the current request path as template path. He will
 * exchange the file extension against ".html" to find a corresponding template. Thus a request to "/testpath/test.pdf"
 * will lead to a template with the path /testpath/test.html"
 * </UL>
 * <br>
 * <br>
 * <br/>
 * Config-Parameter:<br/>
 * possible parameters, which are read from the configuration
 * <UL>
 * <LI>{@value #TEMPLATE_NAME_PROP}
 * <LI>{@value #FONT_PATH_PROP}
 * <LI/>all parameters like defined in {@link ThymeleafTemplateController}
 * </UL>
 * <br>
 * 
 * Request-Parameter:<br/>
 * none
 * <br/>
 * 
 * Result-Parameter:<br/>
 * none
 * <br/>
 * 
 * further infoprmation on how to create the templates:
 * http://flyingsaucerproject.github.io/flyingsaucer/r8/guide/users-guide-R8.html#xil_20
 * 
 * Copyright: Copyright (c) 09.03.2016 <br>
 * Company: Braintags GmbH <br>
 * 
 * @author mremme
 * 
 */

public class Html2PdfController extends AbstractController {

  /**
   * If the content shall be generated by one specific template, this is defined with this property. If this property is
   * undefined, the template will be used by using the path of the current request.
   */
  public static final String TEMPLATE_NAME_PROP = "template";

  /**
   * The property defines the path to a directory, where font definitions are stored as ttf or otf files. All found
   * definitions are added to the resulting pdf
   */
  public static final String FONT_PATH_PROP = "fonts";

  private String template;
  private ThymeleafTemplateEngine templateEngine;
  private String templateDirectory;
  private String fontsDir;

  /**
   * 
   */
  public Html2PdfController() {
  }

  /*
   * (non-Javadoc)
   * 
   * @see io.vertx.core.Handler#handle(java.lang.Object)
   */
  @Override
  public void handleController(RoutingContext context) {
    createTemplateContent(context, tc -> {
      if (succeeded(context, tc)) {
        convert(tc.result(), cr -> {
{'fraction_non_alphanumeric': 0.10430769230769231, 'fraction_numerical': 0.004615384615384616, 'mean_word_length': 4.094827586206897, 'pattern_counts': {'":': 0, '<': 47, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4444754', 'n_tokens_mistral': 2038, 'n_tokens_neox': 1985, 'n_words': 607}