PDF转换工具类(byte[]转PDF并生成文件)

对html转为标准的xhtml

  public byte[] transferHtml2XHtml(byte[] html){Tidy tidy = new Tidy();ByteArrayOutputStream baos = new ByteArrayOutputStream();tidy.setQuiet(true);tidy.setInputEncoding("utf-8");tidy.setQuiet(true);tidy.setOutputEncoding("utf-8");tidy.setShowWarnings(false);tidy.setIndentContent(true);tidy.setSmartIndent(true);tidy.setIndentAttributes(false);tidy.setWraplen(1024);tidy.setXHTML(true);tidy.parseDOM(new ByteArrayInputStream(html), baos);return baos.toByteArray();}

将标准的byte[]转为PDF

  /*** @param xhtml xhtml文件内容* @return 转换后的pdf文件内容* @throws Exception*/public static byte[] transferPDF2Html(byte[] xhtml) throws Exception {ByteArrayOutputStream pdfOS = new ByteArrayOutputStream();System.out.println(pdfOS);Document document = new Document(PageSize.A4, 36, 36, 36, 36);// 上、下、左、右间距PdfWriter writer = PdfWriter.getInstance(document, pdfOS);document.open();InputStream is = new ByteArrayInputStream(xhtml);System.out.println("is    :"+is);XMLWorkerHelper.getInstance().parseXHtml(writer, document, is, StandardCharsets.UTF_8, new FontProvider() {public boolean isRegistered(String fontname) {return false;}public Font getFont(String fontname, String encoding, boolean embedded, float size, int style, BaseColor color) {try {BaseFont bfChinese;
//                    bfChinese = BaseFont.createFont("STSong-Light", "Adobe-GB1-0", BaseFont.NOT_EMBEDDED);
//                    bfChinese = BaseFont.createFont("STSongStd-Light", "UniGB-UTF8-H", BaseFont.NOT_EMBEDDED);bfChinese = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);return new Font(bfChinese, size, style, color);} catch (DocumentException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}return null;}});document.close();byte[] pdfByte = pdfOS.toByteArray();pdfOS.close();is.close();writer.close();log.info("PDF转换完成");return pdfByte;}

生成pdf文件

public static void getFile(byte[] bfile, String filePath, String fileName){BufferedOutputStream bos = null;FileOutputStream fos = null;File file = null;try{File dir = new File(filePath);if ((!dir.exists()) && (dir.isDirectory())) {dir.mkdirs();}file = new File(filePath + "\\" + fileName);fos = new FileOutputStream(file);bos = new BufferedOutputStream(fos);bos.write(bfile); return;}catch (Exception e){e.printStackTrace();}finally{if (bos != null) {try{bos.close();}catch (IOException e1){e1.printStackTrace();}}if (fos != null) {try{fos.close();}catch (IOException e1){e1.printStackTrace();}}}}

相关jar包

<!--jsoup start --><dependency><groupId>com.itextpdf</groupId><artifactId>itext-asian</artifactId><version>5.2.0</version></dependency><dependency><groupId>org.jsoup</groupId><artifactId>jsoup</artifactId><version>1.7.3</version></dependency><dependency><groupId>net.sf.jtidy</groupId><artifactId>jtidy</artifactId><version>r938</version></dependency><dependency><groupId>com.itextpdf</groupId><artifactId>itextpdf</artifactId><version>5.5.13.2</version></dependency><dependency><groupId>com.itextpdf</groupId><artifactId>itext-asian</artifactId><version>5.2.0</version></dependency><dependency><groupId>com.itextpdf.tool</groupId><artifactId>xmlworker</artifactId><version>5.5.13.2</version></dependency>
<!--jsoup end -->

PDF转换工具类(byte[]转PDF并生成文件)相关推荐

  1. 一款免费的PDF转换工具-LightPDF完美版(PDF编辑器) 最新版

    LightPDF完美版(PDF编辑器)是一款免费的pdf编辑转换工具,可以在pdf和word.ppt以及png等文件格式之间进行互相转换,非常的便捷.LightPDF破解版不需要注册,打开软件选择需要 ...

  2. 「Java工具类」pdf导出工具类java导出pdf文件工具类

    介绍语 本号主要是Java常用关键技术点,通用工具类的分享:以及springboot+springcloud+Mybatisplus+druid+mysql+redis+swagger+maven+d ...

  3. pdf转换工具有哪些?试一试这几个方法!

    PDF文件的广泛使用已成为常态,办公软件也在不断的升级.由于PDF的格式不会轻易改变,所以工作日常中通用性很强,一般情况下打开都不会出现编辑或者排版错乱的问题.但是它有时候就也恰恰限制了我们对其直接内 ...

  4. 好用免费的在线PDF转换工具推荐

    首先,先上网址 ClickOnce ,这个是目前能找到的里面,操作最直观的工具类网站了 界面相对来说,比较的优雅,好用是他最大的特点. 转换工具方面,支持多种转换,总体给我的感觉是非常的干净整洁,符合 ...

  5. 轻量级实用PDF转换工具

    老农家有个小侄子,说是学校里现在交作业越来越麻烦了,一会会要交pdf格式的,一会会要交word格式的,弄来弄去好是麻烦. 老农作为一个合格的长辈,于是就去帮他翻了翻家底,找到了这款非常好用的PDF转换 ...

  6. 打开pdf文件提示文件过大_pdf文件太大如何用pdf转换工具进行压缩?

    PDF文件如果是由许多高清图片组合而成的,一般来说所占的空间都会比较大,这样的话我们在传输文件的时候时间会很长不太方便.那我们可以将PDF文件进行压缩变小这样可以大大提高我们的工作效率.那PDF文件过 ...

  7. 免费的PDF转换工具,简单高效有它就够了!

    对PDF软件进行处理转换,相信经常进行文件办公的朋友们并不陌生.很多时候我们想要留住PDF部分文件内容,或者直接对其进行修饰和改动,要么是操作复杂的,要么是花费时间的.这时候小编不得不给大家推荐一种办 ...

  8. 计算机自带pdf吗,电脑自带pdf转换工具哪个好?办公必备!

    大家都知道 ,由于互联网上的各种信息基本上只能通过微信.qq等电子邮件发给我们 .因此很多人会把文件格式转换为pp演文件以及将其他类型的文件另存为或直接保存为ppt .那么 ,电脑自带pdf转换工具哪 ...

  9. 最好的在线PDF转换工具服务

    工作中有时候会碰到需要转换PDF文件的情况,现在网上就要很多免费的在线工具,可以进行PDF文件的转换,下面就来介绍一些可以直接在浏览器中将文档.电子表格.和图片转换为PDF或者互相转换的服务工具. 原 ...

最新文章

  1. PHP 中 include 和 require 的概要及区别介绍
  2. python利器下载-Python打包利器:auto-py-to-exe
  3. python最难学的是什么_python是最难学的语言吗
  4. 如果重力对人的意识有影响
  5. linux 内核配置raid,在 Linux VM 上配置软件 RAID - Azure Virtual Machines | Microsoft Docs
  6. VNC介绍(第一篇)
  7. 下拉加载更多--判断页面距离
  8. AcWing285. 没有上司的舞会(树形DP)题解
  9. OpenCV之图像二值化与去噪
  10. 冰点还原精灵怎么卸载
  11. 重装战姬电脑版模拟器怎么玩
  12. 【立青】安装python及各种数据包
  13. 笔记:全网最详细jQuery教程
  14. 最全的关于硬件测试的解读
  15. 用matlab画椭圆曲线方程,椭圆曲线入门详解
  16. 超低功耗MCU 的网站
  17. Ubuntu 14.04 安装配置 JDK8
  18. tcl文件调用c语言,TCL与c/c++的互相调用
  19. 肌酐研究之Biovision肌酐合成试剂盒
  20. Diskgenius分区简单教程

热门文章

  1. 华为手机广告“来自iPhone客户端”
  2. 南卡和华为蓝牙耳机哪个好?国产蓝牙耳机南卡对比华为!
  3. Codeforces Round #697 (Div. 3) (Unrated for Div. 3)全部题解
  4. 《潜入》FLASH大游戏 终于完工
  5. 冲上热搜!华为30岁以下员工仅占28%
  6. 分享104个PHP源码,总有一款适合您
  7. 跟卖又被“抄家”了,亚马逊新手要怎么做?
  8. 饥荒机器人升级上限多少_饥荒机器人升满级之后
  9. 饥荒服务器怎么显示帧数,饥荒的fps值怎么显示 | 手游网游页游攻略大全
  10. hbase 建的表删除不掉的问题,已成功解决,经验分享