赞
踩
下载当前系统对应的安装包,中文简体安装包,官方下载地址;
下载后的安装包:
配置好的安装包:
链接:https://pan.baidu.com/s/1XCQUbNwjF_KqYL-Tqpy5lw
提取码:s462
假设安装路径为:D:\pdftool\xpdf
1、解压xpdf-tools-win-4.03.zip,并将bin64目录下所有文件和doc目录下的 sample-xpdfrc 复制到 D:\pdftool\xpdf目录;
2、将 sample-xpdfrc 改名为xpdfrc(默认配置名称为xpdfrc);
3、解压xpdf-chinese-simplified.tar.gz和xpdf-t1fonts.tar.gz,并复制到 D:\pdftool\xpdf目录;
4、修改xpdfrc配置:
找到textEncoding UTF-8,将注释打开;
并在下面增加 textPageBreaks no 参数,表示pdf文档的两页间不加入分行符。
5、在最后面增加内容:
#------ 配置字体文件 ----
fontFile Symbol D:/pdftool/xpdf/xpdf-t1fonts/s050000l.pfb
fontFile ZapfDingbats D:/pdftool/xpdf/xpdf-t1fonts/d050000l.pfb
#----- begin Chinese Simplified support package (2011-sep-02)
cidToUnicode Adobe-GB1 D:/tool/xpdf/xpdf-chinese-simplified/Adobe-GB1.cidToUnicode
unicodeMap ISO-2022-CN D:/tool/xpdf/xpdf-chinese-simplified/ISO-2022-CN.unicodeMap
unicodeMap EUC-CN D:/tool/xpdf/xpdf-chinese-simplified/EUC-CN.unicodeMap
unicodeMap GBK D:/tool/xpdf/xpdf-chinese-simplified/GBK.unicodeMap
cMapDir Adobe-GB1 D:/tool/xpdf/xpdf-chinese-simplified/CMap
toUnicodeDir D:/tool/xpdf/xpdf-chinese-simplified/CMap
#displayCIDFontTT Adobe-GB1 D:/xpdf/xpdf-chinese-simplified/CMap/gkai00mp.ttf
#fontFileCC Adobe-GB1 /usr/..../gkai00mp.ttf
#----- end Chinese Simplified support package
1、解压xpdf-tools-linux-4.03.tar.gz,并将bin64目录下所有文件和doc目录下的 sample-xpdfrc 复制到 /usr/local/bin/xpdf目录;
2、将 sample-xpdfrc 改名为xpdfrc(默认配置名称为xpdfrc);
3、解压xpdf-chinese-simplified.tar.gz和xpdf-t1fonts.tar.gz,并复制到 /usr/local/bin/xpdf目录;
4、修改xpdfrc配置:
找到textEncoding UTF-8,将注释打开,指定编码为UTF-8,并在下面增加 textPageBreaks no 参数,表示pdf文档的两页间不加入分行符。
5、在最后面增加内容:(相关配置本人根据需要已做过调整)
#------ 配置字体文件 ---- fontFile Symbol /usr/local/bin/xpdf/fonts/s050000l.pfb fontFile ZapfDingbats /usr/local/bin/xpdf/fonts/d050000l.pfb #fontFile MicrosoftYaHei /usr/local/bin/xpdf/fonts/generalFonts/MicrosoftYaHei.ttf fontFile STSongStd-Light-UniGB-UCS2-H /usr/local/bin/xpdf/fonts/generalFonts/STSong-Light-UniGB-UCS2-H.ttf fontFile TimesNewRomanPSMT /usr/local/bin/xpdf/fonts/generalFonts/TimesNewRomanPSMT.otf # 字体包(可从上面百度网盘下载) fontDir /usr/local/bin/xpdf/fonts/generalFonts #----- begin Chinese Simplified support package (2011-sep-02) cidToUnicode Adobe-GB1 /usr/local/bin/xpdf/xpdf-chinese-simplified/Adobe-GB1.cidToUnicode unicodeMap ISO-2022-CN /usr/local/bin/xpdf/xpdf-chinese-simplified/ISO-2022-CN.unicodeMap unicodeMap EUC-CN /usr/local/bin/xpdf/xpdf-chinese-simplified/EUC-CN.unicodeMap unicodeMap GBK /usr/local/bin/xpdf/xpdf-chinese-simplified/GBK.unicodeMap cMapDir Adobe-GB1 /usr/local/bin/xpdf/xpdf-chinese-simplified/CMap toUnicodeDir /usr/local/bin/xpdf/xpdf-chinese-simplified/CMap #displayCIDFontTT Adobe-GB1 /usr/local/bin/xpdf/xpdf-chinese-simplified/CMap/gkai00mp.ttf #fontFileCC Adobe-GB1 /usr/..../gkai00mp.ttf #----- end Chinese Simplified support package
D:\pdftool\xpdf\pdftopng.exe -f 1 -l 2 D:\temp\test.pdf D:\temp\testpng
解释:
-f 1 ----- 从第1页开始转换。
-l 2 ----- 转换至第2页结束,
D:\temp\test.pdf ----- 待转换的pdf文件
D:\temp\testpng ----- 生成的图片名前缀叫testpng 最终会以testpng-000001.png的形式生成在D:\temp\目录下
其他参数解释可执行D:\pdftool\xpdf\pdftopng.exe 查看
cd /usr/local/bin/xpdf
需要给予执行权限:chomd 777 chmod 777 pdf*
./pdftopng -f 1 -l 1 /tmp/test.pdf /tmp/testpng
解释:
-f 1 是指从第一页开始。
-l 1 是指第一页结束,
/tmp/testpng是指生成的图片名前缀叫testpng 最终会以testpng-000001.png的形式生成在/tmp/目录下
其他参数解释可执行./pdftopng 查看
import org.apache.commons.lang.StringUtils; import java.io.*; import java.util.List; /** * PDF转换工具类(xpdf : https://www.xpdfreader.com/download.html) * * * @Classname PDFConvert * @Description * @Date 2022/1/4 8:59 * @Created by zhanyp */ public class PDFConvertUtils { /** * widows xpdf 安装路径 */ private static String win_path = "D:\\pdftool\\xpdf"; private static String linux_path = "/usr/local/bin/xpdf"; /** * -f 1 :第一页开始; * -l 1 :第一页结束; */ private static String PDF_TO_PNG_CMD = "%s -f %s -l %s %s %s"; /** * windows环境下pdf转png * * @Auther zhanyp * @Date 2022/1/4 * @Param toolPath : xpdf安装目录 例如D:\pdftool\xpdf * @Param sourcePath : 源pdf文件 例如 D:\test.pdf * @Param targetPath : 目标png保存路径 D:\pdf\png * @Param pngName ;目标png图片名称 例如:test * @Param startPageNum : 开始页数 例如 1, 从第一页开始 * @Param endPageNum : 结束页数 例如 3,第三页结束 * @Return */ public static void pdfToPngWin(String toolPath, String sourcePath, String targetPath, String pngName, Integer startPageNum, Integer endPageNum) { if (StringUtils.isBlank(toolPath)) { toolPath = win_path; } File dir = new File(targetPath); if (!dir.exists() && !dir.isDirectory()) { dir.mkdirs(); } String targetName = targetPath + File.separator + pngName; String command = String.format(PDF_TO_PNG_CMD, toolPath + File.separator + "pdftopng.exe", startPageNum, endPageNum, sourcePath, targetName); System.out.println("执行命令:" + command); try { Runtime.getRuntime().exec(command); System.out.println("pdfToPng 成功"); } catch (IOException e) { System.out.println("pdfToPng 失败"); e.printStackTrace(); } } /** * linux环境下pdf转png * * @Auther zhanyp * @Date 2022/1/4 * @Param toolPath : xpdf安装目录 例如/usr/local/bin/xpdf * @Param sourcePath : 源pdf文件 例如 /tmp/pdftopng/a.pdf * @Param targetPath : 目标png保存路径 /tmp/pdftopng * @Param pngName ;目标png图片名称 例如:test * @Param startPageNum : 开始页数 例如 1, 从第一页开始 * @Param endPageNum : 结束页数 例如 3,第三页结束 * @Return */ public static void pdfToPngLinux(String toolPath, String sourcePath, String targetPath, String pngName, Integer startPageNum, Integer endPageNum) { if (StringUtils.isBlank(toolPath)) { toolPath = linux_path; } File dir = new File(targetPath); if (!dir.exists() && !dir.isDirectory()) { dir.mkdirs(); } String targetName = targetPath + File.separator + pngName; String command = String.format(PDF_TO_PNG_CMD, "./pdftopng", startPageNum, endPageNum, sourcePath, targetName); System.out.println("执行命令:" + command); Runtime run = Runtime.getRuntime(); Process proc = null; BufferedReader in = null; PrintWriter out = null; try { proc = run.exec("/bin/bash", null, null); in = new BufferedReader(new InputStreamReader(proc.getInputStream())); out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(proc.getOutputStream())), true); out.println("cd " + toolPath); out.println(command); // out.println("cd /home/test"); // out.println("pwd"); // out.println("rm -fr /home/proxy.log"); out.println("exit");// 这个命令必须执行,否则in流不结束。 proc.waitFor(); } catch (Exception e) { System.out.println("pdf转图片异常:"); e.printStackTrace(); } finally { try { if (in != null) { in.close(); } if (out != null) { out.close(); } if (proc != null) { proc.destroy(); } } catch (Exception e){ } } } public static void main(String[] args) { long start = System.currentTimeMillis(); pdfToPngWin(win_path, "D:\\pdftool\\test\\a.pdf", "D:\\pdftool\\test", "aa", 1, 2); System.out.println(System.currentTimeMillis() - start); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。