All Downloads are FREE. Search and download functionalities are using the official Maven repository.

br.com.objectos.way.reports.htmltopdf.HtmltopdfDelegateGuice Maven / Gradle / Ivy

/*
 * Copyright 2013 Objectos, Fábrica de Software LTDA.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package br.com.objectos.way.reports.htmltopdf;

import java.io.File;
import java.io.IOException;
import java.net.URL;

import com.google.common.base.Throwables;
import com.google.common.io.Files;
import com.google.common.io.Resources;
import com.google.inject.Inject;
import com.google.inject.Singleton;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author Marcio Endo ([email protected])
 */
@Singleton
class HtmltopdfDelegateGuice implements HtmltopdfDelegate {

  private static final Logger logger = LoggerFactory.getLogger(Htmltopdf.class);

  private final ObjectMapper mapper = new ObjectMapper();

  private final HtmltopdfConfig config;

  private File script;

  @Inject
  public HtmltopdfDelegateGuice(HtmltopdfConfig config) {
    this.config = config;
  }

  @Override
  public void render(HtmltopdfOptions options) {
    logger.debug("get :>> " + options.toString());

    try {
      String cmd = config.getCmd();
      String json = toJson(options);

      logger.debug("json=" + json);

      Process process = new ProcessBuilder(
          cmd,
          script.getAbsolutePath(),
          options.getUrl(),
          options.getFile().getAbsolutePath(),
          json)
          .start();
      process.waitFor();
    } catch (IOException e) {
      throw new HtmltopdfException(e);
    } catch (InterruptedException e) {
      throw new HtmltopdfException(e);
    }
  }

  @Override
  public void start() {
    try {
      script = tryToCopyScript();
    } catch (IOException e) {
      throw Throwables.propagate(e);
    }
  }

  @Override
  public void stop() {
  }

  private File tryToCopyScript() throws IOException {
    File file = File.createTempFile("way-reports-htmltopdf-", ".js");

    URL url = Resources.getResource(getClass(), "/htmltopdf.js");
    byte[] bytes = Resources.toByteArray(url);
    Files.write(bytes, file);

    return file;
  }

  private String toJson(HtmltopdfOptions options)
      throws JsonGenerationException, JsonMappingException, IOException {
    return mapper.writeValueAsString(options);
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy