java讀取pdf文本轉換html

補充:一下代碼基於maven,現將依賴的jar包單獨導出css

地址:pdf jarhtml

 

完整代碼地址 也就兩個文件java

 

 java讀取pdf中的純文字,這裏使用的是pdfbox工具包git

maven引入以下配置github

     <dependency>
            <groupId>net.sf.cssbox</groupId>
            <artifactId>pdf2dom</artifactId>
            <version>1.7</version>
        </dependency>
        <dependency>
            <groupId>org.apache.pdfbox</groupId>
            <artifactId>pdfbox</artifactId>
            <version>2.0.12</version>
        </dependency>
        <dependency>
            <groupId>org.apache.pdfbox</groupId>
            <artifactId>pdfbox-tools</artifactId>
            <version>2.0.12</version>
        </dependency>

工具類直接讀取spring

代碼示例apache

  /* 讀取pdf文字 */ @Test public void readPdfTextTest() throws IOException { byte[] bytes = getBytes("D:\\code\\pdf\\HashMap.pdf"); //加載PDF文檔
        PDDocument document = PDDocument.load(bytes); readText(document); } public void readText(PDDocument document) throws IOException { PDFTextStripper stripper = new PDFTextStripper(); String text = stripper.getText(document); System.out.println(text); }

將pdf轉換爲html數組

效果圖springboot

 代碼示例mybatis

/* pdf轉換html */ @Test public void pdfToHtmlTest() { String outputPath = "D:\\code\\pdf\\HashMap.html"; byte[] bytes = getBytes("D:\\code\\pdf\\HashMap.pdf"); // try() 寫在()裏面會自動關閉流
        try (BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(outputPath)),"UTF-8"));){ //加載PDF文檔
            PDDocument document = PDDocument.load(bytes); PDFDomTree pdfDomTree = new PDFDomTree(); pdfDomTree.writeText(document,out); } catch (Exception e) { e.printStackTrace(); } } /* 將文件轉換爲byte數組 */
    private byte[] getBytes(String filePath){ byte[] buffer = null; try { File file = new File(filePath); FileInputStream fis = new FileInputStream(file); ByteArrayOutputStream bos = new ByteArrayOutputStream(1000); byte[] b = new byte[1000]; int n; while ((n = fis.read(b)) != -1) { bos.write(b, 0, n); } fis.close(); bos.close(); buffer = bos.toByteArray(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return buffer; }

完整的一個上傳pdf轉換爲HTML功能(從此轉換pdf也不須要找什麼第三方了,哈哈)

@RequestMapping("ud") @Controller public class UpAndDownController { @RequestMapping("upload.do") @ResponseBody public Map<String,Object> upload(@RequestParam("file") MultipartFile file, HttpServletRequest request){ Map<String, Object> map = new HashMap<>(); map.put("code","200"); try { PdfConvertUtil pdfConvertUtil = new PdfConvertUtil(); String pdfName = file.getOriginalFilename(); int lastIndex = pdfName.lastIndexOf(".pdf"); String fileName = pdfName.substring(0, lastIndex); String htmlName = fileName + ".html"; String realPath = ResourceUtils.getURL("classpath:").getPath() + "/templates/file"; File f = new File(realPath); if(!f.exists()){ f.mkdirs(); } String htmlPath = realPath + "\\" + htmlName; pdfConvertUtil.pdftohtml(file.getBytes(), htmlPath); } catch (Exception e) { map.put("code","500"); e.printStackTrace(); } return map; } }

可使用postman調試

須要設置請求頭 Content-Type 指定爲 application/x-www-form-urlencoded

以後選擇body選擇form-data,OK

 

若是涉及到HTML頁面直接加載PDF,無需插件

能夠參考下 

http://www.javashuo.com/article/p-oejmzdur-hy.html

https://github.com/mozilla/pdf.js

相關文章
相關標籤/搜索