Step 1: install LibreOffice for Windows.
1. 1 Go to official website LibreOffice.
. libreoffice.org/
1.2 download
1.2. 1 Step 1: Enter official website and click Download.
1.2.2 Step 2: Select the version suitable for your system.
1.2.2 Step 3: Click Download in the red box.
1.3 installation and configuration
1.3. 1 Step 1: Double-click the downloaded file.
1.3.2 Step 2: Click Next.
1.3.3 Step 3: Select Custom here.
1.3.4 Step 4: You can use the default road strength here or choose to install the road strength yourself.
1.3.5 Step 5: Click Next.
1.3.6 Step 6: Click Install.
1.3.7 Step 7: If windows 10 = > right-click and select Attribute => advanced system settings => environment variable =>, find the system environment variable path, and create and add the path D:\LibreOffice\program (note that this path is configured according to the road strength of your own installation, and my installation is under the D drive).
1.3.8 Step 8 Then windows+R opens the cmd command window => Enter officeopen libre office to indicate successful installation.
Step 2: Operate LibreOffice with java code to convert (doc, ppt, xls) files into PDF files.
1. 1: Open IDEA or eclipse creates a SpringBoot project.
pom.xml
& lt? Xml version =" 1.0 "encoding ="UTF-8"? & gt& ltproject xmlns = " . Liu app . workable . converter & lt; /groupId & gt;
& ltartifactId & gt operational converter < lt/artifactid >
& lt version & gtv1.4.2 < /version & gt;
& lt/dependency & gt;
& lt dependency & gt
& ltgroupId & gtorg . spring framework . boot & lt; /groupId & gt;
& ltartifactId & gt Test of Spring Starter
& lt/scope & gt; Test & lt/scope >
& lt/dependency & gt;
& lt/dependencies & gt;
& lt build & gt
& lt plug-in & gt
& lt plug-in & gt
& ltgroupId & gtorg . spring framework . boot & lt; /groupId & gt;
& ltartifactId & gtspring-boot-maven-plugin</artifact id & gt;
& lt/plugin & gt;
& lt/plugins & gt;
& lt/build & gt; & lt/project & gt; 123456789 10 1 1 12 13 14 15 16 17 18 19202 12223242526272829303 1323334353637383940
1.2 Create ConvertOfficePDFUtils.java class. Take the file name yourself.
/**
* @ describes the libreoffice tool class
* @ Author holle
* @create 2020/ 12/24
* @ from 1.0.0
*/package com . file . preview . utils; Import org.slf4j.logger; Import org.slf4j.loggerfactory; Import org.springframework.stereotype.service; Import java.io.ioexception; Import java.io.inputstream; @ service public class ConvertOfficePDFUtils {
private static Logger Logger = Logger factory . get Logger(convertofficepdfutils . class);
/**
* use libreOffice to convert Office documents into pdf.
* @param inputFile? Target file address
* @param pdfFile output folder
* @ Return
*/
public static boolean convertOfficePDF(String input file,String pdfFile){
long start = system . current time millis();
String command;
Boolean sign;
string osName = system . getproperty(" OS . name ");
if(osname . contains(" Windows ")){
command = " cmd/c start soffice-headless-invisible-convert-to pdf:writer _ pdf _ Export "+input file+"-outdir "+pdf file;
} Otherwise {
command = " libre office-headless-invisible-convert-to pdf:writer _ pdf _ Export "+input file+"-outdir "+pdf file;
}
Flag =executeLibreOfficeCommand (command);
long end = system . current time millis();
Logger.debug ("Time: {} ms ",end-start);
Return flag;
}
/**
* execute commands and instructions
* @param command
* @ Return
*/
Public static Boolean executeLibreOfficeCommand (string command) (
Logger.info ("Start conversion ...");
Technological process; //A process can control the execution of a child process or obtain the information of the child process.
Try {
Logger.debug ("convertoffice2pdcmd: {0}", command);
process = Runtime.getRuntime()。 Exec (command); The exec () method instructs the Java virtual machine to create a subprocess to execute the specified executable program, and returns the process object instance corresponding to the subprocess.
//The following two can get iostream.
InputStream errorStream = process . get errorStream();
InputStream InputStream = process . getinputstream();
} catch (IOException e) {
Logger.error ("convertoffice2pdf {} error", command,? e);
Returns false
}
int exit status = 0;
Try {
exit status = process . wait for(); //Wait for the subprocess to complete before executing. The return value is the return value of the child thread, and returning 0 means normal ending.
//The second method that accepts the return value
int I = process . exit value(); //Receive the return value after execution.
logger . debug(" I-"+I);
} catch (InterruptedException e) {
logger . error(" interrupted exception? ConvertOffice2PDF {} ",command, e);
Returns false
}
if (exitStatus! = 0) {
logger . error(" convertoffice 2 pdf cmd exit status { } ",exit status);
} Otherwise {
logger . debug(" convertoffice 2 pdf cmd exit status { } ",exit status);
}
process . destroy(); //Destroy the subprocess
Logger.info ("end of conversion ...");
Return true
}} 123456789 10 1 1 12 13 14 15 16 17 18 19202 1 2223242526272829303 132333435363738 39404 14243444546474849505 15253545556575859606 16263646566676869707 17273747576777879
1.3 Create a test class.
/**
* @ Describe the test class
* @ Author holle
* @create 2020/ 12/24
* @ from 1.0.0
*/package com . test; Import com; file; preview; filepreviewapplication; Import com.file.preview.utils.convertofficepdfutiles; Import org.junit.test; Import org.junit.runner.runwith; Import org.springframework.boot.test.context.springboottest; Import org.springframework.test.context.junit4.springjunit4classrunner; Import javax.annotation.resource; Import java.io.ioexception; @ runwith (springjunit4classrunner.class) @ springboottest (classes = filepreviewapplication.class) public class TestOffice {
@ Resources
private ConvertOfficePDFUtils ConvertOfficePDFUtils;
@ Test
public void JunintTestPDF(){
long start = system . current time millis();
string src path = " C:/Users/holler/Desktop/*/*。 docx ",des path = " C:/Users/holler/Desktop/* ";
String command = "";
string osName = system . getproperty(" OS . name ");
if(osname . contains(" Windows ")){
//Enter the cmd command; Add: srcPath indicates the path where the file is located; The path of the file output.
command = " so ffice-headless-convert-to pdf "+src path+"-outdir "+desPath;
Convertofficepdfutiles. executelibreofficecommand (command);
}
long end = system . current time millis();
system . out . println(" Time:{ } ms "+(end-start));
}
} ? 123456789 10 1 1 12 13 14 15 16 17 18 19202 12223242526272829303 132333435363738
path+"-outdir "+desPath;
Convertofficepdfutiles. executelibreofficecommand (command);
}
long end = system . current time millis();
system . out . println(" Time:{ } ms "+(end-start));
}
}