preface

Recently, I have a requirement to convert Word into PDF. Since I have never done this before, I started baidu programming. Generally speaking, there are a lot of scattered information, but not much really general, so I sorted it out, there are many methods to achieve this demand, specific reference points I direct this article, this article only introduces the Method of SpringBoot + OpenOffice.

Download openOffice

First click download, then install the custom path or default installation, as follows:

Install the package directly after downloading, installation steps will not be described, always the next step is to pay attention to the installation path, the program will be used.

Code – POM dependencies

This step may have some errors, but I have already trod on it, so I will simply introduce you to it. This step may have some errors: 1. This problem is mostly the private server pointed to in the Maven configuration file. If it is the default, it will be slow and lead to download failure. It is recommended to point to the domestic Ali cloud library, this step can be baidu. 2. There is a version conflict in the local Maven inventory. It is recommended to delete the local library first and then download it again. (there are other pits that are welcome to be added, basically maven’s pit)… Tips :(if this step takes too long and the maven version crashes, you can try to overwrite your local maven library with my local maven library. Make a backup before you try.) To download: Click download me

<! Jar begin --> <dependency> <groupId>org.jodconverter</groupId> <artifactId>jodconverter -- core</artifactId> < version > 4.2.2 < / version > < / dependency > < the dependency > < groupId > org. Jodconverter < / groupId > < artifactId > jodconverter - spring - the boot - starter < / artifactId > < version > 4.2.2 < / version > < / dependency > < the dependency > < the groupId > org. Jodconverter < / groupId > < artifactId > jodconverter - local < / artifactId > < version > 4.2.2 < / version > < / dependency > <! Jar end -->Copy the code

Code – configuration files

Application configuration is as follows:

Jodconverter. Local. Enabled = true # set OpenOffice home directory jodconverter. Local. Office - home: D:\\OpenOffice4.1.7 # Maximum number of OpenOffice processes before OpenOffice restarts jodconverter local.max-tasks-per-process: Port numbers: 8100,8101,8102 # Start multiple OpenOffice processes, one for each portCopy the code

If I install this machine to D:\OpenOffice4.1.7, the specific files are as follows:

Four, code – front page

The HTML code is as follows:

<input type="button" value="PDF" style="width:100%;"  onclick="convertPdf()">
Copy the code

The js code is as follows:

function convertPdf(a) {
        $.ajax({
            type: 'POST',
            url: '/convertPdf',
            success: function (result) {

            },
            error: function () {

            }
        });
    }
Copy the code

5. Code-controller layer

The code looks like this:

import org.jodconverter.DocumentConverter;
import org.jodconverter.office.OfficeException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.File;

@Controller
public class WordToPdf {

	@Autowired
	private DocumentConverter converter;

	@RequestMapping("/convertPdf")
	@ResponseBody
	public String toPdf(String filepath) throws OfficeException {

		filepath="D:\\test.docx";	// The converted Word document
		File word = new File(filepath);
		// Cut the string to change the suffix of word to PDF
		String pdfpath= filepath.substring(0,filepath.lastIndexOf(".")) +".pdf";
		File pdf = new File(pdfpath);
		// File conversion
		converter.convert(word).to(pdf).execute();
		return "true"; }}Copy the code

6. Effect display

This address is exactly the address entered in the Java code, you can modify the path of the input and output files according to specific requirements: