跳至主要內容

pdf转html

三思原创大约 1 分钟javajava文档处理

该代码实现了将PDF文件转换为H ML格式的功能,通过PDFDom ree工具实现解析,支持指定起始页和结束页。用户可以自定义配置,包括图片处理、字体处理等。最终生成的H ML文件可用于展示PDF内容,并支持保存到指定目录。

image
image

pdf转html

package com.neusoft.sl.ehrss.base.si.matter.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.io.Writer;

import javax.xml.parsers.ParserConfigurationException;

import org.apache.commons.io.FileUtils;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.fit.pdfdom.PDFDomTree;
import org.fit.pdfdom.PDFDomTreeConfig;
public class PdfToHtml {
  public static String parseWithPdfDomTree(InputStream is, int startPage, int endPage, PDFDomTreeConfig config)
          throws IOException, ParserConfigurationException {
    PDDocument pdf = PDDocument.load(is);
    PDFDomTree parser = new PDFDomTree(config);
    parser.setStartPage(startPage);
    parser.setEndPage(endPage);
    Writer output = new StringWriter();
    parser.writeText(pdf, output);
    pdf.close();
    String htmlOutput = output.toString();
    return htmlOutput;
  }


  public static void main(String[] arges) {
    PDFDomTreeConfig config = PDFDomTreeConfig.createDefaultConfig();
    config.setImageHandler(PDFDomTreeConfig.saveToDirectory(new File("E:\\")));
    config.setFontHandler(config.getImageHandler());
    try {
      String html = parseWithPdfDomTree(new FileInputStream("E:\\eee.pdf"), 0, 10, config);
      FileUtils.write(new File("E:\\test.html"), html, "utf-8");
    } catch (IOException e) {
      throw new RuntimeException(e);
    } catch (ParserConfigurationException e) {
      throw new RuntimeException(e);
    }

  }
}