![JAR search and dependency download from the Maven repository](/logo.png)
net.sf.nervalreports.converters.FSHTMLToPDFConverter Maven / Gradle / Ivy
/** This file is part of nervalreports.
*
* nervalreports is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* nervalreports 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with nervalreports. If not, see . */
package net.sf.nervalreports.converters;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import org.xhtmlrenderer.pdf.ITextRenderer;
import com.lowagie.text.DocumentException;
import net.sf.nervalreports.core.TextualFileConverter;
/** Converter from HTML to PDF using the FlyingSaucer library (http://github.com/flyingsaucerproject/flyingsaucer). */
public class FSHTMLToPDFConverter extends TextualFileConverter {
/** The single instance of the converter. */
private static final FSHTMLToPDFConverter INSTANCE = new FSHTMLToPDFConverter();
/** target file format */
private static final String TARGET_FILE_FORMAT = "pdf";
/** Content type (to use as servlet response) of output file. */
private static final String CONTENT_TYPE = "application/pdf";
private FSHTMLToPDFConverter() {
}
/** @return the single instance of {@link FSHTMLToPDFConverter}. */
public static FSHTMLToPDFConverter getInstance() {
return INSTANCE;
}
/** {@inheritDoc} */
@Override
public byte[] doConvert(String html, Charset charset) throws IOException, DocumentException {
if (html != null) {
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
ITextRenderer renderer = new ITextRenderer();
renderer.setDocumentFromString(html);
renderer.layout();
renderer.createPDF(outputStream);
return outputStream.toByteArray();
}
}
return null;
}
/** {@inheritDoc} */
@Override
public String getContentType() {
return CONTENT_TYPE;
}
/** {@inheritDoc} */
@Override
public String getFileExtension() {
return TARGET_FILE_FORMAT;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy