Word to pdf, xls to pdf, ppt to pdf under Linux system

The technical solution for converting word to pdf, for reference. [doc/docx/ppt/pptx/xls/xlsx all support conversion]
 
         This solution is completed by Java combined with shell commands . It is different from the previous solution that only relies on java components to convert or print pdf. The target server needs to install the office suite. The optional solutions are OpenOffice and LibreOffice [the two sources are one product]. After installing the office After the suite, use the script provided by office to complete the conversion. When installing LibreOffice, in order to avoid complex dependencies, it is recommended to use yum to install: yum install libreoffice
 
         Script content: soffice --headless --invisible --convert-to pdf source office document path --outdir directory storage directory

 
The above script needs to be written in the form of sh script, in which the source path needs to be configured as a parameter, which is passed in when the Java program calls the sh script. After the conversion, the pdf document with the same name can be obtained from the specified directory, and then the business-related logical processing. Note: The temporary directory needs to be cleaned up regularly to prevent too many temporary files from being stored, causing the disk to be full.

Java calling code:

 

  try {
            String shpath = "/tmp/topdf.sh";//Script path, the source doc path needs to be configured as a variable in the script, which is entered as a parameter when called by the downlink program
String cmd = "param";//xshell script input parameters
            Process ps = Runtime.getRuntime().exec(shpath,cmd);//
            ps.waitFor();//Wait for the conversion result

            BufferedReader br = new BufferedReader(new InputStreamReader(ps.getInputStream()));
            StringBuffer sb = new StringBuffer();
            String line;
            while ((line = br.readLine()) != null) {
                sb.append(line).append("\n");
            }
            String result = sb.toString();
            System.out.println(result);
        } catch (Exception e) {
            e.printStackTrace ();
        }
 

 

#!/bin/sh  
  
source=$1
echo ${source}
soffice --headless --invisible --convert-to pdf ${source} --outdir /tmp/
 

 

But found that the command no longer responds after running for a period of time, and there is no document result. After checking the server, libreoffice is running fine, but the soffice --convert-to command is unresponsive and no output. View active threads as follows:

 


 

There are two related active instances running.

 

After reading the information, this is a bug generated by LO in 2011, related bug list reference:

https://bugs.documentfoundation.org/show_bug.cgi?id=37531 

 

https://bugs.documentfoundation.org/show_bug.cgi?id=45026     



 

大体意思是LO GUI实例一旦运行过一个,再运行一个实例的话,就会出现无响应的问题。解决思路有两个:

1、杀掉所有的libreoffice实例,即将上面的两个实例杀掉

2、执行命令时增加一行参数,经测试下面两条命令均可以执行【必须保证命令的执行要有权限】:

soffice --headless --convert-to pdf ${source-file} --outdir ${target-path}  "-env:UserInstallation=file:///tmp/LibreOffice_Conversion_${USER}"

soffice --headless --convert-to pdf ${source-file} --outdir ${target-path}  -env:UserInstallation=file:///home/user/.libreoffice-alt     

 

 

程序员,除了编码,生活还应该有沉淀!

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326681136&siteId=291194637
pdf