linux环境下如何将word转为pdf
时间: 2023-11-12 11:03:40 浏览: 43
在 Linux 环境下,可以使用 LibreOffice 套件中的 soffice 命令将 Word 转换成 PDF,具体步骤如下:
1. 安装 LibreOffice
```
sudo apt-get update
sudo apt-get install libreoffice
```
2. 使用 soffice 命令将 Word 转换为 PDF
```
soffice --convert-to pdf filename.docx
```
其中,`filename.docx` 为需要转换的 Word 文件名。
转换完成后,会在同一目录下生成一个同名的 PDF 文件。
注意:如果提示“soffice command not found”错误,需要先将 soffice 命令所在的路径添加到环境变量中,或者直接使用完整路径调用 soffice 命令。
相关问题
linux环境下javaspringBoot如何将word转为pdf
在 Spring Boot 项目中,可以使用 Apache POI 和 iText 库来实现 Word 转 PDF 的功能。
1. 添加 Maven 依赖
在 pom.xml 文件中添加以下依赖:
```
<dependencies>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.13.2</version>
</dependency>
</dependencies>
```
2. 实现 Word 转 PDF 的方法
```
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.poi.xwpf.converter.pdf.PdfConverter;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import com.itextpdf.text.Document;
import com.itextpdf.text.pdf.PdfWriter;
public class WordToPdfConverter {
public void convert(String inputPath, String outputPath) throws IOException {
// 读取 Word 文档
InputStream in = new FileInputStream(new File(inputPath));
XWPFDocument document = new XWPFDocument(in);
// 将 Word 转换为 PDF
OutputStream out = new FileOutputStream(new File(outputPath));
PdfConverter.getInstance().convert(document, out, null);
out.close();
// PDF 文档加密(可选)
encryptPdf(outputPath);
}
// PDF 文档加密
private void encryptPdf(String pdfPath) throws IOException {
String ownerPassword = "123456";
String userPassword = "123456";
String permissions = "PRINTING";
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(pdfPath));
writer.setEncryption(ownerPassword.getBytes(), userPassword.getBytes(),
PdfWriter.ALLOW_PRINTING, PdfWriter.ENCRYPTION_AES_128);
document.open();
document.close();
}
}
```
其中,`inputPath` 为 Word 文件路径,`outputPath` 为 PDF 文件路径。在 `convert()` 方法中,先读取 Word 文档,然后使用 `PdfConverter` 将 Word 转换为 PDF,最后使用 `encryptPdf()` 方法对 PDF 进行加密。
3. 调用 Word 转 PDF 的方法
在 Spring Boot 控制器中调用 `WordToPdfConverter` 类的 `convert()` 方法即可实现 Word 转 PDF 的功能。
```
import java.io.IOException;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class FileController {
@PostMapping("/word-to-pdf")
public String wordToPdf(@RequestParam("inputPath") String inputPath,
@RequestParam("outputPath") String outputPath) {
WordToPdfConverter converter = new WordToPdfConverter();
try {
converter.convert(inputPath, outputPath);
return "Word 转 PDF 成功";
} catch (IOException e) {
e.printStackTrace();
return "Word 转 PDF 失败";
}
}
}
```
以上就是使用 Java Spring Boot 将 Word 转为 PDF 的方法。需要注意的是,在使用 iText 对 PDF 进行加密时,需要添加 iText 的依赖。
在linux环境下word转pdf
在Linux环境下,可以使用LibreOffice将Word文档转换为PDF格式。具体操作如下:
1.安装LibreOffice
在终端中输入以下命令:
```shell
sudo apt-get install libreoffice
```
2.将Word文档转换为PDF
在终端中输入以下命令:
```shell
libreoffice --headless --convert-to pdf 1.docx
```
其中,1.docx是要转换的Word文档的文件名。
3.查看PDF文件
转换完成后,可以在同一目录下找到生成的PDF文件。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)