java实现pdf转图片

java 同时被 2 个专栏收录
3 篇文章 0 订阅
1 篇文章 0 订阅

java实现pdf转图片
public class BMTTask extends Task{

private  String pdfHttp = "http://121.194.3.209/";
//apache 基础路径
private  String apacheUrl = "http://localhost:9999/images/";
//存储基本路径
private String basePath = "D:\\TRS\\httpd-2.4.37-o110j-x64-vc14-r2\\Apache\\htdocs\\images\\";
//版面图redis key
private  String BMTKey = "BMT";
// 海贝url
private   String hybaseUrl = "http://121.194.3.209/";
private  String  username = "admin";
private   String password = "trsadmin";

public static void main(String[] args ){
    new DebugTask().run(-1);
}


@Override
public void work() {
    try{
        getBMT();
    }catch (Exception e){
        e.printStackTrace();
    }

}

public void getBMT() throws IOException {

// 获取配置文件
InputStream in = new Object().getClass().getResourceAsStream("/config.properties");
Properties properties = new Properties();
properties.load(in);
System.out.println(properties);
hybaseUrl = (String) properties.get(“hybaseurl”);
username = (String) properties.get(“hybaseuserid”);
password = (String) properties.get(“hybasepassword”);
apacheUrl = (String) properties.get(“apacheUrl”);
pdfHttp = (String) properties.get(“pdfHttp”);
basePath = (String) properties.get(“basePath”);
BMTKey = (String) properties.get(“BMTKey”);
try {
// 加载配置文件
properties.load(in);
// 获取海贝连接

        TRSConnection connection = new TRSConnection(hybaseUrl
                ,username,password,new ConnectParams());
        info("连接海贝--"+connection);

// 获取海贝结果集
String today = DateUtil.getHybaseToday();
TRSResultSet trsResultSet = connection.executeSelect(“mlf_product_formal_20160511”,
“PDPATH:group1* AND DOCPUBTIME:[”+today+" TO *]", 0, 1000, new SearchParams());
info(“获取结果集–”+trsResultSet);
// 获取所有数据
ArrayList<Map<String,Object>> rows = new ArrayList<>();
while (trsResultSet.moveNext()) {
TRSRecord trsRecord = trsResultSet.get();
String[] columnNames = trsRecord.getColumnNames();
Map<String, Object> row = new LinkedHashMap<>();
row.put(“UID”, trsRecord.getUid());
for (String columnName : columnNames) {
String value = trsRecord.getString(columnName);
row.put(columnName, value);
}
rows.add(row);
}
info(“版面图数据量”+rows.size());
// 保存栏目周刊
Map<String,String> imageMap = new HashMap<>();
// List imageList = new ArrayList<>();
List list = new ArrayList<>();
for (Map<String, Object> row : rows) {
String docchannel = (String) row.get(“DOCCHANNEL”);
if (list.contains(docchannel)){
continue;
}
list.add(docchannel);
String pdfpath = (String) row.get(“PDPATH”);
String documentid = (String) row.get(“DOCUMENTID”);
// 生成图片
createImage(pdfpath,docchannel,imageMap);
}
// 存储redis
String json = JSON.toJSONString(imageMap);
RedisUtil.getRedisUtil().set(BMTKey,json);
info(“存入redis成功…”);

    } catch (Exception e) {
        e.printStackTrace();
    }
}

public void createImage(String pdfpath,String docchannel, Map<String,String> imageMap) throws IOException {
    HttpURLConnection connection = (HttpURLConnection) new URL(pdfHttp+pdfpath).openConnection();
    info("获取pdf..");

// FileURLConnection connection = new FileURLConnection(“file:///D:/pdf/1.pdf”);
connection.setReadTimeout(5000);
connection.setConnectTimeout(5000);
// connection.setRequestMethod(“GET”);
InputStream in = null;
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
in = connection.getInputStream();
}
PDFParser parser = new PDFParser(in);
parser.parse();
PDDocument doc = parser.getPDDocument();
// PDDocument doc = PDDocument.load(“D:/pdf/1.pdf”);
int pageCount = doc.getPageCount();
System.out.println(pageCount);
List pages = doc.getDocumentCatalog().getAllPages();
info(“开始生成版面图…”+docchannel);
for (int i = 0; i < pages.size(); i++) {
PDPage page = (PDPage) pages.get(i);
BufferedImage image = page.convertToImage();
Iterator iter = ImageIO.getImageWritersBySuffix(“jpg”);
ImageWriter writer = (ImageWriter) iter.next();
File outFile = new File(basePath
+ docchannel + “.jpg”);
FileOutputStream out = new FileOutputStream(outFile);
ImageOutputStream outImage = ImageIO
.createImageOutputStream(out);
writer.setOutput(outImage);
writer.write(new IIOImage(image, null, null));
info(“生成成功…”);
imageMap.put(docchannel,apacheUrl+outFile.getName());
outImage.close();
out.close();
page.clear();
doc.close();
}
}
}

jar包

百度云jar包连接:https://pan.baidu.com/s/14MjecKZPflISyB5Lhc7Qrw

  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值