Ubuntu 服务器 LibreOffice 转换为 txt 错误

Ubuntu 服务器 LibreOffice 转换为 txt 错误

运行 headless(自服务器 14.04 以来的唯一选项)尝试将 .doc 文件转换为 .txt,以便能够被 php 提取。运行

soffice --headless --convert-to txt test.doc

生成一个 test.txt 文件,其内容如下:

504b 0304 1400 0008 0000 0313 2247 9f03
2ec4 2b00 0000 2b00 0000 0800 0000 6d69
6d65 7479 7065 6170 706c 6963 6174 696f
6e2f 766e 642e 6f61 7369 732e 6f70 656e
...
0000 c1a3 0000 4d45 5441 2d49 4e46 2f6d
616e 6966 6573 742e 786d 6c50 4b05 0600
0000 000e 000e 0094 0300 0007 a500 0000
00

转换为 .pdf 就可以了。

我猜是缺少了一个库...但不知道从哪里开始。我直接安装了 libreoffice...apt-get install libreoffice

在 Mac 上使用 libreoffice 运行相同的文件效果很好,所以我可以排除任何来源文件类型问题。

有人有什么想法吗?

答案1

从初始字节序列504b 0304可以推断出输出是zip档案的形式。

事实上,使用最小.doc文件进行的简单测试表明,--convert-to txt单独指定结果将产生一个开放文档文本文件 - 但带有.txt扩展名:

:~$ soffice --headless --convert-to txt testdoc.doc
convert /home/steeldriver/testdoc.doc -> /home/steeldriver/testdoc.txt using 
Overwriting: /home/steeldriver/testdoc.txt

:~$ file testdoc.txt
testdoc.txt: OpenDocument Text

:~$ zipinfo testdoc.txt
Archive:  testdoc.txt
Zip file size: 8764 bytes, number of entries: 15
-rw----     2.0 fat       39 b- stor 15-Sep-02 15:58 mimetype
-rw----     2.0 fat     1136 b- stor 15-Sep-02 15:58 Thumbnails/thumbnail.png
-rw----     2.0 fat     4065 bl defN 15-Sep-02 15:58 content.xml
-rw----     2.0 fat     8849 bl defN 15-Sep-02 15:58 settings.xml
-rw----     2.0 fat      894 bl defN 15-Sep-02 15:58 meta.xml
-rw----     2.0 fat    14890 bl defN 15-Sep-02 15:58 styles.xml
-rw----     2.0 fat      899 bl defN 15-Sep-02 15:58 manifest.rdf
-rw----     2.0 fat        0 b- stor 15-Sep-02 15:58 Configurations2/toolpanel/
-rw----     2.0 fat        0 b- stor 15-Sep-02 15:58 Configurations2/progressbar/
-rw----     2.0 fat        0 b- stor 15-Sep-02 15:58 Configurations2/floater/
-rw----     2.0 fat        0 b- stor 15-Sep-02 15:58 Configurations2/statusbar/
-rw----     2.0 fat        0 b- stor 15-Sep-02 15:58 Configurations2/toolbar/
-rw----     2.0 fat        0 b- stor 15-Sep-02 15:58 Configurations2/popupmenu/
-rw----     2.0 fat        0 b- stor 15-Sep-02 15:58 Configurations2/menubar/
-rw----     2.0 fat      978 bl defN 15-Sep-02 15:58 META-INF/manifest.xml
15 files, 31750 bytes uncompressed, 6938 bytes compressed:  78.1%

为了强制纯文本转换,需要指定输出滤波器以及扩展:

:~$ soffice --headless --convert-to txt:Text testdoc.doc
convert /home/steeldriver/testdoc.doc -> /home/steeldriver/testdoc.txt using Text
Overwriting: /home/steeldriver/testdoc.txt

:~$ file testdoc.txt
testdoc.txt: UTF-8 Unicode (with BOM) text

请注意,这仍然是带有 BOM 的 UTF-8:我还没有找到能产生纯 ASCII 的过滤器。如果你确实需要 ASCII,那么总会有iconv例如

iconv -t ASCII//TRANSLIT testdoc.txt

相关内容