本文整理汇总了Java中org.xhtmlrenderer.pdf.ITextRenderer类的典型用法代码示例。如果您正苦于以下问题:Java ITextRenderer类的具体用法?Java ITextRenderer怎么用?Java ITextRenderer使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ITextRenderer类属于org.xhtmlrenderer.pdf包,在下文中一共展示了ITextRenderer类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: renderToPDF
import org.xhtmlrenderer.pdf.ITextRenderer; //导入依赖的package包/类
public static byte[] renderToPDF(String orig_html) throws Exception {
File html_file = null;
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ITextRenderer renderer = new ITextRenderer();
html_file = prepareTempHTML(orig_html);
renderer.setDocument(html_file);
renderer.layout();
renderer.createPDF(baos);
return baos.toByteArray();
} finally {
if (html_file != null) {
html_file.delete();
}
}
}
示例2: generatePdf
import org.xhtmlrenderer.pdf.ITextRenderer; //导入依赖的package包/类
@Override
@Nullable
public byte[] generatePdf(@NonNull final String _html) {
try {
final ITextRenderer renderer = new ITextRenderer();
final ITextFontResolver fontResolver = renderer.getFontResolver();
final ClassPathResource regular = new ClassPathResource("fonts/LiberationSerif-Regular.ttf");
fontResolver.addFont(regular.getURL().toString(), BaseFont.IDENTITY_H, true);
renderer.setDocumentFromString(_html);
renderer.layout();
@Cleanup final ByteArrayOutputStream os = new ByteArrayOutputStream();
renderer.createPDF(os);
return os.toByteArray();
} catch(Exception _e) {
log.error("Failed to generate PDF", _e);
return null;
}
}
示例3: exportPdfFile
import org.xhtmlrenderer.pdf.ITextRenderer; //导入依赖的package包/类
public static File exportPdfFile(String urlStr) throws Exception {
// String outputFile = this.fileRoot + "/" +
// ServiceConstants.DIR_PUBINFO_EXPORT + "/" + getFileName() + ".pdf";
String outputFile = "D:/Documents/test3.pdf";
OutputStream os = new FileOutputStream(outputFile);
ITextRenderer renderer = new ITextRenderer();
String str = getHtmlFile(urlStr);
renderer.setDocumentFromString(str);
ITextFontResolver fontResolver = renderer.getFontResolver();
fontResolver.addFont("F:/keyworkspace/survey/src/conf/itextpdf/simsun.ttc",BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
// // 宋体字
fontResolver.addFont("F:/keyworkspace/survey/src/conf/itextpdf/ARIALUNI.TTF",BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);// 宋体字
renderer.layout();
renderer.createPDF(os);
os.flush();
os.close();
return new File(outputFile);
}
示例4: convert
import org.xhtmlrenderer.pdf.ITextRenderer; //导入依赖的package包/类
@Override
public byte[] convert(String input) throws ConversionException {
try {
ITextRenderer renderer = new ITextRenderer();
renderer.setDocumentFromString(new String(input.getBytes()));
renderer.layout();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
renderer.createPDF(outputStream);
byte[] bytes = outputStream.toByteArray();
return bytes;
} catch (DocumentException e) {
e.printStackTrace();
throw ConversionException.HTML_TO_PDF_EXCEPTION;
}
}
示例5: resolveFonts
import org.xhtmlrenderer.pdf.ITextRenderer; //导入依赖的package包/类
private void resolveFonts( cfSession _Session, ITextRenderer _renderer ) throws dataNotSupportedException, cfmRunTimeException{
ITextFontResolver resolver = _renderer.getFontResolver();
boolean embed = getDynamic( _Session, "FONTEMBED" ).getBoolean();
for ( int i = 0; i < defaultFontDirs.length; i++ ){
File nextFontDir = new File( defaultFontDirs[i] );
File[] fontFiles = nextFontDir.listFiles( new FilenameFilter() {
public boolean accept( File _dir, String _name ){
String name = _name.toLowerCase();
return name.endsWith( ".otf" ) || name.endsWith( ".ttf" );
}
});
if ( fontFiles != null ){
for ( int f = 0; f < fontFiles.length; f++ ){
try{
resolver.addFont( fontFiles[f].getAbsolutePath(), BaseFont.IDENTITY_H,
embed );
}catch( Exception ignored ){} // ignore fonts that can't be added
}
}
}
}
示例6: convertHTMLtoPDF
import org.xhtmlrenderer.pdf.ITextRenderer; //导入依赖的package包/类
public static byte[] convertHTMLtoPDF(InputStream bais, boolean isFitWidth) throws ParserConfigurationException, SAXException, IOException, DocumentException {
ITextRenderer renderer = new ITextRenderer();
// renderer.getFontResolver().addFont("calibri.ttf", true);
renderer.setScaleToFit(isFitWidth);
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
InputSource is = new InputSource(bais);
Document doc = builder.parse(is);
renderer.setDocument(doc, null);
renderer.layout();
ByteArrayOutputStream out = new ByteArrayOutputStream();
renderer.createPDF(out);
out.flush();
out.close();
return out.toByteArray();
}
示例7: renderPdfDocument
import org.xhtmlrenderer.pdf.ITextRenderer; //导入依赖的package包/类
protected void renderPdfDocument(String htmlContent, OutputStream outputStream) {
ITextRenderer renderer = new ITextRenderer();
File temporaryFile = null;
try {
temporaryFile = File.createTempFile("htmlReport", ".htm");
DataOutputStream dataOutputStream = new DataOutputStream(new FileOutputStream(temporaryFile));
dataOutputStream.write(htmlContent.getBytes(Charset.forName("UTF-8")));
dataOutputStream.close();
loadFonts(renderer);
String url = temporaryFile.toURI().toURL().toString();
renderer.setDocument(url);
renderer.layout();
renderer.createPDF(outputStream);
} catch (Exception e) {
throw wrapWithReportingException("", e);
} finally {
FileUtils.deleteQuietly(temporaryFile);
}
}
示例8: loadFontsFromDirectory
import org.xhtmlrenderer.pdf.ITextRenderer; //导入依赖的package包/类
protected void loadFontsFromDirectory(ITextRenderer renderer, File fontsDir) {
if (fontsDir.exists()) {
if (fontsDir.isDirectory()) {
File[] files = fontsDir.listFiles((dir, name) -> {
String lower = name.toLowerCase();
return lower.endsWith(".otf") || lower.endsWith(".ttf");
});
for (File file : files) {
try {
// Usage of some fonts may be not permitted
renderer.getFontResolver().addFont(file.getAbsolutePath(), BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
} catch (IOException | DocumentException e) {
if (StringUtils.contains(e.getMessage(), "cannot be embedded due to licensing restrictions")) {
e.printStackTrace();//todo log message
} else {
e.printStackTrace();//todo log message
}
}
}
} else {
//todo log message
}
} else {
//todo log message
}
}
示例9: embedFonts
import org.xhtmlrenderer.pdf.ITextRenderer; //导入依赖的package包/类
public static void embedFonts(ITextRenderer renderer) {
final PropertySet propertySet = Properties.instance().getPropertySet();
for (final String propertyName : propertySet.getPropertiesStartsWith("oxf.fr.pdf.font.path")) {
final String path = StringUtils.trimToNull(propertySet.getString(propertyName));
if (path != null) {
try {
// Overriding the font family is optional
final String family; {
final String[] tokens = StringUtils.split(propertyName, '.');
if (tokens.length >= 6) {
final String id = tokens[5];
family = StringUtils.trimToNull(propertySet.getString("oxf.fr.pdf.font.family" + '.' + id));
} else {
family = null;
}
}
// Add the font
renderer.getFontResolver().addFont(path, family, BaseFont.IDENTITY_H, BaseFont.EMBEDDED, null);
} catch (Exception e) {
logger.warn("Failed to load font by path: '" + path + "' specified with property '" + propertyName + "'");
}
}
}
}
示例10: createPdf
import org.xhtmlrenderer.pdf.ITextRenderer; //导入依赖的package包/类
public static void createPdf(String content, String dest)
throws IOException, DocumentException, com.lowagie.text.DocumentException {
ITextRenderer render = new ITextRenderer();
ITextFontResolver fontResolver = render.getFontResolver();
fontResolver.addFont(FONT, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
// 解析html生成pdf
render.setDocumentFromString(content);
// 解决图片相对路径的问题
render.getSharedContext().setBaseURL(LOGO_PATH);
render.layout();
render.createPDF(new FileOutputStream(dest));
}
示例11: generatePdfToPath
import org.xhtmlrenderer.pdf.ITextRenderer; //导入依赖的package包/类
public void generatePdfToPath(String templatePath, Map<String, Object> model, String outputFilePath) throws FileNotFoundException, IOException, DocumentException, TemplateException {
ITextRenderer renderer = generatePdf(templatePath, model);
OutputStream os = new FileOutputStream(outputFilePath);
renderer.createPDF(os, true);
os.close();
log.debug("Generate Pdf to File Successfully.");
}
示例12: generatePdfToInputStream
import org.xhtmlrenderer.pdf.ITextRenderer; //导入依赖的package包/类
public InputStream generatePdfToInputStream(String templatePath, Map<String, Object> model) throws FileNotFoundException, IOException, DocumentException, TemplateException {
ITextRenderer renderer = generatePdf(templatePath, model);
ByteArrayOutputStream pdfOutS = new ByteArrayOutputStream();
renderer.createPDF(pdfOutS, true);
InputStream inputStream = new ByteArrayInputStream(pdfOutS.toByteArray());
pdfOutS.close();
log.debug("Generate Pdf as InputStream Successfully.");
return inputStream;
}
示例13: generatePdf
import org.xhtmlrenderer.pdf.ITextRenderer; //导入依赖的package包/类
private ITextRenderer generatePdf(String templatePath, Map<String, Object> model) throws FileNotFoundException, IOException, DocumentException, TemplateException {
if (templatePath ==null || "".equals(templatePath) || model ==null){
return null;
}
fmConfig.setDefaultEncoding("UTF-8");
fmConfig.setClassForTemplateLoading(this.getClass(), "/");
String resourcePath = getResourcePath();
fmConfig.setSharedVariable("base", resourcePath);
Template template = fmConfig.getTemplate(templatePath);
StringWriter result = new StringWriter();
template.process(model, result);
String content = result.toString();
//String content = FreeMarkers.renderTemplate(template, model);//<#assign fmt=JspTaglibs["/WEB-INF/tlds/fmt.tld"]><@fmt.message key="label.fn.compnt.name"/>
ITextRenderer renderer = new ITextRenderer();
ITextFontResolver fontResolver = renderer.getFontResolver();
fontResolver.addFont(resourcePath.concat("/report/resource/font/MSYH.TTF"), BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
fontResolver.addFont(resourcePath.concat("/report/resource/font/ARIAL.TTF"), BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
//Image Path
//renderer.getSharedContext().setBaseURL(resourcePath.concat("/report/resource/image"));
//renderer.getSharedContext().setUserAgentCallback(new HttpURLUserAgent(renderer.getOutputDevice()));
renderer.setDocumentFromString(content);
renderer.layout();
log.debug("Generate Pdf as ITextRenderer Successfully.");
return renderer;
}
示例14: initRenderer
import org.xhtmlrenderer.pdf.ITextRenderer; //导入依赖的package包/类
private ITextRenderer initRenderer() throws DocumentException, IOException {
ITextRenderer renderer = new ITextRenderer();
asList(FONTS).forEach(font -> runtimeEx(() -> renderer.getFontResolver().addFont(
getClass().getResource(font).toURI().toString(), IDENTITY_H, NOT_EMBEDDED),
e -> logger.error("Error while configuring fonts", e)));
return renderer;
}
示例15: createPDF
import org.xhtmlrenderer.pdf.ITextRenderer; //导入依赖的package包/类
@Override
public byte[] createPDF(byte[] documentSource, String url) {
try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {
ITextRenderer renderer = initRenderer();
Document document = load(new ByteArrayInputStream(documentSource)).getDocument();
renderer.setDocument(document, url);
renderer.layout();
renderer.createPDF(os);
return os.toByteArray();
} catch (Exception e) {
throw new RuntimeException(e);
}
}