java pdf转图片base64,itextpdf 实现html转pdf中中文及图片base64的解决方法
itextpdf 实现html转pdf中中文及图片base64的解决方法:
1. 中文解决方案:
实现FontProvider接口:
package com.mumu.image2pdf;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontProvider;
import com.itextpdf.text.pdf.BaseFont;
public class MyFontProvider implements FontProvider {
private BaseColor bc;
private String fontname;
private String encoding;
private boolean embedded;
private boolean cached;
private float size;
private int style;
private BaseFont baseFont;
public MyFontProvider() {
}
public BaseColor getBc() {
return bc;
}
public void setBc(BaseColor bc) {
this.bc = bc;
}
public String getFontname() {
return fontname;
}
public void setFontname(String fontname) {
this.fontname = fontname;
}
public String getEncoding() {
return encoding;
}
public void setEncoding(String encoding) {
this.encoding = encoding;
}
public boolean isEmbedded() {
return embedded;
}
public void setEmbedded(boolean embedded) {
this.embedded = embedded;
}
public boolean isCached() {
return cached;
}
public void setCached(boolean cached) {
this.cached = cached;
}
public float getSize() {
return size;
}
public void setSize(float size) {
this.size = size;
}
public int getStyle() {
return style;
}
public void setStyle(int style) {
this.style = style;
}
public BaseFont getBaseFont() {
return baseFont;
}
public void setBaseFont(BaseFont baseFont) {
this.baseFont = baseFont;
}
public MyFontProvider(BaseColor bc, String fontname, String encoding, boolean embedded, boolean cached, float size,
int style, BaseFont baseFont) {
super();
this.bc = bc;
this.fontname = fontname;
this.encoding = encoding;
this.embedded = embedded;
this.cached = cached;
this.size = size;
this.style = style;
this.baseFont = baseFont;
}
@Override
public Font getFont(String arg0, String arg1, boolean arg2, float arg3, int arg4, BaseColor arg5) {
Font font = null;
if (baseFont == null) {
font = new Font();
} else {
font = new Font(baseFont);
}
font.setColor(arg5);
font.setFamily(fontname);
font.setSize(size);
font.setStyle(arg4);
return font;
}
@Override
public boolean isRegistered(String arg0) {
// TODO Auto-generated method stub
return true;
}
}
2. 图片base64的dataurl显示问题解决:
实现com.itextpdf.tool.xml.html.Image类
package com.mumu.image2pdf;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Element;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.codec.Base64;
import com.itextpdf.tool.xml.NoCustomContextException;
import com.itextpdf.tool.xml.Tag;
import com.itextpdf.tool.xml.WorkerContext;
import com.itextpdf.tool.xml.exceptions.RuntimeWorkerException;
import com.itextpdf.tool.xml.html.HTML;
import com.itextpdf.tool.xml.pipeline.html.HtmlPipelineContext;
public class ImageTagProcessor extends com.itextpdf.tool.xml.html.Image {
/*
* (non-Javadoc)
*
* @see com.itextpdf.tool.xml.TagProcessor#endElement(com.itextpdf.tool.xml.Tag, java.util.List, com.itextpdf.text.Document)
*/
@Override
public List end(final WorkerContext ctx, final Tag tag, final List currentContent) {
final Map attributes = tag.getAttributes();
String src = attributes.get(HTML.Attribute.SRC);
List elements = new ArrayList(1);
if (null != src && src.length() > 0) {
Image img = null;
if (src.startsWith("data:image/")) {
final String base64Data = src.substring(src.indexOf(",") + 1);
try {
img = Image.getInstance(Base64.decode(base64Data));
} catch (Exception e) {
throw new I18NIllegalArgumentException(e);
}
if (img != null) {
try {
final HtmlPipelineContext htmlPipelineContext = getHtmlPipelineContext(ctx);
elements.add(getCssAppliers().apply(new Chunk((com.itextpdf.text.Image) getCssAppliers().apply(img, tag, htmlPipelineContext), 0, 0, true), tag,
htmlPipelineContext));
} catch (NoCustomContextException e) {
throw new RuntimeWorkerException(e);
}
}
}
if (img == null) {
elements = super.end(ctx, tag, currentContent);
}
}
return elements;
}
}
3. 转换方法:
public void html2pdf(String html, File file) throws I18NIllegalArgumentException {
try {
// step 1
Document document = new Document();
BaseFont bfChinese;
bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", false);
MyFontProvider myFontProvider = new MyFontProvider(BaseColor.BLACK, "", "", false, false, 16, 1, bfChinese);
// step 2
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
// step 3
document.open();
final TagProcessorFactory tagProcessorFactory = Tags.getHtmlTagProcessorFactory();
tagProcessorFactory.removeProcessor(HTML.Tag.IMG);
tagProcessorFactory.addProcessor(new ImageTagProcessor(), HTML.Tag.IMG);
final CssFilesImpl cssFiles = new CssFilesImpl();
cssFiles.add(XMLWorkerHelper.getInstance().getDefaultCSS());
final StyleAttrCSSResolver cssResolver = new StyleAttrCSSResolver(cssFiles);
final HtmlPipelineContext hpc = new HtmlPipelineContext(new CssAppliersImpl(myFontProvider));
hpc.setAcceptUnknown(true).autoBookmark(true).setTagFactory(tagProcessorFactory);
final HtmlPipeline htmlPipeline = new HtmlPipeline(hpc, new PdfWriterPipeline(document, writer));
final Pipeline> pipeline = new CssResolverPipeline(cssResolver, htmlPipeline);
final XMLWorker worker = new XMLWorker(pipeline, true);
final Charset charset = Charset.forName("UTF-8");
final XMLParser xmlParser = new XMLParser(true, worker, charset);
ByteArrayInputStream bais = new ByteArrayInputStream(html.getBytes("UTF-8"));
xmlParser.parse(bais, charset);
// step 5
document.close();
bais.close();
} catch (Exception e) {
throw new I18NIllegalArgumentException(e);
}
}
PS: input框等的显示还没实现,待完善
java pdf转图片base64,itextpdf 实现html转pdf中中文及图片base64的解决方法相关推荐
- java http 400 错误的请求_IE浏览器http请求,中文传参报400错误-解决方法
做项目的时候,遇到一个小的问题.一个get请求列表数据的接口,在其它浏览器上是可以正常请求的.但是在ie浏览器上确出现奇怪的http请求400错误,其含义是你访问的页面域名不存在或者请求错误,自己的本 ...
- itextpdf 实现html转pdf中中文及图片base64的解决方法
为什么80%的码农都做不了架构师?>>> itextpdf 实现html转pdf中中文及图片base64的解决方法: 1. 中文解决方案: 实现FontProvider接口: ...
- java 转换gbk编码,java中GBK转UTF-8乱码的解决方法
java中GBK转UTF-8乱码的解决方法 如果自己采用的是GBK编码,对方采用得到是UTF-8编码,发送数据时需要将GBK编码数据转换成UTF-8编码数据,这样对方才不会乱码. 问题出现:GBK转U ...
- android 按钮 图片文字居中显示,[Android]Android 布局中如何让图片和文字居中显示?...
图片文字居中显示 **①组件TextView的属性 drawableTop ``` 块内或者行内图片与文字居中对齐最靠谱的方式! 做图片与文字在一行的按钮时候最常用到,总结了一个靠谱的方法,终于可以完 ...
- php文章远程图片,php保存远程图片到本地 php正则匹配文章中的远程图片地址
在添加文章的时候,很多情况下我们需要处理文章中的远程图片,将其保存到本地,以免别人网站删除后文章里面就无法访问了. 因此我们需要正则匹配文章中的图片地址, 这里我们使用php的正则表达式来实现:$co ...
- java oom_Java中关于OOM的场景及解决方法
1.OOM for Heap=>例如:java.lang.OutOfMemoryError: Java heap space 分 析 此OOM是由于JVM中heap的***值不满足需要,将设置 ...
- 错误/异常:java.io.FileNotFoundException: .\src\db.properties (系统找不到指定的路径。);的解决方法...
1.异常视图 2.解决方法 与之相关的部分代码: 1 static{ 2 try { 3 //读取db.properties 4 Properties props = new Properties() ...
- java中的方法求和_在Java中模拟求和类型的巧妙解决方法
java中的方法求和 在继续阅读实际文章之前,我想感谢令人敬畏的Javaslang库的作者Daniel Dietrich ,他在我面前有了这个主意: @lukaseder尝试使用静态方法<T,T ...
- 在Java中模拟求和类型的巧妙解决方法
在继续阅读实际文章之前,我想感谢令人敬畏的Javaslang库的作者Daniel Dietrich ,他在我面前有了这个主意: @lukaseder尝试使用静态方法<T,T1扩展T,... Tn ...
- java咖啡机故障5_【咖啡小常识】咖啡机不得不知道的常见故障及解决方法
原标题:[咖啡小常识]咖啡机不得不知道的常见故障及解决方法 咖啡行业交流请加私 人微信精品咖啡香,微信号: (长按复制) thinkingcapacity 一.没有蒸汽或做出的咖啡不热 可能原因 1. ...
最新文章
- ASCII + Url + Base64
- 把windows键盘作为xfce环境中的打开Applications
- Leetcode每日一题:1025.divisor-game(除数博弈)
- Codeforces Round #467 (Div. 1): C. Lock Puzzle(构造)
- [SDOI2011]打地鼠
- RailsCasts中文版,#15 Fun with Find Conditions 使用hash为查询条件以便生成正确语法的SQL查询...
- 学习笔记-Matlab算法篇-动态规划
- 工具-破解pdf密码
- ueditor imageup.php,ThinkPHP整合百度Ueditor,thinkphpueditor_PHP教程
- python开发ps插件_【UI/UE】22款设计师必备的PS插件【附教程】
- FFmpeg 视频旋转处理命令
- 8年Python程序员,去2线城市大厂面试崩了……网友:太真实!
- Servlet容器与Web容器详解
- 性质:自反、反自反、对称、反对称、传递、互斥
- 【视频】离职创业感悟
- Android 手表WearOs 禁止滑动返回、监听滑动事件分发
- USB PD协议规范
- 电子元件-温湿度元件与传感器
- 华为5g cpe 虚拟服务器,一图看懂华为5G CPE Pro
- 机器学习算法衡量指标——准确率、精确率(查准率)、召回率(查全率)