/*
* This file is part of rockframework.
*
* rockframework is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* rockframework 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>;.
*/
package br.net.woodstock.rockframework.document.pdf.itextpdf;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.StringReader;
import java.nio.charset.Charset;
import br.net.woodstock.rockframework.core.RockFrameworkVersion;
import br.net.woodstock.rockframework.core.util.Assert;
import br.net.woodstock.rockframework.core.utils.Conditions;
import br.net.woodstock.rockframework.document.DocumentInput;
import br.net.woodstock.rockframework.document.DocumentOutput;
import br.net.woodstock.rockframework.document.DocumentProcessor;
import br.net.woodstock.rockframework.document.pdf.PDFException;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.Pipeline;
import com.itextpdf.tool.xml.XMLWorker;
import com.itextpdf.tool.xml.XMLWorkerHelper;
import com.itextpdf.tool.xml.html.Tags;
import com.itextpdf.tool.xml.parser.XMLParser;
import com.itextpdf.tool.xml.pipeline.css.CSSResolver;
import com.itextpdf.tool.xml.pipeline.css.CssResolverPipeline;
import com.itextpdf.tool.xml.pipeline.end.PdfWriterPipeline;
import com.itextpdf.tool.xml.pipeline.html.AbstractImageProvider;
import com.itextpdf.tool.xml.pipeline.html.HtmlPipeline;
import com.itextpdf.tool.xml.pipeline.html.HtmlPipelineContext;
public class HTML2PDFProcessor implements DocumentProcessor {
private static final long serialVersionUID = RockFrameworkVersion.VERSION;
public static final String SOURCE_PARAMETER = "source";
public static final String CHARSET_PARAMETER = "charset";
public static final String MARGIN_PARAMETER = "margin";
public static final String MARGIN_TOP_PARAMETER = "marginTop";
public static final String MARGIN_BOTTOM_PARAMETER = "marginBottom";
public static final String MARGIN_LEFT_PARAMETER = "marginLeft";
public static final String MARGIN_RIGHT_PARAMETER = "marginRight";
public static final String IMAGES_ROOT_PATH_PARAMETER = "imagesRootPath";
public HTML2PDFProcessor() {
super();
}
@Override
public DocumentOutput process(final DocumentInput input) {
Assert.notNull(input, "input");
try {
Object source = input.getParameter(HTML2PDFProcessor.SOURCE_PARAMETER);
Object charset = input.getParameter(HTML2PDFProcessor.CHARSET_PARAMETER);
Object margin = input.getParameter(HTML2PDFProcessor.MARGIN_PARAMETER);
Object marginTop = input.getParameter(HTML2PDFProcessor.MARGIN_TOP_PARAMETER);
Object marginBottom = input.getParameter(HTML2PDFProcessor.MARGIN_BOTTOM_PARAMETER);
Object marginLeft = input.getParameter(HTML2PDFProcessor.MARGIN_LEFT_PARAMETER);
Object marginRight = input.getParameter(HTML2PDFProcessor.MARGIN_RIGHT_PARAMETER);
final String imagesRootPath = input.getParameter(HTML2PDFProcessor.IMAGES_ROOT_PATH_PARAMETER);
if (source == null) {
throw new DocumentException("Parameter 'source' + must be set");
}
if ((!(source instanceof InputStream)) && (!(source instanceof Reader)) && (!(source instanceof String))) {
throw new DocumentException("Invalid HTML source type");
}
Charset cCharset = IText.getCharset(charset);
Document document = new Document();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
PdfWriter writer = PdfWriter.getInstance(document, outputStream);
if (margin != null) {
float f = this.getMargin(margin);
writer.setMargins(f, f, f, f);
} else {
writer.setMargins(this.getMargin(marginLeft), this.getMargin(marginRight), this.getMargin(marginTop), this.getMargin(marginBottom));
}
document.open();
XMLWorkerHelper workerHelper = XMLWorkerHelper.getInstance();
HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);
htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());
if (Conditions.isNotEmpty(imagesRootPath)) {
htmlContext.setImageProvider(new AbstractImageProvider() {
@Override
public String getImageRootPath() {
return imagesRootPath;
}
});
}
HtmlPipeline htmlPipeline = new HtmlPipeline(htmlContext, new PdfWriterPipeline(document, writer));
CSSResolver cssResolver = workerHelper.getDefaultCssResolver(true);
Pipeline<?> pipeline = new CssResolverPipeline(cssResolver, htmlPipeline);
XMLWorker worker = new XMLWorker(pipeline, true);
XMLParser parser = new XMLParser(worker);
if (source instanceof InputStream) {
parser.parse((InputStream) source, cCharset);
} else if (source instanceof Reader) {
parser.parse((Reader) source);
} else {
StringReader reader = new StringReader((String) source);
parser.parse(reader);
}
document.close();
return new DocumentOutput(new ByteArrayInputStream(outputStream.toByteArray()));
} catch (IOException e) {
throw new PDFException(e);
} catch (DocumentException e) {
throw new PDFException(e);
}
}
private float getMargin(final Object value) {
if (value instanceof Float) {
return ((Float) value).floatValue();
}
if (value instanceof Number) {
return ((Number) value).floatValue();
}
if (value instanceof String) {
return Float.parseFloat((String) value);
}
return 10;
}
}