/*
 * Copyright (c) 2015 Konstantin Lepa <konstantin+ukase@lepabox.net>
 *
 * This file is part of Ukase.
 *
 *  Ukase is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as
 *  published by the Free Software Foundation, either version 3 of the
 *  License, or (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

package com.github.ukase.service;

import com.github.ukase.toolkit.render.RenderException;
import com.github.ukase.toolkit.pdf.PdfSaucerRenderer;
import com.itextpdf.text.DocumentException;
import com.github.ukase.toolkit.ResourceProvider;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

@Service
@Log4j
public class PdfRenderer implements Renderer<String, byte[]> {
    private final ResourceProvider provider;

    @Autowired
    public PdfRenderer(ResourceProvider provider) {
        this.provider = provider;
    }

    public byte[] render(String html) throws RenderException {
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            PdfSaucerRenderer renderer = provider.getRenderer(html);

            renderer.createPDF(baos);
            renderer.finishPDF();

            return baos.toByteArray();
        } catch (IOException|DocumentException e) {
            throw new RenderException("Cannot render pdf", e, "pdf");
        }
    }
}

@@ -1539,16 +1539,102 @@
   try %7B%0A
+            log.debug(%22Start rendering pdf from html with size of %22 + html.length());%0A
         
@@ -1691,24 +1691,24 @@
 utStream();%0A
-
             
@@ -1761,24 +1761,69 @@
 rer(html);%0A%0A
+            log.debug(%22Renderer prepared%22);%0A%0A
             
@@ -1879,24 +1879,63 @@
 nishPDF();%0A%0A
+            log.debug(%22pdf rendered%22);%0A
             

improve logging on pdf renderer