实操可行!通过源码安装pdf2htmlEX

组里让调研将pdf转html的工具,在网上搜索了一圈,发现pdf2htmlEX这个工具能较好地实现这个功能(项目地址:https://github.com/coolwanglu/pdf2htmlEX),但美中不足的是,pdf2htmlEX项目的时间太过久远了,作者提供的安装脚本中有许多的依赖已经无法下载,而且这个项目也已经不再维护了。所幸的是,有其他大佬基于原始的pdf2htmlEX代码分支在继续维护着这个工具(项目地址:https://github.com/pdf2htmlEX/pdf2htmlEX),在后者的基础上,成功安装了pdf2htmlEX。为了安装这个工具,前后花了很多时间,疯狂踩坑,处理各种包的依赖报错更是让人头大,但好在还是安装成功了,觉得实在有必要复盘记录下来,防止更多的人踩坑。

源码安装

首先下载源码到服务器上:

git clone https://github.com/pdf2htmlEX/pdf2htmlEX.git

作者提供了不同Linux发行版对应的安装脚本,我使用的是Debian系统,所以使用buildInstallLocallyApt这个脚本。执行脚本:

sh buildScripts/buildInstallLocallyApt

运气好的话,只需要静静等待脚本执行完就行了。但是,通常情况下并不会一帆风顺,所以要对脚本的内容做一些修改。安装过程可以大致分为以下四步:

  • 安装依赖

    getBuildToolsApt

    sudo apt-get update
    sudo apt-get $UNATTENDED install \
      sudo                           \
      wget                           \
      git                            \
      pkg-config                     \
      ruby                           \
      autoconf                       \
      libtool                        \
      cmake                          \
      make                           \
      gcc                            \
      g++                            \
      dpkg                           \
      dpkg-dev                       \
      gettext                        \
    # openjdk-8-jre-headless         \
      jq 
    # openjdk-8-jre-headless由于没有合适的源已经无法下载了,可以用Oraclejdk代替。其他的包安装出现问题,可以通过换源解决
    
  • 安装poppler

    getPoppler脚本中下载了poppler-0.89.0.tar.xzpoppler-data-0.4.9.tar.gz两个文件,由于网络的原因,建议提前下载好上传到服务器,然后对脚本做如下修改:

    # rm -rf $POPPLER_VERSION.tar.xz
    # rm -rf poppler
    # rm -rf poppler-data-0.4.9.tar.gz
    # rm -rf poppler-data
    
    set -ev
    
    # wget https://poppler.freedesktop.org/$POPPLER_VERSION.tar.xz
    
    tar xvf $POPPLER_VERSION.tar.xz
    
    echo "Getting poppler-data version: 0.4.9"
    
    mv $POPPLER_VERSION poppler
    
    # wget https://poppler.freedesktop.org/poppler-data-0.4.9.tar.gz
    
    tar xvf poppler-data-0.4.9.tar.gz
    
    mv poppler-data-0.4.9 poppler-data
    
  • 安装fontforge

    getFontforge中下载了20220308.tar.gz,同样提前下载好上传

    FONTFORGE_SRC=$FONTFORGE_VERSION.tar.gz
    
    # rm -rf $FONTFORGE_SRC
    # rm -rf fontforge
    
    # wget https://github.com/fontforge/fontforge/archive/$FONTFORGE_SRC
    
    tar xvf $FONTFORGE_SRC
    
    mv fontforge-$FONTFORGE_VERSION fontforge
    
  • 安装pdf2htmlEX

    这部分脚本不做修改。当安装完成后,执行pdf2htmlEX -v,如果正常输出版本信息,则安装成功

代码调用

使用pdf2htmlEX --dest-dir [输出路径] [输入路径]命令将pdf转换为html格式,在java中通过Runtime类实现调用。

    /**
     * @param inputFile 输入文件 
     * @param output 输出路径
     * @return
     */
    private void convertPdf(String inputFile, String output) throws IOException {
        String command = String.format("pdf2htmlEX --dest-dir %s %s", output, inputFile);
        Process process = Runtime.getRuntime().exec(command);
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
            process.waitFor();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

参数

pdf2htmlEX有许多可用参数,使转换效果更加灵活

  -f,--first-page <int>         first page to convert (default: 1)
  -l,--last-page <int>          last page to convert (default: 2147483647)
  --zoom <fp>                   zoom ratio
  --fit-width <fp>              fit width to <fp> pixels
  --fit-height <fp>             fit height to <fp> pixels
  --use-cropbox <int>           use CropBox instead of MediaBox (default: 1)
  --hdpi <fp>                   horizontal resolution for graphics in DPI (default: 144)
  --vdpi <fp>                   vertical resolution for graphics in DPI (default: 144)
  --embed <string>              specify which elements should be embedded into output
  --embed-css <int>             embed CSS files into output (default: 1)
  --embed-font <int>            embed font files into output (default: 1)
  --embed-image <int>           embed image files into output (default: 1)
  --embed-javascript <int>      embed JavaScript files into output (default: 1)
  --embed-outline <int>         embed outlines into output (default: 1)
  --split-pages <int>           split pages into separate files (default: 0)
  --dest-dir <string>           specify destination directory (default: ".")
  --css-filename <string>       filename of the generated css file (default: "")
  --page-filename <string>      filename template for split pages  (default: "")
  --outline-filename <string>   filename of the generated outline file (default: "")
  --process-nontext <int>       render graphics in addition to text (default: 1)
  --process-outline <int>       show outline in HTML (default: 1)
  --process-annotation <int>    show annotation in HTML (default: 0)
  --process-form <int>          include text fields and radio buttons (default: 0)
  --printing <int>              enable printing support (default: 1)
  --fallback <int>              output in fallback mode (default: 0)
  --tmp-file-size-limit <int>   Maximum size (in KB) used by temporary files, -1 for no limit. (default: -1)
  --embed-external-font <int>   embed local match for external fonts (default: 1)
  --font-format <string>        suffix for embedded font files (ttf,otf,woff,svg) (default: "woff")
  --decompose-ligature <int>    decompose ligatures, such as 'fi' -> 'f''i'

小结

除了源码安装外,pdf2htmlEX还可以通过docker安装,如果不是很闲的话,建议还是使用docker,毕竟源码安装太费时间了。但是使用java去调用docker感觉比较麻烦,而且还需要多部署一个容器,综合考虑还是使用了源码安装的方式。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值