获取http的pdf文件并存到本地转为img
2016-10-14 14:51
423 查看
获取http的pdf文件并存到本地转为img,该方法是为了打印使用。因为市面上大部分打印插件无法直接将pdf扔到设备上打印,需要二次操作。从而影响了效率,为此转存为img,利用lodop插件img的base64编码打印。从而可以实现连打功能。
package com.fx.wms.common.util; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.URL; import javax.imageio.ImageIO; import org.apache.commons.codec.binary.Base64; import org.apache.commons.io.FileUtils; import org.jpedal.PdfDecoder; import org.jpedal.exception.PdfException; import org.jpedal.fonts.FontMappings; public class JPedal { public static void main(String[] args) throws IOException, PdfException,InterruptedException { /**instance of PdfDecoder to convert PDF into image*/ PdfDecoder decode_pdf = new PdfDecoder(true); /**set mappings for non-embedded fonts to use*/ FontMappings.setFontReplacements(); /**open the PDF file - can also be a URL or a byte array*/ try { decode_pdf.openPdfFile("C:/Users/oftoo/pdf/EV930739775CN.pdf"); //file //decode_pdf.openPdfFile("C:/myPDF.pdf", "password"); //encrypted file //decode_pdf.openPdfArray(bytes); //bytes is byte[] array with PDF //decode_pdf.openPdfFileFromURL("http://www.mysite.com/myPDF.pdf",false); /**get page 1 as an image*/ //page range if you want to extract all pages with a loop int start = 1, end = decode_pdf.getPageCount(); for(int i=start;i<end+1;i++){ BufferedImage img=decode_pdf.getPageAsImage(i); ImageIO.write(img, "png", new File("C:/Users/oftoo/pdf/"+i+"123.png")); } /**close the pdf file*/ decode_pdf.closePdfFile(); } catch (PdfException e) { e.printStackTrace(); } System.out.println("22222222222"); } /** * * @Title: PdftoImage * @Description: TODO(本地的pdf文件转img) * @param @param urlPath * @param @param savePath * @param @return * @param @throws IOException * @param @throws PdfException * @param @throws InterruptedException 设定文件 * @return boolean 返回类型 * @throws * @author:oftoo */ public boolean PdftoImage(String urlPath, String savePath)throws IOException, PdfException, InterruptedException{ /**instance of PdfDecoder to convert PDF into image*/ PdfDecoder decode_pdf = new PdfDecoder(true); /**set mappings for non-embedded fonts to use*/ FontMappings.setFontReplacements(); /**open the PDF file - can also be a URL or a byte array*/ try { decode_pdf.openPdfFile(urlPath); //file //decode_pdf.openPdfFile("C:/myPDF.pdf", "password"); //encrypted file //decode_pdf.openPdfArray(bytes); //bytes is byte[] array with PDF //decode_pdf.openPdfFileFromURL("http://www.mysite.com/myPDF.pdf",false); /**get page 1 as an image*/ //page range if you want to extract all pages with a loop int start = 1, end = decode_pdf.getPageCount(); for(int i=start;i<end+1;i++){ BufferedImage img=decode_pdf.getPageAsImage(i); ImageIO.write(img, "png", new File(savePath+"_i"+".png")); } /**close the pdf file*/ decode_pdf.closePdfFile(); } catch (PdfException e) { e.printStackTrace(); return false; } return true; } /** * * @Title: savePdf * @Description: TODO(获取http的pdf文件保存到本地路径) * @param @param url * @param @param savePath * @param @return 设定文件 * @return boolean 返回类型 * @throws * @author:oftoo */ public boolean savePdf(String url,String savePath){ boolean res = downloadFromUrl("http://baidu.com/xxx.pdf","d:/"); return res; } public static String getFileNameFromUrl(String url){ String name = new Long(System.currentTimeMillis()).toString() + ".X"; int index = url.lastIndexOf("/"); if(index > 0){ name = url.substring(index + 1); if(name.trim().length()>0){ return name; } } return name; } public boolean downloadFromUrl(String url,String dir) { try { URL httpurl = new URL(url); String fileName = getFileNameFromUrl(url); System.out.println(fileName); File f = new File(dir + fileName); FileUtils.copyURLToFile(httpurl, f); } catch (Exception e) { e.printStackTrace(); return false; } return true; } /** * * @Title: GetImageStr * @Description: TODO(将图片文件转化为字节数组字符串,并对其进行Base64编码处理 ) * @param @param imgFilePath * @param @return 设定文件 * @return String 返回类型 * @throws * @author:oftoo */ public static String GetImageStr(String imgFilePath) {// 将图片文件转化为字节数组字符串,并对其进行Base64编码处理 byte[] data = null; // 读取图片字节数组 try { InputStream in = new FileInputStream(imgFilePath); data = new byte[in.available()]; in.read(data); in.close(); } catch (IOException e) { e.printStackTrace(); } // 对字节数组Base64编码 //BASE64Encoder encoder = new BASE64Encoder(); return new String(Base64.encodeBase64(data));// 返回Base64编码过的字节数组字符串 } /** * * @Title: GenerateImage * @Description: TODO(对字节数组字符串进行Base64解码并生成图片 ) * @param @param imgStr * @param @param imgFilePath * @param @return 设定文件 * @return boolean 返回类型 * @throws * @author:oftoo */ public static boolean GenerateImage(String imgStr, String imgFilePath) {// 对字节数组字符串进行Base64解码并生成图片 if (imgStr == null) // 图像数据为空 return false; try { // Base64解码 byte[] bytes = Base64.decodeBase64(imgStr); for (int i = 0; i < bytes.length; ++i) { if (bytes[i] < 0) {// 调整异常数据 bytes[i] += 256; } } // 生成jpeg图片 OutputStream out = new FileOutputStream(imgFilePath); out.write(bytes); out.flush(); out.close(); return true; } catch (Exception e) { return false; } } }
相关文章推荐
- 如何将本地的文件变成http请求获取
- http获取网络资源文件等并保存到本地sd或者磁盘
- http获取网络资源文件等并保存到本地sd或者磁盘
- flex4 RIA 获取本地图片文件并加载给img控件
- HttpWebRequest 请求PDF文件,并下载到本地
- Powershell实现从注册表获取本地关联文件的扩展名
- 关于Android7.0版本及其以上系统获取本地数据(图片和文件)报系统出现异常,正在退出问题
- 获取本地视频文件的缩略图
- HttpContext是干什么的; asp.net 获取网站路径;DataSet读取xml文件;updatepanel 控件的局部刷新 always和condition
- Struts2中文件上传和多文件上传以及获取HttpServletRequest / HttpSession / ServletContext / HttpServletRespons
- 如何把SWF转为PDF文件
- 让Chrome支持Ajax/$http方式读取本地文件
- 如何把本地 PDF 文件添加到 iOS 的 iBooks
- 在服务器中发布IIS服务,挡在本地iis网站访问服务时候经常出现文件无法获取
- js/jquery 获取本地文件的文件路劲 获取input框中type=‘file’ 中的文件路径
- 网站开发之ie下在线浏览pdf文件无需本地支持
- Python3.4 获取百度网页源码并保存在本地文件中
- Android 如何本地加载pdf文件
- Windows Phone 本地数据库创建、获取数据库物理文件、数据库引用---本地数据库创建
- php获取本地图片文件并生成xml文件输出