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) {